UNPKG

2.38 kBTypeScriptView Raw
1import { ReactNode } from 'react';
2import { PublicBaseSelectProps } from './Select';
3import { GetOptionLabel, GetOptionValue, GroupBase, Options, OptionsOrGroups } from './types';
4export interface Accessors<Option> {
5 getOptionValue: GetOptionValue<Option>;
6 getOptionLabel: GetOptionLabel<Option>;
7}
8export interface CreatableAdditionalProps<Option, Group extends GroupBase<Option>> {
9 /**
10 * Allow options to be created while the `isLoading` prop is true. Useful to
11 * prevent the "create new ..." option being displayed while async results are
12 * still being loaded.
13 */
14 allowCreateWhileLoading?: boolean;
15 /** Sets the position of the createOption element in your options list. Defaults to 'last' */
16 createOptionPosition?: 'first' | 'last';
17 /**
18 * Gets the label for the "create new ..." option in the menu. Is given the
19 * current input value.
20 */
21 formatCreateLabel?: (inputValue: string) => ReactNode;
22 /**
23 * Determines whether the "create new ..." option should be displayed based on
24 * the current input value, select value and options array.
25 */
26 isValidNewOption?: (inputValue: string, value: Options<Option>, options: OptionsOrGroups<Option, Group>, accessors: Accessors<Option>) => boolean;
27 /**
28 * Returns the data for the new option when it is created. Used to display the
29 * value, and is passed to `onChange`.
30 */
31 getNewOptionData?: (inputValue: string, optionLabel: ReactNode) => Option;
32 /**
33 * If provided, this will be called with the input value when a new option is
34 * created, and `onChange` will **not** be called. Use this when you need more
35 * control over what happens when new options are created.
36 */
37 onCreateOption?: (inputValue: string) => void;
38}
39declare type BaseCreatableProps<Option, IsMulti extends boolean, Group extends GroupBase<Option>> = PublicBaseSelectProps<Option, IsMulti, Group> & CreatableAdditionalProps<Option, Group>;
40export default function useCreatable<Option, IsMulti extends boolean, Group extends GroupBase<Option>>({ allowCreateWhileLoading, createOptionPosition, formatCreateLabel, isValidNewOption, getNewOptionData, onCreateOption, options: propsOptions, onChange: propsOnChange, ...restSelectProps }: BaseCreatableProps<Option, IsMulti, Group>): PublicBaseSelectProps<Option, IsMulti, Group>;
41export {};