UNPKG

5 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 _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
10var React = _interopRequireWildcard(require("react"));
11var _pickAttrs = _interopRequireDefault(require("rc-util/lib/pickAttrs"));
12var _Input = _interopRequireDefault(require("./Input"));
13var _commonUtil = require("../utils/commonUtil");
14function _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); }
15function _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; }
16var SingleSelector = function SingleSelector(props) {
17 var inputElement = props.inputElement,
18 prefixCls = props.prefixCls,
19 id = props.id,
20 inputRef = props.inputRef,
21 disabled = props.disabled,
22 autoFocus = props.autoFocus,
23 autoComplete = props.autoComplete,
24 activeDescendantId = props.activeDescendantId,
25 mode = props.mode,
26 open = props.open,
27 values = props.values,
28 placeholder = props.placeholder,
29 tabIndex = props.tabIndex,
30 showSearch = props.showSearch,
31 searchValue = props.searchValue,
32 activeValue = props.activeValue,
33 maxLength = props.maxLength,
34 onInputKeyDown = props.onInputKeyDown,
35 onInputMouseDown = props.onInputMouseDown,
36 onInputChange = props.onInputChange,
37 onInputPaste = props.onInputPaste,
38 onInputCompositionStart = props.onInputCompositionStart,
39 onInputCompositionEnd = props.onInputCompositionEnd,
40 title = props.title;
41 var _React$useState = React.useState(false),
42 _React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
43 inputChanged = _React$useState2[0],
44 setInputChanged = _React$useState2[1];
45 var combobox = mode === 'combobox';
46 var inputEditable = combobox || showSearch;
47 var item = values[0];
48 var inputValue = searchValue || '';
49 if (combobox && activeValue && !inputChanged) {
50 inputValue = activeValue;
51 }
52 React.useEffect(function () {
53 if (combobox) {
54 setInputChanged(false);
55 }
56 }, [combobox, activeValue]);
57
58 // Not show text when closed expect combobox mode
59 var hasTextInput = mode !== 'combobox' && !open && !showSearch ? false : !!inputValue;
60
61 // Get title of selection item
62 var selectionTitle = title === undefined ? (0, _commonUtil.getTitle)(item) : title;
63 var placeholderNode = React.useMemo(function () {
64 if (item) {
65 return null;
66 }
67 return /*#__PURE__*/React.createElement("span", {
68 className: "".concat(prefixCls, "-selection-placeholder"),
69 style: hasTextInput ? {
70 visibility: 'hidden'
71 } : undefined
72 }, placeholder);
73 }, [item, hasTextInput, placeholder, prefixCls]);
74 return /*#__PURE__*/React.createElement("span", {
75 className: "".concat(prefixCls, "-selection-wrap")
76 }, /*#__PURE__*/React.createElement("span", {
77 className: "".concat(prefixCls, "-selection-search")
78 }, /*#__PURE__*/React.createElement(_Input.default, {
79 ref: inputRef,
80 prefixCls: prefixCls,
81 id: id,
82 open: open,
83 inputElement: inputElement,
84 disabled: disabled,
85 autoFocus: autoFocus,
86 autoComplete: autoComplete,
87 editable: inputEditable,
88 activeDescendantId: activeDescendantId,
89 value: inputValue,
90 onKeyDown: onInputKeyDown,
91 onMouseDown: onInputMouseDown,
92 onChange: function onChange(e) {
93 setInputChanged(true);
94 onInputChange(e);
95 },
96 onPaste: onInputPaste,
97 onCompositionStart: onInputCompositionStart,
98 onCompositionEnd: onInputCompositionEnd,
99 tabIndex: tabIndex,
100 attrs: (0, _pickAttrs.default)(props, true),
101 maxLength: combobox ? maxLength : undefined
102 })), !combobox && item ? /*#__PURE__*/React.createElement("span", {
103 className: "".concat(prefixCls, "-selection-item"),
104 title: selectionTitle
105 // 当 Select 已经选中选项时,还需 selection 隐藏但留在原地占位
106 // https://github.com/ant-design/ant-design/issues/27688
107 // https://github.com/ant-design/ant-design/issues/41530
108 ,
109 style: hasTextInput ? {
110 visibility: 'hidden'
111 } : undefined
112 }, item.label) : null, placeholderNode);
113};
114var _default = exports.default = SingleSelector;
\No newline at end of file