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

[NDS-807] fix: select backspace does not clear value [v5] #674

Merged
Show file tree
Hide file tree
Changes from 2 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
38 changes: 29 additions & 9 deletions src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,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) {
kostasdano marked this conversation as resolved.
Show resolved Hide resolved
onChange(undefined);
setSearchValue('');
asyncSearch('');
}
}
}
},
onAlphaNumerical: () => {
if (isSearchable) {
Expand Down Expand Up @@ -109,9 +120,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 +139,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 +170,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 +183,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 +255,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
Loading