import { css } from '@emotion/css'; import { FC, forwardRef } from 'react'; import { GroupBase, OptionsOrGroups, createFilter } from 'react-select'; import { SelectableValue } from '@grafana/data'; import { Field, Select, useStyles2 } from '@grafana/ui'; export interface AlertLabelDropdownProps { onChange: (newValue: SelectableValue) => void; onOpenMenu?: () => void; options: SelectableValue[]; defaultValue?: SelectableValue; type: 'key' | 'value'; } const _customFilter = createFilter({ ignoreCase: false }); function customFilter(opt: SelectableValue, searchQuery: string) { return _customFilter( { label: opt.label ?? '', value: opt.value ?? '', data: {}, }, searchQuery ); } const handleIsValidNewOption = ( inputValue: string, _: SelectableValue | null, options: OptionsOrGroups, GroupBase>> ) => { const exactValueExists = options.some((el) => el.label === inputValue); const valueIsNotEmpty = inputValue.trim().length; return !Boolean(exactValueExists) && Boolean(valueIsNotEmpty); }; const AlertLabelDropdown: FC = forwardRef( function LabelPicker({ onChange, options, defaultValue, type, onOpenMenu = () => {} }, ref) { const styles = useStyles2(getStyles); return (
placeholder={`Choose ${type}`} width={29} className="ds-picker select-container" backspaceRemovesValue={false} onChange={onChange} onOpenMenu={onOpenMenu} filterOption={customFilter} isValidNewOption={handleIsValidNewOption} options={options} maxMenuHeight={500} noOptionsMessage="No labels found" defaultValue={defaultValue} allowCustomValue />
); } ); const getStyles = () => ({ resetMargin: css({ marginBottom: 0 }), }); export default AlertLabelDropdown;