UNPKG

8.69 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4var _typeof = require("@babel/runtime/helpers/typeof");
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.default = void 0;
9var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
10var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
11var _react = _interopRequireWildcard(require("react"));
12var React = _react;
13var _classnames = _interopRequireDefault(require("classnames"));
14var _pickAttrs = _interopRequireDefault(require("rc-util/lib/pickAttrs"));
15var _rcOverflow = _interopRequireDefault(require("rc-overflow"));
16var _TransBtn = _interopRequireDefault(require("../TransBtn"));
17var _Input = _interopRequireDefault(require("./Input"));
18var _useLayoutEffect = _interopRequireDefault(require("../hooks/useLayoutEffect"));
19var _commonUtil = require("../utils/commonUtil");
20function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
21function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
22function itemKey(value) {
23 var _value$key;
24 return (_value$key = value.key) !== null && _value$key !== void 0 ? _value$key : value.value;
25}
26var onPreventMouseDown = function onPreventMouseDown(event) {
27 event.preventDefault();
28 event.stopPropagation();
29};
30var SelectSelector = function SelectSelector(props) {
31 var id = props.id,
32 prefixCls = props.prefixCls,
33 values = props.values,
34 open = props.open,
35 searchValue = props.searchValue,
36 autoClearSearchValue = props.autoClearSearchValue,
37 inputRef = props.inputRef,
38 placeholder = props.placeholder,
39 disabled = props.disabled,
40 mode = props.mode,
41 showSearch = props.showSearch,
42 autoFocus = props.autoFocus,
43 autoComplete = props.autoComplete,
44 activeDescendantId = props.activeDescendantId,
45 tabIndex = props.tabIndex,
46 removeIcon = props.removeIcon,
47 maxTagCount = props.maxTagCount,
48 maxTagTextLength = props.maxTagTextLength,
49 _props$maxTagPlacehol = props.maxTagPlaceholder,
50 maxTagPlaceholder = _props$maxTagPlacehol === void 0 ? function (omittedValues) {
51 return "+ ".concat(omittedValues.length, " ...");
52 } : _props$maxTagPlacehol,
53 tagRender = props.tagRender,
54 onToggleOpen = props.onToggleOpen,
55 onRemove = props.onRemove,
56 onInputChange = props.onInputChange,
57 onInputPaste = props.onInputPaste,
58 onInputKeyDown = props.onInputKeyDown,
59 onInputMouseDown = props.onInputMouseDown,
60 onInputCompositionStart = props.onInputCompositionStart,
61 onInputCompositionEnd = props.onInputCompositionEnd;
62 var measureRef = React.useRef(null);
63 var _useState = (0, _react.useState)(0),
64 _useState2 = (0, _slicedToArray2.default)(_useState, 2),
65 inputWidth = _useState2[0],
66 setInputWidth = _useState2[1];
67 var _useState3 = (0, _react.useState)(false),
68 _useState4 = (0, _slicedToArray2.default)(_useState3, 2),
69 focused = _useState4[0],
70 setFocused = _useState4[1];
71 var selectionPrefixCls = "".concat(prefixCls, "-selection");
72
73 // ===================== Search ======================
74 var inputValue = open || mode === 'multiple' && autoClearSearchValue === false || mode === 'tags' ? searchValue : '';
75 var inputEditable = mode === 'tags' || mode === 'multiple' && autoClearSearchValue === false || showSearch && (open || focused);
76
77 // We measure width and set to the input immediately
78 (0, _useLayoutEffect.default)(function () {
79 setInputWidth(measureRef.current.scrollWidth);
80 }, [inputValue]);
81
82 // ===================== Render ======================
83 // >>> Render Selector Node. Includes Item & Rest
84 var defaultRenderSelector = function defaultRenderSelector(item, content, itemDisabled, closable, onClose) {
85 return /*#__PURE__*/React.createElement("span", {
86 title: (0, _commonUtil.getTitle)(item),
87 className: (0, _classnames.default)("".concat(selectionPrefixCls, "-item"), (0, _defineProperty2.default)({}, "".concat(selectionPrefixCls, "-item-disabled"), itemDisabled))
88 }, /*#__PURE__*/React.createElement("span", {
89 className: "".concat(selectionPrefixCls, "-item-content")
90 }, content), closable && /*#__PURE__*/React.createElement(_TransBtn.default, {
91 className: "".concat(selectionPrefixCls, "-item-remove"),
92 onMouseDown: onPreventMouseDown,
93 onClick: onClose,
94 customizeIcon: removeIcon
95 }, "\xD7"));
96 };
97 var customizeRenderSelector = function customizeRenderSelector(value, content, itemDisabled, closable, onClose, isMaxTag) {
98 var onMouseDown = function onMouseDown(e) {
99 onPreventMouseDown(e);
100 onToggleOpen(!open);
101 };
102 return /*#__PURE__*/React.createElement("span", {
103 onMouseDown: onMouseDown
104 }, tagRender({
105 label: content,
106 value: value,
107 disabled: itemDisabled,
108 closable: closable,
109 onClose: onClose,
110 isMaxTag: !!isMaxTag
111 }));
112 };
113 var renderItem = function renderItem(valueItem) {
114 var itemDisabled = valueItem.disabled,
115 label = valueItem.label,
116 value = valueItem.value;
117 var closable = !disabled && !itemDisabled;
118 var displayLabel = label;
119 if (typeof maxTagTextLength === 'number') {
120 if (typeof label === 'string' || typeof label === 'number') {
121 var strLabel = String(displayLabel);
122 if (strLabel.length > maxTagTextLength) {
123 displayLabel = "".concat(strLabel.slice(0, maxTagTextLength), "...");
124 }
125 }
126 }
127 var onClose = function onClose(event) {
128 if (event) {
129 event.stopPropagation();
130 }
131 onRemove(valueItem);
132 };
133 return typeof tagRender === 'function' ? customizeRenderSelector(value, displayLabel, itemDisabled, closable, onClose) : defaultRenderSelector(valueItem, displayLabel, itemDisabled, closable, onClose);
134 };
135 var renderRest = function renderRest(omittedValues) {
136 var content = typeof maxTagPlaceholder === 'function' ? maxTagPlaceholder(omittedValues) : maxTagPlaceholder;
137 return typeof tagRender === 'function' ? customizeRenderSelector(undefined, content, false, false, undefined, true) : defaultRenderSelector({
138 title: content
139 }, content, false);
140 };
141
142 // >>> Input Node
143 var inputNode = /*#__PURE__*/React.createElement("div", {
144 className: "".concat(selectionPrefixCls, "-search"),
145 style: {
146 width: inputWidth
147 },
148 onFocus: function onFocus() {
149 setFocused(true);
150 },
151 onBlur: function onBlur() {
152 setFocused(false);
153 }
154 }, /*#__PURE__*/React.createElement(_Input.default, {
155 ref: inputRef,
156 open: open,
157 prefixCls: prefixCls,
158 id: id,
159 inputElement: null,
160 disabled: disabled,
161 autoFocus: autoFocus,
162 autoComplete: autoComplete,
163 editable: inputEditable,
164 activeDescendantId: activeDescendantId,
165 value: inputValue,
166 onKeyDown: onInputKeyDown,
167 onMouseDown: onInputMouseDown,
168 onChange: onInputChange,
169 onPaste: onInputPaste,
170 onCompositionStart: onInputCompositionStart,
171 onCompositionEnd: onInputCompositionEnd,
172 tabIndex: tabIndex,
173 attrs: (0, _pickAttrs.default)(props, true)
174 }), /*#__PURE__*/React.createElement("span", {
175 ref: measureRef,
176 className: "".concat(selectionPrefixCls, "-search-mirror"),
177 "aria-hidden": true
178 }, inputValue, "\xA0"));
179
180 // >>> Selections
181 var selectionNode = /*#__PURE__*/React.createElement(_rcOverflow.default, {
182 prefixCls: "".concat(selectionPrefixCls, "-overflow"),
183 data: values,
184 renderItem: renderItem,
185 renderRest: renderRest,
186 suffix: inputNode,
187 itemKey: itemKey,
188 maxCount: maxTagCount
189 });
190 return /*#__PURE__*/React.createElement("span", {
191 className: "".concat(selectionPrefixCls, "-wrap")
192 }, selectionNode, !values.length && !inputValue && /*#__PURE__*/React.createElement("span", {
193 className: "".concat(selectionPrefixCls, "-placeholder")
194 }, placeholder));
195};
196var _default = exports.default = SelectSelector;
\No newline at end of file