index.js
5.2 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
/**
* 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 { useRef } from 'react';
import KeyCode from "rc-util/es/KeyCode";
import MultipleSelector from './MultipleSelector';
import SingleSelector from './SingleSelector';
import useLock from '../hooks/useLock';
var Selector = function Selector(props, ref) {
var inputRef = useRef(null);
var compositionStatusRef = useRef(false);
var prefixCls = props.prefixCls,
multiple = props.multiple,
open = props.open,
mode = props.mode,
showSearch = props.showSearch,
tokenWithEnter = props.tokenWithEnter,
onSearch = props.onSearch,
onSearchSubmit = props.onSearchSubmit,
onToggleOpen = props.onToggleOpen,
onInputKeyDown = props.onInputKeyDown,
domRef = props.domRef; // ======================= Ref =======================
React.useImperativeHandle(ref, function () {
return {
focus: function focus() {
inputRef.current.focus();
},
blur: function blur() {
inputRef.current.blur();
}
};
}); // ====================== Input ======================
var _useLock = useLock(0),
_useLock2 = _slicedToArray(_useLock, 2),
getInputMouseDown = _useLock2[0],
setInputMouseDown = _useLock2[1];
var onInternalInputKeyDown = function onInternalInputKeyDown(event) {
var which = event.which;
if (which === KeyCode.UP || which === KeyCode.DOWN) {
event.preventDefault();
}
if (onInputKeyDown) {
onInputKeyDown(event);
}
if (which === KeyCode.ENTER && mode === 'tags' && !compositionStatusRef.current && !open) {
// When menu isn't open, OptionList won't trigger a value change
// So when enter is pressed, the tag's input value should be emitted here to let selector know
onSearchSubmit(event.target.value);
}
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.
*/
var onInternalInputMouseDown = function onInternalInputMouseDown() {
setInputMouseDown(true);
}; // When paste come, ignore next onChange
var pastedTextRef = useRef(null);
var triggerOnSearch = function triggerOnSearch(value) {
if (onSearch(value, true, compositionStatusRef.current) !== false) {
onToggleOpen(true);
}
};
var onInputCompositionStart = function onInputCompositionStart() {
compositionStatusRef.current = true;
};
var onInputCompositionEnd = function onInputCompositionEnd() {
compositionStatusRef.current = false;
};
var onInputChange = function onInputChange(event) {
var value = event.target.value; // Pasted text should replace back to origin content
if (tokenWithEnter && pastedTextRef.current && /[\r\n]/.test(pastedTextRef.current)) {
// CRLF will be treated as a single space for input element
var replacedText = pastedTextRef.current.replace(/\r\n/g, ' ').replace(/[\r\n]/g, ' ');
value = value.replace(replacedText, pastedTextRef.current);
}
pastedTextRef.current = null;
triggerOnSearch(value);
};
var onInputPaste = function onInputPaste(e) {
var clipboardData = e.clipboardData;
var value = clipboardData.getData('text');
pastedTextRef.current = value;
};
var onClick = function onClick(_ref) {
var target = _ref.target;
if (target !== inputRef.current) {
// Should focus input if click the selector
var isIE = document.body.style.msTouchAction !== undefined;
if (isIE) {
setTimeout(function () {
inputRef.current.focus();
});
} else {
inputRef.current.focus();
}
}
};
var onMouseDown = function onMouseDown(event) {
var inputMouseDown = getInputMouseDown();
if (event.target !== inputRef.current && !inputMouseDown) {
event.preventDefault();
}
if (mode !== 'combobox' && (!showSearch || !inputMouseDown) || !open) {
if (open) {
onSearch('', true, false);
}
onToggleOpen();
}
}; // ================= Inner Selector ==================
var sharedProps = {
inputRef: inputRef,
onInputKeyDown: onInternalInputKeyDown,
onInputMouseDown: onInternalInputMouseDown,
onInputChange: onInputChange,
onInputPaste: onInputPaste,
onInputCompositionStart: onInputCompositionStart,
onInputCompositionEnd: onInputCompositionEnd
};
var selectNode = multiple ? React.createElement(MultipleSelector, Object.assign({}, props, sharedProps)) : React.createElement(SingleSelector, Object.assign({}, props, sharedProps));
return React.createElement("div", {
ref: domRef,
className: "".concat(prefixCls, "-selector"),
onClick: onClick,
onMouseDown: onMouseDown
}, selectNode);
};
var ForwardSelector = React.forwardRef(Selector);
ForwardSelector.displayName = 'Selector';
export default ForwardSelector;