New Select: Extend creatable select api (#21869)

This commit is contained in:
Dominik Prokop
2020-01-31 13:47:34 +01:00
committed by GitHub
parent 0390b5601e
commit 249ae05281
2 changed files with 11 additions and 2 deletions

View File

@@ -247,17 +247,23 @@ export const customizedControl = () => {
export const customValueCreation = () => {
const [value, setValue] = useState<SelectableValue<string>>();
const [customOptions, setCustomOptions] = useState<Array<SelectableValue<string>>>([]);
const options = generateOptions();
return (
<>
<Select
options={generateOptions()}
options={[...options, ...customOptions]}
value={value}
onChange={v => {
setValue(v);
}}
size="md"
allowCustomValue
onCreateOption={v => {
const customValue: SelectableValue<string> = { value: kebabCase(v), label: v };
setCustomOptions([...customOptions, customValue]);
setValue(customValue);
}}
{...getDynamicProps()}
/>
</>

View File

@@ -35,6 +35,7 @@ export interface SelectCommonProps<T> {
value?: SelectValue<T>;
getOptionLabel?: (item: SelectableValue<T>) => string;
getOptionValue?: (item: SelectableValue<T>) => string;
onCreateOption?: (value: string) => void;
onChange: (value: SelectableValue<T>) => {} | void;
onInputChange?: (label: string) => void;
onKeyDown?: (event: React.KeyboardEvent) => void;
@@ -140,6 +141,7 @@ export function SelectBase<T>({
defaultValue,
inputValue,
onInputChange,
onCreateOption,
options = [],
onChange,
onBlur,
@@ -242,6 +244,7 @@ export function SelectBase<T>({
if (allowCustomValue) {
ReactSelectComponent = Creatable;
creatableProps.formatCreateLabel = formatCreateLabel ?? ((input: string) => `Create: ${input}`);
creatableProps.onCreateOption = onCreateOption;
}
// Instead of having AsyncSelect, as a separate component we render ReactAsyncSelect