mirror of
https://github.com/apache/superset.git
synced 2026-04-21 00:54:44 +00:00
* Clean up and reorganize effects * Enhance optionFilterProps * Render custom label * Remove prop filtering * Create options * Create option from value in single mode * Change to customLabel * Show search by default * Update superset-frontend/src/components/Select/Select.tsx Co-authored-by: Michael S. Molina <70410625+michael-s-molina@users.noreply.github.com> * Update superset-frontend/src/components/Select/Select.tsx Co-authored-by: Michael S. Molina <70410625+michael-s-molina@users.noreply.github.com> * Update superset-frontend/src/components/Select/Select.tsx Co-authored-by: Michael S. Molina <70410625+michael-s-molina@users.noreply.github.com> * Apply minor changes * Fixes a bug that was failing CI * Adds more tests to the component * Apply customLabel in ColorSchemeControl * Remove customLabel from rendered Option * Hide No data when allowNewOptions * Remove unnecessary prop from tests * Adjust loading height * Show no data with fetchOnlyOnSearch Co-authored-by: Michael S. Molina <70410625+michael-s-molina@users.noreply.github.com> Co-authored-by: Michael S. Molina <michael.s.molina@gmail.com>
75 lines
2.6 KiB
TypeScript
75 lines
2.6 KiB
TypeScript
/**
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
* or more contributor license agreements. See the NOTICE file
|
|
* distributed with this work for additional information
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
* to you under the Apache License, Version 2.0 (the
|
|
* "License"); you may not use this file except in compliance
|
|
* with the License. You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing,
|
|
* software distributed under the License is distributed on an
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
* KIND, either express or implied. See the License for the
|
|
* specific language governing permissions and limitations
|
|
* under the License.
|
|
*/
|
|
import {
|
|
OptionTypeBase,
|
|
ValueType,
|
|
OptionsType,
|
|
GroupedOptionsType,
|
|
} from 'react-select';
|
|
|
|
import { OptionsType as AntdOptionsType } from './Select';
|
|
|
|
/**
|
|
* Find Option value that matches a possibly string value.
|
|
*
|
|
* Translate possible string values to `OptionType` objects, fallback to value
|
|
* itself if cannot be found in the options list.
|
|
*
|
|
* Always returns an array.
|
|
*/
|
|
export function findValue<OptionType extends OptionTypeBase>(
|
|
value: ValueType<OptionType> | string,
|
|
options: GroupedOptionsType<OptionType> | OptionsType<OptionType> = [],
|
|
valueKey = 'value',
|
|
): OptionType[] {
|
|
if (value === null || value === undefined || value === '') {
|
|
return [];
|
|
}
|
|
const isGroup = Array.isArray((options[0] || {}).options);
|
|
const flatOptions = isGroup
|
|
? (options as GroupedOptionsType<OptionType>).flatMap(x => x.options || [])
|
|
: (options as OptionsType<OptionType>);
|
|
|
|
const find = (val: OptionType) => {
|
|
const realVal = (value || {}).hasOwnProperty(valueKey)
|
|
? val[valueKey]
|
|
: val;
|
|
return (
|
|
flatOptions.find(x => x === realVal || x[valueKey] === realVal) || val
|
|
);
|
|
};
|
|
|
|
// If value is a single string, must return an Array so `cleanValue` won't be
|
|
// empty: https://github.com/JedWatson/react-select/blob/32ad5c040bdd96cd1ca71010c2558842d684629c/packages/react-select/src/utils.js#L64
|
|
return (Array.isArray(value) ? value : [value]).map(find);
|
|
}
|
|
|
|
export function hasOption(search: string, options: AntdOptionsType) {
|
|
const searchOption = search.trim().toLowerCase();
|
|
return options.find(opt => {
|
|
const { label, value } = opt;
|
|
const labelText = String(label);
|
|
const valueText = String(value);
|
|
return (
|
|
valueText.toLowerCase() === searchOption ||
|
|
labelText.toLowerCase() === searchOption
|
|
);
|
|
});
|
|
}
|