(react) rework the behavior of the Select component

We decided to change a bit the behavior of the Select:
- Do not trigger onChange on first render if value is defined
- Show all options on searchable select menu  opening even if there is an
  existing value.
- Clear the input field if no choice are selected
- Clear the added text to the input field when a value is already selected
This commit is contained in:
Nathan Vasse
2023-09-19 17:15:58 +02:00
committed by NathanVss
parent 732183420d
commit 4e1d0f0bc7
6 changed files with 199 additions and 15 deletions

View File

@@ -41,11 +41,19 @@ export const SelectMono = (props: SelectProps) => {
const commonDownshiftProps: SubProps["downshiftProps"] = {
initialSelectedItem: defaultSelectedItem,
onSelectedItemChange: (e: UseSelectStateChange<Option>) => {
props.onChange?.({
target: {
value: e.selectedItem ? optionToValue(e.selectedItem) : undefined,
},
});
const eventCmp = e.selectedItem ? optionToValue(e.selectedItem) : null;
const valueCmp = props.value ?? null;
// We make sure to not trigger a onChange event if the value are not different.
// This could happen on first render when the component is controlled, the value will be
// set inside a useEffect down in SelectMonoSearchable or SelectMonoSimple. So that means the
// downshift component will always render empty the first time.
if (eventCmp !== valueCmp) {
props.onChange?.({
target: {
value: e.selectedItem ? optionToValue(e.selectedItem) : undefined,
},
});
}
},
isItemDisabled: (item) => !!item.disabled,
};