index.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
  3. /**
  4. * Cursor rule:
  5. * 1. Only `showSearch` enabled
  6. * 2. Only `open` is `true`
  7. * 3. When typing, set `open` to `true` which hit rule of 2
  8. *
  9. * Accessibility:
  10. * - https://www.w3.org/TR/wai-aria-practices/examples/combobox/aria1.1pattern/listbox-combo.html
  11. */
  12. import KeyCode from "rc-util/es/KeyCode";
  13. import * as React from 'react';
  14. import { useRef } from 'react';
  15. import useLock from "../hooks/useLock";
  16. import { isValidateOpenKey } from "../utils/keyUtil";
  17. import MultipleSelector from "./MultipleSelector";
  18. import SingleSelector from "./SingleSelector";
  19. var Selector = function Selector(props, ref) {
  20. var inputRef = useRef(null);
  21. var compositionStatusRef = useRef(false);
  22. var prefixCls = props.prefixCls,
  23. open = props.open,
  24. mode = props.mode,
  25. showSearch = props.showSearch,
  26. tokenWithEnter = props.tokenWithEnter,
  27. disabled = props.disabled,
  28. prefix = props.prefix,
  29. autoClearSearchValue = props.autoClearSearchValue,
  30. onSearch = props.onSearch,
  31. onSearchSubmit = props.onSearchSubmit,
  32. onToggleOpen = props.onToggleOpen,
  33. onInputKeyDown = props.onInputKeyDown,
  34. onInputBlur = props.onInputBlur,
  35. domRef = props.domRef;
  36. // ======================= Ref =======================
  37. React.useImperativeHandle(ref, function () {
  38. return {
  39. focus: function focus(options) {
  40. inputRef.current.focus(options);
  41. },
  42. blur: function blur() {
  43. inputRef.current.blur();
  44. }
  45. };
  46. });
  47. // ====================== Input ======================
  48. var _useLock = useLock(0),
  49. _useLock2 = _slicedToArray(_useLock, 2),
  50. getInputMouseDown = _useLock2[0],
  51. setInputMouseDown = _useLock2[1];
  52. var onInternalInputKeyDown = function onInternalInputKeyDown(event) {
  53. var which = event.which;
  54. // Compatible with multiple lines in TextArea
  55. var isTextAreaElement = inputRef.current instanceof HTMLTextAreaElement;
  56. if (!isTextAreaElement && open && (which === KeyCode.UP || which === KeyCode.DOWN)) {
  57. event.preventDefault();
  58. }
  59. if (onInputKeyDown) {
  60. onInputKeyDown(event);
  61. }
  62. if (which === KeyCode.ENTER && mode === 'tags' && !compositionStatusRef.current && !open) {
  63. // When menu isn't open, OptionList won't trigger a value change
  64. // So when enter is pressed, the tag's input value should be emitted here to let selector know
  65. onSearchSubmit === null || onSearchSubmit === void 0 || onSearchSubmit(event.target.value);
  66. }
  67. // Move within the text box
  68. if (isTextAreaElement && !open && ~[KeyCode.UP, KeyCode.DOWN, KeyCode.LEFT, KeyCode.RIGHT].indexOf(which)) {
  69. return;
  70. }
  71. if (isValidateOpenKey(which)) {
  72. onToggleOpen(true);
  73. }
  74. };
  75. /**
  76. * We can not use `findDOMNode` sine it will get warning,
  77. * have to use timer to check if is input element.
  78. */
  79. var onInternalInputMouseDown = function onInternalInputMouseDown() {
  80. setInputMouseDown(true);
  81. };
  82. // When paste come, ignore next onChange
  83. var pastedTextRef = useRef(null);
  84. var triggerOnSearch = function triggerOnSearch(value) {
  85. if (onSearch(value, true, compositionStatusRef.current) !== false) {
  86. onToggleOpen(true);
  87. }
  88. };
  89. var onInputCompositionStart = function onInputCompositionStart() {
  90. compositionStatusRef.current = true;
  91. };
  92. var onInputCompositionEnd = function onInputCompositionEnd(e) {
  93. compositionStatusRef.current = false;
  94. // Trigger search again to support `tokenSeparators` with typewriting
  95. if (mode !== 'combobox') {
  96. triggerOnSearch(e.target.value);
  97. }
  98. };
  99. var onInputChange = function onInputChange(event) {
  100. var value = event.target.value;
  101. // Pasted text should replace back to origin content
  102. if (tokenWithEnter && pastedTextRef.current && /[\r\n]/.test(pastedTextRef.current)) {
  103. // CRLF will be treated as a single space for input element
  104. var replacedText = pastedTextRef.current.replace(/[\r\n]+$/, '').replace(/\r\n/g, ' ').replace(/[\r\n]/g, ' ');
  105. value = value.replace(replacedText, pastedTextRef.current);
  106. }
  107. pastedTextRef.current = null;
  108. triggerOnSearch(value);
  109. };
  110. var onInputPaste = function onInputPaste(e) {
  111. var clipboardData = e.clipboardData;
  112. var value = clipboardData === null || clipboardData === void 0 ? void 0 : clipboardData.getData('text');
  113. pastedTextRef.current = value || '';
  114. };
  115. var onClick = function onClick(_ref) {
  116. var target = _ref.target;
  117. if (target !== inputRef.current) {
  118. // Should focus input if click the selector
  119. var isIE = document.body.style.msTouchAction !== undefined;
  120. if (isIE) {
  121. setTimeout(function () {
  122. inputRef.current.focus();
  123. });
  124. } else {
  125. inputRef.current.focus();
  126. }
  127. }
  128. };
  129. var onMouseDown = function onMouseDown(event) {
  130. var inputMouseDown = getInputMouseDown();
  131. // when mode is combobox and it is disabled, don't prevent default behavior
  132. // https://github.com/ant-design/ant-design/issues/37320
  133. // https://github.com/ant-design/ant-design/issues/48281
  134. if (event.target !== inputRef.current && !inputMouseDown && !(mode === 'combobox' && disabled)) {
  135. event.preventDefault();
  136. }
  137. if (mode !== 'combobox' && (!showSearch || !inputMouseDown) || !open) {
  138. if (open && autoClearSearchValue !== false) {
  139. onSearch('', true, false);
  140. }
  141. onToggleOpen();
  142. }
  143. };
  144. // ================= Inner Selector ==================
  145. var sharedProps = {
  146. inputRef: inputRef,
  147. onInputKeyDown: onInternalInputKeyDown,
  148. onInputMouseDown: onInternalInputMouseDown,
  149. onInputChange: onInputChange,
  150. onInputPaste: onInputPaste,
  151. onInputCompositionStart: onInputCompositionStart,
  152. onInputCompositionEnd: onInputCompositionEnd,
  153. onInputBlur: onInputBlur
  154. };
  155. var selectNode = mode === 'multiple' || mode === 'tags' ? /*#__PURE__*/React.createElement(MultipleSelector, _extends({}, props, sharedProps)) : /*#__PURE__*/React.createElement(SingleSelector, _extends({}, props, sharedProps));
  156. return /*#__PURE__*/React.createElement("div", {
  157. ref: domRef,
  158. className: "".concat(prefixCls, "-selector"),
  159. onClick: onClick,
  160. onMouseDown: onMouseDown
  161. }, prefix && /*#__PURE__*/React.createElement("div", {
  162. className: "".concat(prefixCls, "-prefix")
  163. }, prefix), selectNode);
  164. };
  165. var ForwardSelector = /*#__PURE__*/React.forwardRef(Selector);
  166. if (process.env.NODE_ENV !== 'production') {
  167. ForwardSelector.displayName = 'Selector';
  168. }
  169. export default ForwardSelector;