Skip to content

Commit

Permalink
Fix allowNewOption
Browse files Browse the repository at this point in the history
  • Loading branch information
ktmud committed Mar 2, 2022
1 parent 00f60aa commit e4ae742
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
12 changes: 8 additions & 4 deletions superset-frontend/src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import { Spin } from 'antd';
import Icons from 'src/components/Icons';
import { getClientErrorObject } from 'src/utils/getClientErrorObject';
import { SLOW_DEBOUNCE } from 'src/constants';
import { hasOption } from './utils';
import { hasOption, hasOptionIgnoreCase } from './utils';

const { Option } = AntdSelect;

Expand All @@ -57,6 +57,8 @@ type PickedSelectProps = Pick<
| 'notFoundContent'
| 'onChange'
| 'onClear'
| 'onFocus'
| 'onBlur'
| 'placeholder'
| 'showSearch'
| 'value'
Expand Down Expand Up @@ -519,7 +521,7 @@ const Select = ({
const debouncedHandleSearch = useMemo(
() =>
debounce((search: string) => {
// async search will triggered in handlePaginatedFetch
// async search will be triggered in handlePaginatedFetch
setSearchedValue(search);
}, SLOW_DEBOUNCE),
[],
Expand All @@ -529,12 +531,14 @@ const Select = ({
const searchValue = search.trim();
if (allowNewOptions && isSingleMode) {
const newOption = searchValue &&
!hasOption(searchValue, selectOptions) && {
!hasOptionIgnoreCase(searchValue, selectOptions) && {
label: searchValue,
value: searchValue,
isNewOption: true,
};
const cleanSelectOptions = selectOptions.filter(opt => !opt.isNewOption);
const cleanSelectOptions = selectOptions.filter(
opt => !opt.isNewOption || hasOption(opt.value, selectValue),
);
const newOptions = newOption
? [newOption, ...cleanSelectOptions]
: cleanSelectOptions;
Expand Down
15 changes: 14 additions & 1 deletion superset-frontend/src/components/Select/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ensureIsArray } from '@superset-ui/core';
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
Expand Down Expand Up @@ -60,7 +61,19 @@ export function findValue<OptionType extends OptionTypeBase>(
return (Array.isArray(value) ? value : [value]).map(find);
}

export function hasOption(search: string, options: AntdOptionsType) {
export function hasOption<VT extends string | number>(
value: VT,
options?: VT | VT[] | { value: VT } | { value: VT }[],
) {
const optionsArray = ensureIsArray(options);
return (
optionsArray.find(x =>
typeof x === 'object' ? x.value === value : x === value,
) !== undefined
);
}

export function hasOptionIgnoreCase(search: string, options: AntdOptionsType) {
const searchOption = search.trim().toLowerCase();
return options.find(opt => {
const { label, value } = opt;
Expand Down

0 comments on commit e4ae742

Please sign in to comment.