-
Notifications
You must be signed in to change notification settings - Fork 399
Expand file tree
/
Copy pathtypes.ts
More file actions
153 lines (131 loc) · 3.91 KB
/
types.ts
File metadata and controls
153 lines (131 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import {
ChangeEvent,
ChangeEventHandler,
FocusEventHandler,
InputHTMLAttributes,
KeyboardEvent,
KeyboardEventHandler,
MouseEvent,
MouseEventHandler,
ReactNode,
RefCallback,
SyntheticEvent,
} from 'react';
import { SIZES } from './constants';
export type AllowNew =
| boolean
| ((options: Option[], state: TypeaheadPropsAndState) => boolean);
export type FilterByCallback = (
option: Option,
state: TypeaheadPropsAndState
) => boolean;
export type Id = string;
export type KeepOpen = boolean | (() => boolean);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Option = string | Record<string, any>;
export type OptionHandler = (option: Option) => void;
export type LabelKey = string | ((option: Option) => string);
export type SelectEvent<T> = MouseEvent<T> | KeyboardEvent<T>;
export type SelectHint = (
shouldSelectHint: boolean,
event: KeyboardEvent<HTMLInputElement>
) => boolean;
export type RefElement<T> = T | null;
export type Size = typeof SIZES[number];
export type TypeaheadChildren =
| ReactNode
| ((props: TypeaheadManagerChildProps) => ReactNode);
export type InputProps = Omit<InputHTMLAttributes<HTMLInputElement>, 'size'>;
export interface TypeaheadInputProps extends InputProps {
inputClassName?: string;
inputRef: RefCallback<HTMLInputElement>;
referenceElementRef: RefCallback<HTMLElement>;
}
export interface RenderTokenProps {
disabled?: boolean;
labelKey: LabelKey;
onRemove?: OptionHandler;
tabIndex?: number;
}
export type RenderToken = (
option: Option,
props: RenderTokenProps,
idx: number
) => JSX.Element;
export interface TypeaheadProps {
allowNew: AllowNew;
autoFocus: boolean;
caseSensitive: boolean;
children: TypeaheadChildren;
defaultInputValue: string;
defaultOpen: boolean;
defaultSelected: Option[];
emptyLabel?: ReactNode;
filterBy: string[] | FilterByCallback;
highlightOnlyResult: boolean;
id?: Id;
ignoreDiacritics: boolean;
inputProps?: InputProps;
keepOpen?: KeepOpen;
labelKey: LabelKey;
maxResults: number;
minLength: number;
multiple: boolean;
onBlur: FocusEventHandler<HTMLInputElement>;
onChange?: (selected: Option[]) => void;
onFocus: (event: SyntheticEvent<HTMLInputElement>) => void;
onInputChange: (text: string, event: ChangeEvent<HTMLInputElement>) => void;
onKeyDown: KeyboardEventHandler<HTMLInputElement>;
onMenuToggle: (isOpen: boolean) => void;
onPaginate: (event: SelectEvent<HTMLElement>, shownResults: number) => void;
open?: boolean;
options: Option[];
paginate: boolean;
selected?: Option[];
selectHint?: SelectHint;
}
export interface TypeaheadState {
activeIndex: number;
activeItem?: Option;
initialItem?: Option;
isFocused: boolean;
selected: Option[];
showMenu: boolean;
shownResults: number;
text: string;
}
export type TypeaheadPropsAndState = Omit<TypeaheadProps, 'onChange'> &
TypeaheadState;
export interface TypeaheadManagerChildProps {
activeIndex: number;
getInputProps: (props?: InputProps) => TypeaheadInputProps;
hideMenu: () => void;
isMenuShown: boolean;
labelKey: LabelKey;
onClear: () => void;
onHide: () => void;
onRemove: OptionHandler;
results: Option[];
selected: Option[];
text: string;
toggleMenu: () => void;
}
export interface TypeaheadManagerProps extends TypeaheadPropsAndState {
hideMenu: () => void;
inputNode: RefElement<HTMLInputElement>;
inputRef: RefCallback<HTMLInputElement>;
isMenuShown: boolean;
onActiveItemChange: OptionHandler;
onAdd: OptionHandler;
onChange: ChangeEventHandler<HTMLInputElement>;
onClear: () => void;
onClick: MouseEventHandler<HTMLInputElement>;
onHide: () => void;
onInitialItemChange: (option?: Option) => void;
onMenuItemClick: (option: Option, event: SelectEvent<HTMLElement>) => void;
onRemove: OptionHandler;
placeholder?: string;
results: Option[];
setItem: (item: Option, position: number) => void;
toggleMenu: () => void;
}