Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear combobox value when invalid after suggestions update #806

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions ui-core/src/components/inputs/ComboBox/ComboBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
useState,
} from 'react';

import { ChevronDown, XCircle } from '@osrd-project/ui-icons';

Check warning on line 12 in ui-core/src/components/inputs/ComboBox/ComboBox.tsx

View workflow job for this annotation

GitHub Actions / build

Unable to resolve path to module '@osrd-project/ui-icons'
import cx from 'classnames';

import { normalizeString } from './utils';
Expand Down Expand Up @@ -119,6 +119,17 @@

useEffect(() => {
setFilteredSuggestions(sortedSuggestions);

const isInputValid = sortedSuggestions.some(
(suggestion) =>
normalizeString(getSuggestionLabel(suggestion)) === normalizeString(inputValue)
);

if (!isInputValid) {
setInputValue('');
setSelectedOption(null);
onSelectSuggestion?.(undefined);
}
}, [sortedSuggestions]);

const handleInputChange: ChangeEventHandler<HTMLInputElement> = (e) => {
Expand Down
37 changes: 36 additions & 1 deletion ui-core/src/stories/ComboBox.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect, useState } from 'react';

import type { Meta, StoryObj } from '@storybook/react';
import '@osrd-project/ui-core/dist/theme.css';
Expand Down Expand Up @@ -126,3 +126,38 @@ export const DisabledDefaultFilter: Story = {
disableDefaultFilter: true,
},
};

export const DynamicSuggestions: Story = {
render: (args) => {
const [dynamicSuggestions, setDynamicSuggestions] = useState<Suggestion[]>([
{ id: '1', label: 'Manuel' },
{ id: '2', label: 'Manolo' },
{ id: '3', label: 'Maria' },
{ id: '4', label: 'Miguel' },
]);

// Simulate a dynamic update to the suggestions list after 5 seconds
useEffect(() => {
const timeout = setTimeout(() => {
setDynamicSuggestions([
{ id: '1', label: 'Consuela' },
{ id: '2', label: 'Juan' },
{ id: '3', label: 'Jose' },
{ id: '4', label: 'Ana' },
{ id: '5', label: 'Pedro' },
{ id: '6', label: 'Lucia' },
{ id: '7', label: 'Carlos' },
{ id: '8', label: 'Elena' },
]);
}, 5000);

return () => clearTimeout(timeout);
}, []);

return <ComboBox {...args} label="Dynamic Suggestions" suggestions={dynamicSuggestions} />;
},
args: {
label: 'Dynamic Suggestions',
type: 'text',
},
};
Loading