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

fix: Don't allow duplicated tag values in the Select #19283

Merged
Merged
Changes from 1 commit
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
44 changes: 25 additions & 19 deletions superset-frontend/src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -385,31 +385,37 @@ 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[];
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a ensureIsArray function in @superset-ui/core


const handleOnSelect = (
selectedValue: string | number | AntdLabeledValue,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename this local selectedValue to selectedItem so not to confuse with selectValue in the parent context which may also be an array?

) => {
if (isSingleMode) {
setSelectValue(selectedValue);
} else if (
typeof selectedValue === 'number' ||
typeof selectedValue === 'string'
) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe these two if/else can be rewritten with hasOption in Select/utils.

setSelectValue(previousState => {
const primitiveArray = valueAsArray<string | number>(previousState);
// Tokenized values can contain duplicated values
if (!primitiveArray.some(value => value === selectedValue)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!primitiveArray.some(value => value === selectedValue)) {
if (!primitiveArray.includes(selectedValue)) {

return [...primitiveArray, selectedValue as string | number];
}
return previousState;
});
} else {
const currentSelected = selectValue
? Array.isArray(selectValue)
? selectValue
: [selectValue]
: [];
if (
typeof selectedValue === 'number' ||
typeof selectedValue === 'string'
) {
setSelectValue([
...(currentSelected as (string | number)[]),
selectedValue as string | number,
]);
} else {
setSelectValue([
...(currentSelected as AntdLabeledValue[]),
selectedValue as AntdLabeledValue,
]);
}
setSelectValue(previousState => {
const objectArray = valueAsArray<AntdLabeledValue>(previousState);
// Tokenized values can contain duplicated values
if (!objectArray.some(object => object.value === selectedValue.label)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we comparing value to labels?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙈 Lack of coffee

return [...objectArray, selectedValue as AntdLabeledValue];
}
return previousState;
});
}
setInputValue('');
};
Expand Down