Skip to content

Commit

Permalink
Addresses comments and adds test
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-s-molina committed Mar 21, 2022
1 parent 323f43f commit 05602ca
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
11 changes: 11 additions & 0 deletions superset-frontend/src/components/Select/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,17 @@ test('searches for custom fields', async () => {
expect(screen.getByText(NO_DATA)).toBeInTheDocument();
});

test('removes duplicated values', async () => {
render(<Select {...defaultProps} mode="multiple" allowNewOptions />);
await type('a,b,b,b,c,d,d');
const values = await findAllSelectValues();
expect(values.length).toBe(4);
expect(values[0]).toHaveTextContent('a');
expect(values[1]).toHaveTextContent('b');
expect(values[2]).toHaveTextContent('c');
expect(values[3]).toHaveTextContent('d');
});

test('renders a custom label', async () => {
const options = [
{ label: 'John', value: 1, customLabel: <h1>John</h1> },
Expand Down
34 changes: 10 additions & 24 deletions superset-frontend/src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -385,34 +385,20 @@ const Select = (

const hasCustomLabels = fullSelectOptions.some(opt => !!opt?.customLabel);

const valueAsArray = <T,>(value: any): T[] => {
const array = value ? (Array.isArray(value) ? value : [value]) : [];
return array as T[];
};

const handleOnSelect = (
selectedValue: string | number | AntdLabeledValue,
) => {
const handleOnSelect = (selectedItem: string | number | AntdLabeledValue) => {
if (isSingleMode) {
setSelectValue(selectedValue);
} else if (
typeof selectedValue === 'number' ||
typeof selectedValue === 'string'
) {
setSelectValue(previousState => {
const primitiveArray = valueAsArray<string | number>(previousState);
// Tokenized values can contain duplicated values
if (!primitiveArray.some(value => value === selectedValue)) {
return [...primitiveArray, selectedValue as string | number];
}
return previousState;
});
setSelectValue(selectedItem);
} else {
setSelectValue(previousState => {
const objectArray = valueAsArray<AntdLabeledValue>(previousState);
const array = ensureIsArray(previousState);
const isObject = typeof selectedItem === 'object';
const value = isObject ? selectedItem.value : selectedItem;
// Tokenized values can contain duplicated values
if (!objectArray.some(object => object.value === selectedValue.label)) {
return [...objectArray, selectedValue as AntdLabeledValue];
if (!hasOption(value, array)) {
const result = [...array, selectedItem];
return isObject
? (result as AntdLabeledValue[])
: (result as (string | number)[]);
}
return previousState;
});
Expand Down

0 comments on commit 05602ca

Please sign in to comment.