Input.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import _extends from "@babel/runtime/helpers/esm/extends";
  3. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  4. import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
  5. import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
  6. import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
  7. var _excluded = ["autoComplete", "onChange", "onFocus", "onBlur", "onPressEnter", "onKeyDown", "onKeyUp", "prefixCls", "disabled", "htmlSize", "className", "maxLength", "suffix", "showCount", "count", "type", "classes", "classNames", "styles", "onCompositionStart", "onCompositionEnd"];
  8. import clsx from 'classnames';
  9. import useMergedState from "rc-util/es/hooks/useMergedState";
  10. import omit from "rc-util/es/omit";
  11. import React, { forwardRef, useEffect, useImperativeHandle, useRef, useState } from 'react';
  12. import BaseInput from "./BaseInput";
  13. import useCount from "./hooks/useCount";
  14. import { resolveOnChange, triggerFocus } from "./utils/commonUtils";
  15. var Input = /*#__PURE__*/forwardRef(function (props, ref) {
  16. var autoComplete = props.autoComplete,
  17. onChange = props.onChange,
  18. onFocus = props.onFocus,
  19. onBlur = props.onBlur,
  20. onPressEnter = props.onPressEnter,
  21. onKeyDown = props.onKeyDown,
  22. onKeyUp = props.onKeyUp,
  23. _props$prefixCls = props.prefixCls,
  24. prefixCls = _props$prefixCls === void 0 ? 'rc-input' : _props$prefixCls,
  25. disabled = props.disabled,
  26. htmlSize = props.htmlSize,
  27. className = props.className,
  28. maxLength = props.maxLength,
  29. suffix = props.suffix,
  30. showCount = props.showCount,
  31. count = props.count,
  32. _props$type = props.type,
  33. type = _props$type === void 0 ? 'text' : _props$type,
  34. classes = props.classes,
  35. classNames = props.classNames,
  36. styles = props.styles,
  37. _onCompositionStart = props.onCompositionStart,
  38. onCompositionEnd = props.onCompositionEnd,
  39. rest = _objectWithoutProperties(props, _excluded);
  40. var _useState = useState(false),
  41. _useState2 = _slicedToArray(_useState, 2),
  42. focused = _useState2[0],
  43. setFocused = _useState2[1];
  44. var compositionRef = useRef(false);
  45. var keyLockRef = useRef(false);
  46. var inputRef = useRef(null);
  47. var holderRef = useRef(null);
  48. var focus = function focus(option) {
  49. if (inputRef.current) {
  50. triggerFocus(inputRef.current, option);
  51. }
  52. };
  53. // ====================== Value =======================
  54. var _useMergedState = useMergedState(props.defaultValue, {
  55. value: props.value
  56. }),
  57. _useMergedState2 = _slicedToArray(_useMergedState, 2),
  58. value = _useMergedState2[0],
  59. setValue = _useMergedState2[1];
  60. var formatValue = value === undefined || value === null ? '' : String(value);
  61. // =================== Select Range ===================
  62. var _useState3 = useState(null),
  63. _useState4 = _slicedToArray(_useState3, 2),
  64. selection = _useState4[0],
  65. setSelection = _useState4[1];
  66. // ====================== Count =======================
  67. var countConfig = useCount(count, showCount);
  68. var mergedMax = countConfig.max || maxLength;
  69. var valueLength = countConfig.strategy(formatValue);
  70. var isOutOfRange = !!mergedMax && valueLength > mergedMax;
  71. // ======================= Ref ========================
  72. useImperativeHandle(ref, function () {
  73. var _holderRef$current;
  74. return {
  75. focus: focus,
  76. blur: function blur() {
  77. var _inputRef$current;
  78. (_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 || _inputRef$current.blur();
  79. },
  80. setSelectionRange: function setSelectionRange(start, end, direction) {
  81. var _inputRef$current2;
  82. (_inputRef$current2 = inputRef.current) === null || _inputRef$current2 === void 0 || _inputRef$current2.setSelectionRange(start, end, direction);
  83. },
  84. select: function select() {
  85. var _inputRef$current3;
  86. (_inputRef$current3 = inputRef.current) === null || _inputRef$current3 === void 0 || _inputRef$current3.select();
  87. },
  88. input: inputRef.current,
  89. nativeElement: ((_holderRef$current = holderRef.current) === null || _holderRef$current === void 0 ? void 0 : _holderRef$current.nativeElement) || inputRef.current
  90. };
  91. });
  92. useEffect(function () {
  93. if (keyLockRef.current) {
  94. keyLockRef.current = false;
  95. }
  96. setFocused(function (prev) {
  97. return prev && disabled ? false : prev;
  98. });
  99. }, [disabled]);
  100. var triggerChange = function triggerChange(e, currentValue, info) {
  101. var cutValue = currentValue;
  102. if (!compositionRef.current && countConfig.exceedFormatter && countConfig.max && countConfig.strategy(currentValue) > countConfig.max) {
  103. cutValue = countConfig.exceedFormatter(currentValue, {
  104. max: countConfig.max
  105. });
  106. if (currentValue !== cutValue) {
  107. var _inputRef$current4, _inputRef$current5;
  108. setSelection([((_inputRef$current4 = inputRef.current) === null || _inputRef$current4 === void 0 ? void 0 : _inputRef$current4.selectionStart) || 0, ((_inputRef$current5 = inputRef.current) === null || _inputRef$current5 === void 0 ? void 0 : _inputRef$current5.selectionEnd) || 0]);
  109. }
  110. } else if (info.source === 'compositionEnd') {
  111. // Avoid triggering twice
  112. // https://github.com/ant-design/ant-design/issues/46587
  113. return;
  114. }
  115. setValue(cutValue);
  116. if (inputRef.current) {
  117. resolveOnChange(inputRef.current, e, onChange, cutValue);
  118. }
  119. };
  120. useEffect(function () {
  121. if (selection) {
  122. var _inputRef$current6;
  123. (_inputRef$current6 = inputRef.current) === null || _inputRef$current6 === void 0 || _inputRef$current6.setSelectionRange.apply(_inputRef$current6, _toConsumableArray(selection));
  124. }
  125. }, [selection]);
  126. var onInternalChange = function onInternalChange(e) {
  127. triggerChange(e, e.target.value, {
  128. source: 'change'
  129. });
  130. };
  131. var onInternalCompositionEnd = function onInternalCompositionEnd(e) {
  132. compositionRef.current = false;
  133. triggerChange(e, e.currentTarget.value, {
  134. source: 'compositionEnd'
  135. });
  136. onCompositionEnd === null || onCompositionEnd === void 0 || onCompositionEnd(e);
  137. };
  138. var handleKeyDown = function handleKeyDown(e) {
  139. if (onPressEnter && e.key === 'Enter' && !keyLockRef.current) {
  140. keyLockRef.current = true;
  141. onPressEnter(e);
  142. }
  143. onKeyDown === null || onKeyDown === void 0 || onKeyDown(e);
  144. };
  145. var handleKeyUp = function handleKeyUp(e) {
  146. if (e.key === 'Enter') {
  147. keyLockRef.current = false;
  148. }
  149. onKeyUp === null || onKeyUp === void 0 || onKeyUp(e);
  150. };
  151. var handleFocus = function handleFocus(e) {
  152. setFocused(true);
  153. onFocus === null || onFocus === void 0 || onFocus(e);
  154. };
  155. var handleBlur = function handleBlur(e) {
  156. if (keyLockRef.current) {
  157. keyLockRef.current = false;
  158. }
  159. setFocused(false);
  160. onBlur === null || onBlur === void 0 || onBlur(e);
  161. };
  162. var handleReset = function handleReset(e) {
  163. setValue('');
  164. focus();
  165. if (inputRef.current) {
  166. resolveOnChange(inputRef.current, e, onChange);
  167. }
  168. };
  169. // ====================== Input =======================
  170. var outOfRangeCls = isOutOfRange && "".concat(prefixCls, "-out-of-range");
  171. var getInputElement = function getInputElement() {
  172. // Fix https://fb.me/react-unknown-prop
  173. var otherProps = omit(props, ['prefixCls', 'onPressEnter', 'addonBefore', 'addonAfter', 'prefix', 'suffix', 'allowClear',
  174. // Input elements must be either controlled or uncontrolled,
  175. // specify either the value prop, or the defaultValue prop, but not both.
  176. 'defaultValue', 'showCount', 'count', 'classes', 'htmlSize', 'styles', 'classNames', 'onClear']);
  177. return /*#__PURE__*/React.createElement("input", _extends({
  178. autoComplete: autoComplete
  179. }, otherProps, {
  180. onChange: onInternalChange,
  181. onFocus: handleFocus,
  182. onBlur: handleBlur,
  183. onKeyDown: handleKeyDown,
  184. onKeyUp: handleKeyUp,
  185. className: clsx(prefixCls, _defineProperty({}, "".concat(prefixCls, "-disabled"), disabled), classNames === null || classNames === void 0 ? void 0 : classNames.input),
  186. style: styles === null || styles === void 0 ? void 0 : styles.input,
  187. ref: inputRef,
  188. size: htmlSize,
  189. type: type,
  190. onCompositionStart: function onCompositionStart(e) {
  191. compositionRef.current = true;
  192. _onCompositionStart === null || _onCompositionStart === void 0 || _onCompositionStart(e);
  193. },
  194. onCompositionEnd: onInternalCompositionEnd
  195. }));
  196. };
  197. var getSuffix = function getSuffix() {
  198. // Max length value
  199. var hasMaxLength = Number(mergedMax) > 0;
  200. if (suffix || countConfig.show) {
  201. var dataCount = countConfig.showFormatter ? countConfig.showFormatter({
  202. value: formatValue,
  203. count: valueLength,
  204. maxLength: mergedMax
  205. }) : "".concat(valueLength).concat(hasMaxLength ? " / ".concat(mergedMax) : '');
  206. return /*#__PURE__*/React.createElement(React.Fragment, null, countConfig.show && /*#__PURE__*/React.createElement("span", {
  207. className: clsx("".concat(prefixCls, "-show-count-suffix"), _defineProperty({}, "".concat(prefixCls, "-show-count-has-suffix"), !!suffix), classNames === null || classNames === void 0 ? void 0 : classNames.count),
  208. style: _objectSpread({}, styles === null || styles === void 0 ? void 0 : styles.count)
  209. }, dataCount), suffix);
  210. }
  211. return null;
  212. };
  213. // ====================== Render ======================
  214. return /*#__PURE__*/React.createElement(BaseInput, _extends({}, rest, {
  215. prefixCls: prefixCls,
  216. className: clsx(className, outOfRangeCls),
  217. handleReset: handleReset,
  218. value: formatValue,
  219. focused: focused,
  220. triggerFocus: focus,
  221. suffix: getSuffix(),
  222. disabled: disabled,
  223. classes: classes,
  224. classNames: classNames,
  225. styles: styles,
  226. ref: holderRef
  227. }), getInputElement());
  228. });
  229. export default Input;