-
Notifications
You must be signed in to change notification settings - Fork 449
/
index.tsx
211 lines (180 loc) · 5 KB
/
index.tsx
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
* Cursor rule:
* 1. Only `showSearch` enabled
* 2. Only `open` is `true`
* 3. When typing, set `open` to `true` which hit rule of 2
*
* Accessibility:
* - https://www.w3.org/TR/wai-aria-practices/examples/combobox/aria1.1pattern/listbox-combo.html
*/
import * as React from 'react';
import KeyCode from 'rc-util/lib/KeyCode';
import MultipleSelector from './MultipleSelector';
import SingleSelector from './SingleSelector';
import { LabelValueType, RawValueType } from '../interface/generator';
import { RenderNode, Mode } from '../interface';
import useLock from '../hooks/useLock';
export interface InnerSelectorProps {
prefixCls: string;
id: string;
mode: Mode;
inputRef: React.Ref<HTMLInputElement | HTMLTextAreaElement>;
placeholder?: React.ReactNode;
disabled?: boolean;
autoFocus?: boolean;
values: LabelValueType[];
showSearch?: boolean;
searchValue: string;
accessibilityIndex: number;
open: boolean;
tabIndex?: number;
onInputKeyDown: React.KeyboardEventHandler<
HTMLInputElement | HTMLTextAreaElement
>;
onInputMouseDown: React.MouseEventHandler<
HTMLInputElement | HTMLTextAreaElement
>;
onInputChange: React.ChangeEventHandler<
HTMLInputElement | HTMLTextAreaElement
>;
}
export interface RefSelectorProps {
focus: () => void;
blur: () => void;
}
export interface SelectorProps {
id: string;
prefixCls: string;
showSearch?: boolean;
open: boolean;
/** Display in the Selector value, it's not same as `value` prop */
values: LabelValueType[];
multiple: boolean;
mode: Mode;
searchValue: string;
activeValue: string;
inputElement: JSX.Element;
autoFocus?: boolean;
accessibilityIndex: number;
tabIndex?: number;
disabled?: boolean;
placeholder?: React.ReactNode;
removeIcon?: RenderNode;
// Tags
maxTagCount?: number;
maxTagTextLength?: number;
maxTagPlaceholder?:
| React.ReactNode
| ((omittedValues: LabelValueType[]) => React.ReactNode);
// Motion
choiceTransitionName?: string;
onToggleOpen: (open?: boolean) => void;
/** `onSearch` returns go next step boolean to check if need do toggle open */
onSearch: (searchValue: string) => boolean;
onSelect: (value: RawValueType, option: { selected: boolean }) => void;
onInputKeyDown?: React.KeyboardEventHandler<
HTMLInputElement | HTMLTextAreaElement
>;
/**
* @private get real dom for trigger align.
* This may be removed after React provides replacement of `findDOMNode`
*/
domRef: React.Ref<HTMLDivElement>;
}
const Selector: React.RefForwardingComponent<
RefSelectorProps,
SelectorProps
> = (props, ref) => {
const inputRef = React.useRef<HTMLInputElement>(null);
const {
prefixCls,
multiple,
onSearch,
onToggleOpen,
onInputKeyDown,
domRef,
} = props;
// ======================= Ref =======================
React.useImperativeHandle(ref, () => ({
focus: () => {
inputRef.current.focus();
},
blur: () => {
inputRef.current.blur();
},
}));
// ====================== Input ======================
const [getInputMouseDown, setInputMouseDown] = useLock(0);
const onInternalInputKeyDown: React.KeyboardEventHandler<
HTMLInputElement
> = event => {
const { which } = event;
if (which === KeyCode.UP || which === KeyCode.DOWN) {
event.preventDefault();
}
if (onInputKeyDown) {
onInputKeyDown(event);
}
if (
![KeyCode.SHIFT, KeyCode.TAB, KeyCode.BACKSPACE, KeyCode.ESC].includes(
which,
)
) {
onToggleOpen(true);
}
};
/**
* We can not use `findDOMNode` sine it will get warning,
* have to use timer to check if is input element.
*/
const onInternalInputMouseDown: React.MouseEventHandler<
HTMLInputElement
> = () => {
setInputMouseDown(true);
};
const onInputChange = ({ target: { value } }) => {
if (onSearch(value) !== false) {
onToggleOpen(true);
}
};
// ====================== Focus ======================
// Should focus input if click the selector
const onClick = ({ target }) => {
if (target !== inputRef.current) {
inputRef.current.focus();
}
};
const onMouseDown: React.MouseEventHandler<HTMLElement> = event => {
if (event.target !== inputRef.current && !getInputMouseDown()) {
event.preventDefault();
}
onToggleOpen();
};
// ================= Inner Selector ==================
const sharedProps = {
inputRef,
onInputKeyDown: onInternalInputKeyDown,
onInputMouseDown: onInternalInputMouseDown,
onInputChange,
};
const selectNode = multiple ? (
<MultipleSelector {...props} {...sharedProps} />
) : (
<SingleSelector {...props} {...sharedProps} />
);
return (
<div
ref={domRef}
className={`${prefixCls}-selector`}
onClick={onClick}
onMouseDown={onMouseDown}
>
{selectNode}
</div>
);
};
const ForwardSelector = React.forwardRef<RefSelectorProps, SelectorProps>(
Selector,
);
ForwardSelector.displayName = 'Selector';
export default ForwardSelector;