Skip to content

Commit

Permalink
Merge pull request #674 from Orfium/fix/NDS-807_select_backspace_does…
Browse files Browse the repository at this point in the history
…_not_clear_value_v5

[NDS-807] fix: select backspace does not clear value [v5]
  • Loading branch information
kostasdano authored Sep 27, 2023
2 parents 0211d71 + a0ca577 commit eb363f7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
40 changes: 31 additions & 9 deletions src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import PositionInScreen from 'components/utils/PositionInScreen';

export const emptyValue: SelectOption = { label: '', value: '' };

/** @TODO: Refactor component to reduce Cognitive Complexity */

const Select = React.forwardRef<HTMLInputElement, SelectProps>((props, ref) => {
const {
selectedOption,
Expand Down Expand Up @@ -81,6 +83,17 @@ const Select = React.forwardRef<HTMLInputElement, SelectProps>((props, ref) => {
onBackspace: () => {
debouncedOnChange('');
setIsOpen(true);

/** When the textField's value equals the selectedOption's label we should clear the field*/
if (!isMulti) {
if (selectedOption && onChange) {
if (selectedOption.label === textFieldValue) {
onChange(undefined);
setSearchValue('');
asyncSearch('');
}
}
}
},
onAlphaNumerical: () => {
if (isSearchable) {
Expand Down Expand Up @@ -109,9 +122,15 @@ const Select = React.forwardRef<HTMLInputElement, SelectProps>((props, ref) => {

const [searchValue, setSearchValue] = useState('');

const textFieldValue = Array.isArray(selectedOption)
? searchValue
: searchValue || selectedOption?.label;
const textFieldValue = useMemo(() => {
if (Array.isArray(selectedOption)) return searchValue;

if (!selectedOption) {
return searchValue;
}

return searchValue || selectedOption?.label;
}, [searchValue, selectedOption]);

const handleOptionClick = (option: SelectOption) => {
if (!isMulti) {
Expand All @@ -122,17 +141,20 @@ const Select = React.forwardRef<HTMLInputElement, SelectProps>((props, ref) => {
setSearchValue('');
}

if (onChange && selectedOption) {
if (isMulti) {
if (isMulti) {
if (onChange && selectedOption) {
if (isEqual(option, SELECT_ALL_OPTION)) {
onChange(options.filter((o) => !o.isDisabled));
} else {
onChange([...selectedOption, option]);
}
} else {
}
} else {
if (onChange) {
onChange(option);
}
}

combinedRefs.current?.focus();
};

Expand All @@ -150,7 +172,7 @@ const Select = React.forwardRef<HTMLInputElement, SelectProps>((props, ref) => {
* For Multiselect: [for now] when we select an option the SelectMenu closes but the user
* can still type on the input field (so they must be able to see the SelectMenu)
*/
if (!open) {
if (!isOpen) {
setIsOpen(true);
}

Expand All @@ -163,7 +185,7 @@ const Select = React.forwardRef<HTMLInputElement, SelectProps>((props, ref) => {
minCharactersToSearch,
});
},
[debouncedOnChange, isAsync, isSearchable, minCharactersToSearch]
[debouncedOnChange, isAsync, isOpen, isSearchable, minCharactersToSearch]
);

const filteredOptions = useMemo(() => {
Expand Down Expand Up @@ -235,7 +257,7 @@ const Select = React.forwardRef<HTMLInputElement, SelectProps>((props, ref) => {
if (isMulti) {
onChange([]);
} else {
onChange(emptyValue);
onChange(undefined);
}
}
asyncSearch('');
Expand Down
4 changes: 4 additions & 0 deletions src/components/Select/__snapshots__/Select.stories.storyshot
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ exports[`Storyshots Design System/Select Async Select 1`] = `
readOnly={false}
required={false}
role="combobox"
value=""
/>
<label
className="emotion-9"
Expand Down Expand Up @@ -694,6 +695,7 @@ exports[`Storyshots Design System/Select Async Select with min characters 1`] =
readOnly={false}
required={false}
role="combobox"
value=""
/>
<label
className="emotion-9"
Expand Down Expand Up @@ -2399,6 +2401,7 @@ exports[`Storyshots Design System/Select MultiSelect 1`] = `
readOnly={false}
required={false}
role="combobox"
value=""
/>
<label
className="emotion-58"
Expand Down Expand Up @@ -4717,6 +4720,7 @@ exports[`Storyshots Design System/Select Select with label and statuses 1`] = `
readOnly={false}
required={false}
role="combobox"
value=""
/>
<label
className="emotion-21"
Expand Down
2 changes: 1 addition & 1 deletion src/components/Select/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export type MultiSelectProps = {
};
export type SingleSelectProps = {
/** The function that is used to return the selected options */
onChange?: (selectedOption: SelectOption) => void;
onChange?: (selectedOption?: SelectOption) => void;
/** If false the user can select one option */
isMulti?: never;
/** the value of the select if select is controlled */
Expand Down
3 changes: 3 additions & 0 deletions src/components/storyUtils/SelectShowcase/SelectShowcase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type SelectShowcaseProps = {
};

const SelectShowcase: React.FC<SelectShowcaseProps> = ({ minCharactersToSearch = 0 }) => {
const [selectedOption, setSelectedOption] = useState<SelectOption>();
const [options, setOptions] = useState<SelectOption[]>(dummyUnrefinedData);
const [isLoading, setIsLoading] = React.useState(false);

Expand All @@ -39,6 +40,8 @@ const SelectShowcase: React.FC<SelectShowcaseProps> = ({ minCharactersToSearch =
isAsync
label={'Flavour'}
options={options}
selectedOption={selectedOption}
onChange={setSelectedOption}
asyncSearch={mockedApiCall}
isLoading={isLoading}
onKeyPress={() => setIsLoading(true)}
Expand Down

0 comments on commit eb363f7

Please sign in to comment.