import { AnyFunction, ChangeHandler, Collection, CollectionBroker, CollectionProxy, FormFieldProps, Primitive, ValidationStatus, } from 'onekijs-framework'; import React, { FC } from 'react'; import { TshirtSize } from '../../styles/typings'; import { DropdownWidthModifier } from '../dropdown/typings'; import { FieldLayoutProps } from '../field/typings'; import { CollectionListProps, ListConfig, ListItem, ListItemAdaptee, ListItemHandler, ListItemProps, ListNotFoundProps, ListState, UseListOptions, } from '../list/typings'; import { TreeController, TreeItem, TreeProps, TreeState } from '../tree/typings'; import SelectService from './SelectService'; export type ArraySelectProps = SelectItem> = SelectConfig & { adapter?: SelectItemAdapter; dataSource: T[] | [T, string][] | string; fetchOnce?: boolean; }; export type ArrayTreeSelectProps = TreeSelectItem> = Omit< ArraySelectProps, 'dataSource' > & { dataSource: T[] | string; }; export type AutoCompleteAdapter = (value: string | null) => T | null | Promise; export type AutoCompleteProps< T = Primitive, I extends SelectItem = SelectItem, S extends SelectState = SelectState, C extends SelectController = SelectController, > = Omit, 'mode' | 'autoCompleteSearch' | 'validateValue' | 'clickable' | 'searchable'> & { type?: 'text' | 'number'; }; export type ControllerSelectProps< T = any, I extends SelectItem = SelectItem, S extends SelectState = SelectState, C extends SelectController = SelectController, > = SelectConfig & { controller: CollectionProxy; }; export type ControllerTreeSelectProps< T = any, I extends TreeSelectItem = TreeSelectItem, S extends TreeSelectState = TreeSelectState, C extends TreeSelectController = TreeSelectController, > = ControllerSelectProps & TreeProps; export type FormAutoCompleteProps = SelectItem> = AutoCompleteProps & FormFieldProps & FieldLayoutProps & { defaultValue?: T | T[] | null; FieldComponent?: React.FC>; }; export type FormSelectProps = SelectItem> = SelectProps & FormFieldProps & FieldLayoutProps & { defaultValue?: T | T[] | null; FieldComponent?: React.FC>; }; export type SelectController< T = any, I extends SelectItem = SelectItem, S extends SelectState = SelectState, > = Collection & { config?: SelectConfig; check: () => void; defaultValue: T | T[] | null | undefined; setDefaultValue: (defaultValue: T | T[] | null | undefined) => void; setDefaultValueLoading: (loading: boolean) => void; setItemWidth: (item: I, width: number) => void; setInputValue: (value: string | null) => void; }; export interface SelectIconProps { open: boolean; loading: boolean; fetching: boolean; onClick: AnyFunction; } export interface SelectInputProps = SelectItem> extends Omit, 'value' | 'onChange'> { IconComponent?: FC | null; focus: boolean; value?: string; tokens?: I[]; open: boolean; loading: boolean; fetching: boolean; setOpen: (open: boolean) => void; onChange: (nextValue: string | null) => void; multiple: boolean; onRemove: SelectOptionHandler; style?: React.CSSProperties; nullable?: boolean; searchable: boolean; clickable: boolean; minChars: number; autoCompleteSearch: boolean; maxDisplayTokens?: number; } export type SelectGroup = | string | { text: string; [k: string]: any; }; export type SelectItem = ListItem & { group?: SelectGroup; }; export type SelectItemAdaptee = ListItemAdaptee & { group?: SelectGroup; }; export type SelectItemAdapter = (data: T) => SelectItemAdaptee; // export type SelectInternalProps = Omit & { // collection: Collection; // }; export type SelectNotFoundProps = ListNotFoundProps; export type SelectOptionHandler = SelectItem> = ListItemHandler; export type SelectOptionSelectionHandler = SelectItem> = ( item: I, index: number, close?: boolean, ) => void; export type SelectOptionProps = SelectItem> = ListItemProps & { multiple?: boolean; }; export type SelectProps< T = any, I extends SelectItem = SelectItem, S extends SelectState = SelectState, C extends SelectController = SelectController, > = SelectConfig & { adapter?: SelectItemAdapter; autoCompleteAdapter?: AutoCompleteAdapter; controller?: CollectionProxy; dataSource?: T[] | [T, string][] | string; fetchOnce?: boolean; }; export type SelectBroker< T = any, I extends SelectItem = SelectItem, S extends SelectState = SelectState, C extends SelectController = SelectController, > = CollectionBroker & { setDefaultValue: (defaultValue: T | T[] | null | undefined, subscriberId?: string) => void; }; export type SelectConfig = SelectItem> = Omit, 'ItemComponent'> & { attachDropdownToBody?: boolean; InputComponent?: React.ForwardRefExoticComponent< React.PropsWithoutRef> & React.RefAttributes >; IconComponent?: FC | null; NotFoundComponent?: FC | null; placeholder?: string; value?: T | T[] | null; onChange?: ChangeHandler; onFocus?: () => void; onBlur?: () => void; autoFocus?: boolean; className?: string; multiple?: boolean; status?: ValidationStatus; name?: string; size?: TshirtSize; nullable?: boolean; minChars?: number; openOnFocus?: boolean; clickable?: boolean; searchable?: boolean; dropdownWidthModifier?: DropdownWidthModifier; open?: boolean; OptionComponent?: FC>; OptionGroupComponent?: FC>; OptionContentComponent?: FC>; OptionLoadingComponent?: FC; MultiOptionsComponent?: FC>; animationMs?: number; disabled?: boolean; defaultValue?: T | T[] | null; defaultValueLoading?: boolean; required?: boolean; sameWidth?: boolean; ListComponent?: React.FC>; autoCompleteSearch?: boolean; autoCompleteAdapter?: AutoCompleteAdapter; maxDisplayTokens?: number; validateValue?: boolean; mode?: 'default' | 'autocomplete'; }; export type SelectListComponentProps = SelectItem> = CollectionListProps & { optionsRef: React.RefObject; }; export type SelectState = SelectItem> = ListState & { invalidItems?: I[]; validDefaultValue?: T | T[] | null; defaultValue?: T | T[] | null; defaultValueLoading?: boolean; width?: number; }; export interface SelectTokensProps = SelectItem> { tokens?: I[]; onRemove: SelectOptionHandler; maxDisplayTokens?: number; } export interface SelectTokenProps = SelectItem> { token: I; onRemove: SelectOptionHandler; index: number; } export type TreeSelectController< T, I extends TreeSelectItem = TreeSelectItem, S extends TreeSelectState = TreeSelectState, > = SelectController & TreeController; export type TreeSelectItem = SelectItem & TreeItem; export type TreeSelectProps< T = any, I extends TreeSelectItem = TreeSelectItem, S extends TreeSelectState = TreeSelectState, C extends TreeSelectController = TreeSelectController, > = SelectProps & TreeProps; export type TreeSelectState = TreeSelectItem> = SelectState & TreeState; export type UseSelectOptions = SelectItem> = UseListOptions; export type UseSelectController = SelectItem> = ( dataSource: T[] | [T, string][] | string | undefined, options?: UseSelectOptions, ) => CollectionProxy, SelectService>>;