MultipleSelector.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. var _typeof = require("@babel/runtime/helpers/typeof");
  4. Object.defineProperty(exports, "__esModule", {
  5. value: true
  6. });
  7. exports.default = void 0;
  8. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
  9. var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
  10. var _react = _interopRequireWildcard(require("react"));
  11. var React = _react;
  12. var _classnames = _interopRequireDefault(require("classnames"));
  13. var _pickAttrs = _interopRequireDefault(require("rc-util/lib/pickAttrs"));
  14. var _rcOverflow = _interopRequireDefault(require("rc-overflow"));
  15. var _TransBtn = _interopRequireDefault(require("../TransBtn"));
  16. var _Input = _interopRequireDefault(require("./Input"));
  17. var _useLayoutEffect = _interopRequireDefault(require("../hooks/useLayoutEffect"));
  18. var _commonUtil = require("../utils/commonUtil");
  19. function _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); }
  20. function _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; }
  21. function itemKey(value) {
  22. var _value$key;
  23. return (_value$key = value.key) !== null && _value$key !== void 0 ? _value$key : value.value;
  24. }
  25. var onPreventMouseDown = function onPreventMouseDown(event) {
  26. event.preventDefault();
  27. event.stopPropagation();
  28. };
  29. var SelectSelector = function SelectSelector(props) {
  30. var id = props.id,
  31. prefixCls = props.prefixCls,
  32. values = props.values,
  33. open = props.open,
  34. searchValue = props.searchValue,
  35. autoClearSearchValue = props.autoClearSearchValue,
  36. inputRef = props.inputRef,
  37. placeholder = props.placeholder,
  38. disabled = props.disabled,
  39. mode = props.mode,
  40. showSearch = props.showSearch,
  41. autoFocus = props.autoFocus,
  42. autoComplete = props.autoComplete,
  43. activeDescendantId = props.activeDescendantId,
  44. tabIndex = props.tabIndex,
  45. removeIcon = props.removeIcon,
  46. maxTagCount = props.maxTagCount,
  47. maxTagTextLength = props.maxTagTextLength,
  48. _props$maxTagPlacehol = props.maxTagPlaceholder,
  49. maxTagPlaceholder = _props$maxTagPlacehol === void 0 ? function (omittedValues) {
  50. return "+ ".concat(omittedValues.length, " ...");
  51. } : _props$maxTagPlacehol,
  52. tagRender = props.tagRender,
  53. onToggleOpen = props.onToggleOpen,
  54. onRemove = props.onRemove,
  55. onInputChange = props.onInputChange,
  56. onInputPaste = props.onInputPaste,
  57. onInputKeyDown = props.onInputKeyDown,
  58. onInputMouseDown = props.onInputMouseDown,
  59. onInputCompositionStart = props.onInputCompositionStart,
  60. onInputCompositionEnd = props.onInputCompositionEnd,
  61. onInputBlur = props.onInputBlur;
  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. // ===================== Search ======================
  73. var inputValue = open || mode === 'multiple' && autoClearSearchValue === false || mode === 'tags' ? searchValue : '';
  74. var inputEditable = mode === 'tags' || mode === 'multiple' && autoClearSearchValue === false || showSearch && (open || focused);
  75. // We measure width and set to the input immediately
  76. (0, _useLayoutEffect.default)(function () {
  77. setInputWidth(measureRef.current.scrollWidth);
  78. }, [inputValue]);
  79. // ===================== Render ======================
  80. // >>> Render Selector Node. Includes Item & Rest
  81. var defaultRenderSelector = function defaultRenderSelector(item, content, itemDisabled, closable, onClose) {
  82. return /*#__PURE__*/React.createElement("span", {
  83. title: (0, _commonUtil.getTitle)(item),
  84. className: (0, _classnames.default)("".concat(selectionPrefixCls, "-item"), (0, _defineProperty2.default)({}, "".concat(selectionPrefixCls, "-item-disabled"), itemDisabled))
  85. }, /*#__PURE__*/React.createElement("span", {
  86. className: "".concat(selectionPrefixCls, "-item-content")
  87. }, content), closable && /*#__PURE__*/React.createElement(_TransBtn.default, {
  88. className: "".concat(selectionPrefixCls, "-item-remove"),
  89. onMouseDown: onPreventMouseDown,
  90. onClick: onClose,
  91. customizeIcon: removeIcon
  92. }, "\xD7"));
  93. };
  94. var customizeRenderSelector = function customizeRenderSelector(value, content, itemDisabled, closable, onClose, isMaxTag) {
  95. var onMouseDown = function onMouseDown(e) {
  96. onPreventMouseDown(e);
  97. onToggleOpen(!open);
  98. };
  99. return /*#__PURE__*/React.createElement("span", {
  100. onMouseDown: onMouseDown
  101. }, tagRender({
  102. label: content,
  103. value: value,
  104. disabled: itemDisabled,
  105. closable: closable,
  106. onClose: onClose,
  107. isMaxTag: !!isMaxTag
  108. }));
  109. };
  110. var renderItem = function renderItem(valueItem) {
  111. var itemDisabled = valueItem.disabled,
  112. label = valueItem.label,
  113. value = valueItem.value;
  114. var closable = !disabled && !itemDisabled;
  115. var displayLabel = label;
  116. if (typeof maxTagTextLength === 'number') {
  117. if (typeof label === 'string' || typeof label === 'number') {
  118. var strLabel = String(displayLabel);
  119. if (strLabel.length > maxTagTextLength) {
  120. displayLabel = "".concat(strLabel.slice(0, maxTagTextLength), "...");
  121. }
  122. }
  123. }
  124. var onClose = function onClose(event) {
  125. if (event) {
  126. event.stopPropagation();
  127. }
  128. onRemove(valueItem);
  129. };
  130. return typeof tagRender === 'function' ? customizeRenderSelector(value, displayLabel, itemDisabled, closable, onClose) : defaultRenderSelector(valueItem, displayLabel, itemDisabled, closable, onClose);
  131. };
  132. var renderRest = function renderRest(omittedValues) {
  133. // https://github.com/ant-design/ant-design/issues/48930
  134. if (!values.length) {
  135. return null;
  136. }
  137. var content = typeof maxTagPlaceholder === 'function' ? maxTagPlaceholder(omittedValues) : maxTagPlaceholder;
  138. return typeof tagRender === 'function' ? customizeRenderSelector(undefined, content, false, false, undefined, true) : defaultRenderSelector({
  139. title: content
  140. }, content, false);
  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. onBlur: onInputBlur,
  173. tabIndex: tabIndex,
  174. attrs: (0, _pickAttrs.default)(props, true)
  175. }), /*#__PURE__*/React.createElement("span", {
  176. ref: measureRef,
  177. className: "".concat(selectionPrefixCls, "-search-mirror"),
  178. "aria-hidden": true
  179. }, inputValue, "\xA0"));
  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. };
  196. var _default = exports.default = SelectSelector;