diff --git a/superset-frontend/src/addSlice/AddSliceContainer.tsx b/superset-frontend/src/addSlice/AddSliceContainer.tsx index db8da84beb4dd..b03763f055ed5 100644 --- a/superset-frontend/src/addSlice/AddSliceContainer.tsx +++ b/superset-frontend/src/addSlice/AddSliceContainer.tsx @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -import React, { ReactNode } from 'react'; +import React from 'react'; import rison from 'rison'; import { styled, t, SupersetClient, JsonResponse } from '@superset-ui/core'; import { Steps } from 'src/common/components'; @@ -24,6 +24,7 @@ import Button from 'src/components/Button'; import { Select } from 'src/components'; import { FormLabel } from 'src/components/Form'; import { Tooltip } from 'src/components/Tooltip'; +import { LabeledValues } from 'src/components/Select/Select'; import VizTypeGallery, { MAX_ADVISABLE_VIZ_GALLERY_WIDTH, @@ -252,11 +253,7 @@ export default class AddSliceContainer extends React.PureComponent< return SupersetClient.get({ endpoint: `/api/v1/dataset/?q=${query}`, }).then((response: JsonResponse) => { - const list: { - customLabel: ReactNode; - label: string; - value: string; - }[] = response.json.result.map((item: Dataset) => ({ + const list: LabeledValues = response.json.result.map((item: Dataset) => ({ value: `${item.id}__${item.datasource_type}`, customLabel: this.newLabel(item), label: item.table_name, diff --git a/superset-frontend/src/components/Select/Select.tsx b/superset-frontend/src/components/Select/Select.tsx index a36a0d569aeb7..0caa659148fd8 100644 --- a/superset-frontend/src/components/Select/Select.tsx +++ b/superset-frontend/src/components/Select/Select.tsx @@ -32,7 +32,6 @@ import { styled, t } from '@superset-ui/core'; import AntdSelect, { SelectProps as AntdSelectProps, SelectValue as AntdSelectValue, - LabeledValue as AntdLabeledValue, } from 'antd/lib/select'; import { DownOutlined, SearchOutlined } from '@ant-design/icons'; import debounce from 'lodash/debounce'; @@ -44,7 +43,21 @@ import { hasOption } from './utils'; const { Option } = AntdSelect; -type AntdSelectAllProps = AntdSelectProps; +export type RawValue = string | number; + +export interface LabeledValue { + key?: string; + value: RawValue; + label: ReactNode; + customLabel?: ReactNode; +} + +export type RawValues = RawValue[]; +export type LabeledValues = LabeledValue[]; +export type OptionsType = LabeledValues; + +type SelectValue = RawValue | LabeledValue | RawValues | LabeledValues; +type AntdSelectAllProps = AntdSelectProps; type PickedSelectProps = Pick< AntdSelectAllProps, @@ -63,15 +76,8 @@ type PickedSelectProps = Pick< | 'value' >; -type OptionsProps = Exclude; - -export interface OptionsType extends Omit { - label?: string; - customLabel?: ReactNode; -} - export type OptionsTypePage = { - data: OptionsType; + data: LabeledValues; totalCount: number; }; @@ -157,7 +163,7 @@ export interface SelectProps extends PickedSelectProps { * Works in async mode only (See the options property). */ onError?: (error: string) => void; - sortComparator?: (a: AntdLabeledValue, b: AntdLabeledValue) => number; + sortComparator?: (a: LabeledValue, b: LabeledValue) => number; } const StyledContainer = styled.div` @@ -230,7 +236,7 @@ const Error = ({ error }: { error: string }) => ( ); -const defaultSortComparator = (a: AntdLabeledValue, b: AntdLabeledValue) => { +const defaultSortComparator = (a: LabeledValue, b: LabeledValue) => { if (typeof a.label === 'string' && typeof b.label === 'string') { return a.label.localeCompare(b.label); } @@ -245,7 +251,7 @@ const defaultSortComparator = (a: AntdLabeledValue, b: AntdLabeledValue) => { * Can be used with string and number property values. * */ export const propertyComparator = - (property: string) => (a: AntdLabeledValue, b: AntdLabeledValue) => { + (property: string) => (a: LabeledValue, b: LabeledValue) => { if (typeof a[property] === 'string' && typeof b[property] === 'string') { return a[property].localeCompare(b[property]); } @@ -333,15 +339,15 @@ const Select = ({ if (Array.isArray(selectedValue)) { if (isLabeledValue) { found = - (selectedValue as AntdLabeledValue[]).find( + (selectedValue as LabeledValue[]).find( element => element.value === opt.value, ) !== undefined; } else { - found = selectedValue.includes(opt.value); + found = (selectedValue as RawValue[]).includes(opt.value); } } else { found = isLabeledValue - ? (selectedValue as AntdLabeledValue).value === opt.value + ? (selectedValue as LabeledValue).value === opt.value : selectedValue === opt.value; } @@ -355,16 +361,16 @@ const Select = ({ // fallback for custom options in tags mode as they // do not appear in the selectOptions state if (!isSingleMode && Array.isArray(selectedValue)) { - selectedValue.forEach((val: string | number | AntdLabeledValue) => { + selectedValue.forEach((val: string | number | LabeledValue) => { if ( !topOptions.find( tOpt => tOpt.value === - (isLabeledValue ? (val as AntdLabeledValue)?.value : val), + (isLabeledValue ? (val as LabeledValue)?.value : val), ) ) { if (isLabeledValue) { - const labelValue = val as AntdLabeledValue; + const labelValue = val as LabeledValue; topOptions.push({ label: labelValue.label, value: labelValue.value, @@ -393,9 +399,7 @@ const Select = ({ [isAsync, isSingleMode, labelInValue, selectOptions, sortComparator], ); - const handleOnSelect = ( - selectedValue: string | number | AntdLabeledValue, - ) => { + const handleOnSelect = (selectedValue: string | number | LabeledValue) => { if (isSingleMode) { setSelectValue(selectedValue); } else { @@ -414,21 +418,21 @@ const Select = ({ ]); } else { setSelectValue([ - ...(currentSelected as AntdLabeledValue[]), - selectedValue as AntdLabeledValue, + ...(currentSelected as LabeledValue[]), + selectedValue as LabeledValue, ]); } } setSearchedValue(''); }; - const handleOnDeselect = (value: string | number | AntdLabeledValue) => { + const handleOnDeselect = (value: string | number | LabeledValue) => { if (Array.isArray(selectValue)) { if (typeof value === 'number' || typeof value === 'string') { const array = selectValue as (string | number)[]; setSelectValue(array.filter(element => element !== value)); } else { - const array = selectValue as AntdLabeledValue[]; + const array = selectValue as LabeledValue[]; setSelectValue(array.filter(element => element.value !== value.value)); } } @@ -557,7 +561,7 @@ const Select = ({ } }; - const handleFilterOption = (search: string, option: AntdLabeledValue) => { + const handleFilterOption = (search: string, option: LabeledValue) => { if (typeof filterOption === 'function') { return filterOption(search, option); } @@ -643,22 +647,22 @@ const Select = ({ if (selectValue) { setSelectOptions(selectOptions => { const array = Array.isArray(selectValue) - ? (selectValue as AntdLabeledValue[]) - : [selectValue as AntdLabeledValue | string | number]; - const options: AntdLabeledValue[] = []; + ? (selectValue as LabeledValue[]) + : [selectValue as LabeledValue | string | number]; + const options: LabeledValue[] = []; const isLabeledValue = isAsync || labelInValue; array.forEach(element => { const found = selectOptions.find( (option: { value: string | number }) => isLabeledValue - ? option.value === (element as AntdLabeledValue).value + ? option.value === (element as LabeledValue).value : option.value === element, ); if (!found) { options.push( isLabeledValue - ? (element as AntdLabeledValue) - : ({ value: element, label: element } as AntdLabeledValue), + ? (element as LabeledValue) + : ({ value: element, label: element } as LabeledValue), ); } }); diff --git a/superset-frontend/src/components/TableSelector/index.tsx b/superset-frontend/src/components/TableSelector/index.tsx index 88ac9cefba47f..93c17392daca6 100644 --- a/superset-frontend/src/components/TableSelector/index.tsx +++ b/superset-frontend/src/components/TableSelector/index.tsx @@ -216,7 +216,7 @@ const TableSelector: FunctionComponent = ({ setLoadingTables(false); if (forceRefresh) addSuccessToast('List updated'); }) - .catch(e => { + .catch(() => { setLoadingTables(false); handleError(t('There was an error loading the tables')); }); diff --git a/superset-frontend/src/explore/components/controls/FilterControl/AdhocFilterEditPopoverSimpleTabContent/index.tsx b/superset-frontend/src/explore/components/controls/FilterControl/AdhocFilterEditPopoverSimpleTabContent/index.tsx index e86ed587c8e40..449ef0d8bbf1b 100644 --- a/superset-frontend/src/explore/components/controls/FilterControl/AdhocFilterEditPopoverSimpleTabContent/index.tsx +++ b/superset-frontend/src/explore/components/controls/FilterControl/AdhocFilterEditPopoverSimpleTabContent/index.tsx @@ -387,7 +387,7 @@ const AdhocFilterEditPopoverSimpleTabContent: React.FC = props => { ('column_name' in column && column.column_name) || ('label' in column && column.label), key: - ('id' in column && column.id) || + ('id' in column && String(column.id)) || ('optionName' in column && column.optionName) || undefined, customLabel: renderSubjectOptionLabel(column),