Skip to content

Commit

Permalink
ui-core: add prop to disable default filtering in combo box component
Browse files Browse the repository at this point in the history
Signed-off-by: SarahBellaha <sarah.bellaha@sncf.fr>
  • Loading branch information
SarahBellaha committed Oct 9, 2024
1 parent 0ca64c1 commit 97451f1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
11 changes: 9 additions & 2 deletions ui-core/src/components/inputs/ComboBox/ComboBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type ComboBoxProps<T> = InputProps & {
exactSearch?: boolean;
value?: string;
onSelectSuggestion?: (option: T | undefined) => void;
disableDefaultFilter?: boolean;
};

const ComboBox = <T,>({
Expand All @@ -35,6 +36,7 @@ const ComboBox = <T,>({
value,
small,
onSelectSuggestion,
disableDefaultFilter = false,
...inputProps
}: ComboBoxProps<T>) => {
const [filteredSuggestions, setFilteredSuggestions] = useState<T[]>([]);
Expand All @@ -46,8 +48,13 @@ const ComboBox = <T,>({
const inputRef = useRef<HTMLInputElement>(null);

const sortedSuggestions = useMemo(
() => suggestions.sort((a, b) => getSuggestionLabel(a).localeCompare(getSuggestionLabel(b))),
[suggestions, getSuggestionLabel]
() =>
!disableDefaultFilter
? [...suggestions].sort((a, b) =>
getSuggestionLabel(a).localeCompare(getSuggestionLabel(b))
)
: suggestions,
[suggestions, getSuggestionLabel, disableDefaultFilter]
);

const showSuggestions = isInputFocused && filteredSuggestions.length > 0 && !inputProps.disabled;
Expand Down
13 changes: 11 additions & 2 deletions ui-core/src/stories/ComboBox.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ type Suggestion = { id: string; label: string };

const suggestions = [
{ id: '1', label: 'Manuel' },
{ id: '2', label: 'Manuela' },
{ id: '3', label: 'Manuella' },
{ id: '2', label: 'Consuela' },
{ id: '3', label: 'Juan' },
{ id: '4', label: 'Manolo' },
] as Suggestion[];

const meta: Meta<typeof ComboBox> = {
Expand Down Expand Up @@ -97,3 +98,11 @@ export const SmallInput: Story = {
small: true,
},
};

export const DisabledDefaultFilter: Story = {
args: {
label: 'Name',
type: 'text',
disableDefaultFilter: true,
},
};

0 comments on commit 97451f1

Please sign in to comment.