MultipleSelector.js
6.84 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
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
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import * as React from 'react';
import { useState } from 'react';
import classNames from 'classnames';
import pickAttrs from "rc-util/es/pickAttrs";
import { CSSMotionList } from 'rc-motion';
import TransBtn from '../TransBtn';
import Input from './Input';
import useLayoutEffect from '../hooks/useLayoutEffect';
var REST_TAG_KEY = '__RC_SELECT_MAX_REST_COUNT__';
var SelectSelector = function SelectSelector(props) {
var id = props.id,
prefixCls = props.prefixCls,
values = props.values,
open = props.open,
searchValue = props.searchValue,
inputRef = props.inputRef,
placeholder = props.placeholder,
disabled = props.disabled,
mode = props.mode,
showSearch = props.showSearch,
autoFocus = props.autoFocus,
autoComplete = props.autoComplete,
accessibilityIndex = props.accessibilityIndex,
tabIndex = props.tabIndex,
removeIcon = props.removeIcon,
choiceTransitionName = props.choiceTransitionName,
maxTagCount = props.maxTagCount,
maxTagTextLength = props.maxTagTextLength,
_props$maxTagPlacehol = props.maxTagPlaceholder,
maxTagPlaceholder = _props$maxTagPlacehol === void 0 ? function (omittedValues) {
return "+ ".concat(omittedValues.length, " ...");
} : _props$maxTagPlacehol,
tagRender = props.tagRender,
onSelect = props.onSelect,
onInputChange = props.onInputChange,
onInputPaste = props.onInputPaste,
onInputKeyDown = props.onInputKeyDown,
onInputMouseDown = props.onInputMouseDown,
onInputCompositionStart = props.onInputCompositionStart,
onInputCompositionEnd = props.onInputCompositionEnd;
var _useState = useState(false),
_useState2 = _slicedToArray(_useState, 2),
motionAppear = _useState2[0],
setMotionAppear = _useState2[1];
var measureRef = React.useRef(null);
var _useState3 = useState(0),
_useState4 = _slicedToArray(_useState3, 2),
inputWidth = _useState4[0],
setInputWidth = _useState4[1];
var _useState5 = useState(false),
_useState6 = _slicedToArray(_useState5, 2),
focused = _useState6[0],
setFocused = _useState6[1]; // ===================== Motion ======================
React.useEffect(function () {
setMotionAppear(true);
}, []); // ===================== Search ======================
var inputValue = open || mode === 'tags' ? searchValue : '';
var inputEditable = mode === 'tags' || showSearch && (open || focused); // We measure width and set to the input immediately
useLayoutEffect(function () {
setInputWidth(measureRef.current.scrollWidth);
}, [inputValue]); // ==================== Selection ====================
var displayValues = values; // Cut by `maxTagCount`
var restCount;
if (typeof maxTagCount === 'number') {
restCount = values.length - maxTagCount;
displayValues = values.slice(0, maxTagCount);
} // Update by `maxTagTextLength`
if (typeof maxTagTextLength === 'number') {
displayValues = displayValues.map(function (_ref) {
var label = _ref.label,
rest = _objectWithoutProperties(_ref, ["label"]);
var displayLabel = label;
if (typeof label === 'string' || typeof label === 'number') {
var strLabel = String(displayLabel);
if (strLabel.length > maxTagTextLength) {
displayLabel = "".concat(strLabel.slice(0, maxTagTextLength), "...");
}
}
return _objectSpread(_objectSpread({}, rest), {}, {
label: displayLabel
});
});
} // Fill rest
if (restCount > 0) {
displayValues.push({
key: REST_TAG_KEY,
label: typeof maxTagPlaceholder === 'function' ? maxTagPlaceholder(values.slice(maxTagCount)) : maxTagPlaceholder
});
}
var selectionNode = React.createElement(CSSMotionList, {
component: false,
keys: displayValues,
motionName: choiceTransitionName,
motionAppear: motionAppear
}, function (_ref2) {
var key = _ref2.key,
label = _ref2.label,
value = _ref2.value,
itemDisabled = _ref2.disabled,
className = _ref2.className,
style = _ref2.style;
var mergedKey = key || value;
var closable = !disabled && key !== REST_TAG_KEY && !itemDisabled;
var onMouseDown = function onMouseDown(event) {
event.preventDefault();
event.stopPropagation();
};
var onClose = function onClose(event) {
if (event) event.stopPropagation();
onSelect(value, {
selected: false
});
};
return typeof tagRender === 'function' ? React.createElement("span", {
key: mergedKey,
onMouseDown: onMouseDown,
className: className,
style: style
}, tagRender({
label: label,
value: value,
disabled: itemDisabled,
closable: closable,
onClose: onClose
})) : React.createElement("span", {
key: mergedKey,
className: classNames(className, "".concat(prefixCls, "-selection-item"), _defineProperty({}, "".concat(prefixCls, "-selection-item-disabled"), itemDisabled)),
style: style
}, React.createElement("span", {
className: "".concat(prefixCls, "-selection-item-content")
}, label), closable && React.createElement(TransBtn, {
className: "".concat(prefixCls, "-selection-item-remove"),
onMouseDown: onMouseDown,
onClick: onClose,
customizeIcon: removeIcon
}, "\xD7"));
});
return React.createElement(React.Fragment, null, selectionNode, React.createElement("span", {
className: "".concat(prefixCls, "-selection-search"),
style: {
width: inputWidth
},
onFocus: function onFocus() {
setFocused(true);
},
onBlur: function onBlur() {
setFocused(false);
}
}, React.createElement(Input, {
ref: inputRef,
open: open,
prefixCls: prefixCls,
id: id,
inputElement: null,
disabled: disabled,
autoFocus: autoFocus,
autoComplete: autoComplete,
editable: inputEditable,
accessibilityIndex: accessibilityIndex,
value: inputValue,
onKeyDown: onInputKeyDown,
onMouseDown: onInputMouseDown,
onChange: onInputChange,
onPaste: onInputPaste,
onCompositionStart: onInputCompositionStart,
onCompositionEnd: onInputCompositionEnd,
tabIndex: tabIndex,
attrs: pickAttrs(props, true)
}), React.createElement("span", {
ref: measureRef,
className: "".concat(prefixCls, "-selection-search-mirror"),
"aria-hidden": true
}, inputValue, "\xA0")), !values.length && !inputValue && React.createElement("span", {
className: "".concat(prefixCls, "-selection-placeholder")
}, placeholder));
};
export default SelectSelector;