Skip to content

Commit

Permalink
πŸ›(react) fix options reset for select mono searchable
Browse files Browse the repository at this point in the history
Allows to clear selected options when select mono searchable
uses options fetching and is controlled.
  • Loading branch information
daproclaima committed Jun 27, 2024
1 parent c7fe0bc commit 16a078b
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/react/src/components/Forms/Select/mono-searchable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ export const SelectMonoSearchable = forwardRef<SelectHandle, SubProps>(
return;
}

// this block will not be triggered when component uses options fetching (otherwise it will conflict with the value
// passed to onInputChange)
if (Array.isArray(props.options)) {
const selectedItem = downshiftReturn.selectedItem
? optionToValue(downshiftReturn.selectedItem)
Expand All @@ -181,6 +183,27 @@ export const SelectMonoSearchable = forwardRef<SelectHandle, SubProps>(
}
}, [props.value, props.options, inputFilter]);

useEffect(() => {
if (!isAsyncOptionsFetching) {
return;
}

const selectedItem = downshiftReturn.selectedItem
? optionToValue(downshiftReturn.selectedItem)
: undefined;

const optionToSelect = optionsToDisplay.find(
(option) => optionToValue(option) === props.value,
);

// Already selected
if (optionToSelect && selectedItem === props.value) {
return;
}

downshiftReturn.selectItem(optionToSelect ?? null);
}, [props.value]);

// Even there is already a value selected, when opening the combobox menu we want to display all available choices.
useEffect(() => {
props.onSearchInputChange?.({ target: { value: inputFilter } });
Expand Down

0 comments on commit 16a078b

Please sign in to comment.