Input.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. "use client";
  2. var __rest = this && this.__rest || function (s, e) {
  3. var t = {};
  4. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  5. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  6. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  7. }
  8. return t;
  9. };
  10. import React, { forwardRef, useContext, useEffect, useRef } from 'react';
  11. import cls from 'classnames';
  12. import RcInput from 'rc-input';
  13. import { triggerFocus } from "rc-input/es/utils/commonUtils";
  14. import { composeRef } from "rc-util/es/ref";
  15. import ContextIsolator from '../_util/ContextIsolator';
  16. import getAllowClear from '../_util/getAllowClear';
  17. import { getMergedStatus, getStatusClassNames } from '../_util/statusUtils';
  18. import { devUseWarning } from '../_util/warning';
  19. import { useComponentConfig } from '../config-provider/context';
  20. import DisabledContext from '../config-provider/DisabledContext';
  21. import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
  22. import useSize from '../config-provider/hooks/useSize';
  23. import { FormItemInputContext } from '../form/context';
  24. import useVariant from '../form/hooks/useVariants';
  25. import { useCompactItemContext } from '../space/Compact';
  26. import useRemovePasswordTimeout from './hooks/useRemovePasswordTimeout';
  27. import useStyle, { useSharedStyle } from './style';
  28. import { hasPrefixSuffix } from './utils';
  29. export { triggerFocus };
  30. const Input = /*#__PURE__*/forwardRef((props, ref) => {
  31. const {
  32. prefixCls: customizePrefixCls,
  33. bordered = true,
  34. status: customStatus,
  35. size: customSize,
  36. disabled: customDisabled,
  37. onBlur,
  38. onFocus,
  39. suffix,
  40. allowClear,
  41. addonAfter,
  42. addonBefore,
  43. className,
  44. style,
  45. styles,
  46. rootClassName,
  47. onChange,
  48. classNames,
  49. variant: customVariant
  50. } = props,
  51. rest = __rest(props, ["prefixCls", "bordered", "status", "size", "disabled", "onBlur", "onFocus", "suffix", "allowClear", "addonAfter", "addonBefore", "className", "style", "styles", "rootClassName", "onChange", "classNames", "variant"]);
  52. if (process.env.NODE_ENV !== 'production') {
  53. const {
  54. deprecated
  55. } = devUseWarning('Input');
  56. deprecated(!('bordered' in props), 'bordered', 'variant');
  57. }
  58. const {
  59. getPrefixCls,
  60. direction,
  61. allowClear: contextAllowClear,
  62. autoComplete: contextAutoComplete,
  63. className: contextClassName,
  64. style: contextStyle,
  65. classNames: contextClassNames,
  66. styles: contextStyles
  67. } = useComponentConfig('input');
  68. const prefixCls = getPrefixCls('input', customizePrefixCls);
  69. const inputRef = useRef(null);
  70. // Style
  71. const rootCls = useCSSVarCls(prefixCls);
  72. const [wrapSharedCSSVar, hashId, cssVarCls] = useSharedStyle(prefixCls, rootClassName);
  73. const [wrapCSSVar] = useStyle(prefixCls, rootCls);
  74. // ===================== Compact Item =====================
  75. const {
  76. compactSize,
  77. compactItemClassnames
  78. } = useCompactItemContext(prefixCls, direction);
  79. // ===================== Size =====================
  80. const mergedSize = useSize(ctx => {
  81. var _a;
  82. return (_a = customSize !== null && customSize !== void 0 ? customSize : compactSize) !== null && _a !== void 0 ? _a : ctx;
  83. });
  84. // ===================== Disabled =====================
  85. const disabled = React.useContext(DisabledContext);
  86. const mergedDisabled = customDisabled !== null && customDisabled !== void 0 ? customDisabled : disabled;
  87. // ===================== Status =====================
  88. const {
  89. status: contextStatus,
  90. hasFeedback,
  91. feedbackIcon
  92. } = useContext(FormItemInputContext);
  93. const mergedStatus = getMergedStatus(contextStatus, customStatus);
  94. // ===================== Focus warning =====================
  95. const inputHasPrefixSuffix = hasPrefixSuffix(props) || !!hasFeedback;
  96. const prevHasPrefixSuffix = useRef(inputHasPrefixSuffix);
  97. /* eslint-disable react-hooks/rules-of-hooks */
  98. if (process.env.NODE_ENV !== 'production') {
  99. const warning = devUseWarning('Input');
  100. useEffect(() => {
  101. var _a;
  102. if (inputHasPrefixSuffix && !prevHasPrefixSuffix.current) {
  103. process.env.NODE_ENV !== "production" ? warning(document.activeElement === ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.input), 'usage', `When Input is focused, dynamic add or remove prefix / suffix will make it lose focus caused by dom structure change. Read more: https://ant.design/components/input/#FAQ`) : void 0;
  104. }
  105. prevHasPrefixSuffix.current = inputHasPrefixSuffix;
  106. }, [inputHasPrefixSuffix]);
  107. }
  108. /* eslint-enable */
  109. // ===================== Remove Password value =====================
  110. const removePasswordTimeout = useRemovePasswordTimeout(inputRef, true);
  111. const handleBlur = e => {
  112. removePasswordTimeout();
  113. onBlur === null || onBlur === void 0 ? void 0 : onBlur(e);
  114. };
  115. const handleFocus = e => {
  116. removePasswordTimeout();
  117. onFocus === null || onFocus === void 0 ? void 0 : onFocus(e);
  118. };
  119. const handleChange = e => {
  120. removePasswordTimeout();
  121. onChange === null || onChange === void 0 ? void 0 : onChange(e);
  122. };
  123. const suffixNode = (hasFeedback || suffix) && (/*#__PURE__*/React.createElement(React.Fragment, null, suffix, hasFeedback && feedbackIcon));
  124. const mergedAllowClear = getAllowClear(allowClear !== null && allowClear !== void 0 ? allowClear : contextAllowClear);
  125. const [variant, enableVariantCls] = useVariant('input', customVariant, bordered);
  126. return wrapSharedCSSVar(wrapCSSVar(/*#__PURE__*/React.createElement(RcInput, Object.assign({
  127. ref: composeRef(ref, inputRef),
  128. prefixCls: prefixCls,
  129. autoComplete: contextAutoComplete
  130. }, rest, {
  131. disabled: mergedDisabled,
  132. onBlur: handleBlur,
  133. onFocus: handleFocus,
  134. style: Object.assign(Object.assign({}, contextStyle), style),
  135. styles: Object.assign(Object.assign({}, contextStyles), styles),
  136. suffix: suffixNode,
  137. allowClear: mergedAllowClear,
  138. className: cls(className, rootClassName, cssVarCls, rootCls, compactItemClassnames, contextClassName),
  139. onChange: handleChange,
  140. addonBefore: addonBefore && (/*#__PURE__*/React.createElement(ContextIsolator, {
  141. form: true,
  142. space: true
  143. }, addonBefore)),
  144. addonAfter: addonAfter && (/*#__PURE__*/React.createElement(ContextIsolator, {
  145. form: true,
  146. space: true
  147. }, addonAfter)),
  148. classNames: Object.assign(Object.assign(Object.assign({}, classNames), contextClassNames), {
  149. input: cls({
  150. [`${prefixCls}-sm`]: mergedSize === 'small',
  151. [`${prefixCls}-lg`]: mergedSize === 'large',
  152. [`${prefixCls}-rtl`]: direction === 'rtl'
  153. }, classNames === null || classNames === void 0 ? void 0 : classNames.input, contextClassNames.input, hashId),
  154. variant: cls({
  155. [`${prefixCls}-${variant}`]: enableVariantCls
  156. }, getStatusClassNames(prefixCls, mergedStatus)),
  157. affixWrapper: cls({
  158. [`${prefixCls}-affix-wrapper-sm`]: mergedSize === 'small',
  159. [`${prefixCls}-affix-wrapper-lg`]: mergedSize === 'large',
  160. [`${prefixCls}-affix-wrapper-rtl`]: direction === 'rtl'
  161. }, hashId),
  162. wrapper: cls({
  163. [`${prefixCls}-group-rtl`]: direction === 'rtl'
  164. }, hashId),
  165. groupWrapper: cls({
  166. [`${prefixCls}-group-wrapper-sm`]: mergedSize === 'small',
  167. [`${prefixCls}-group-wrapper-lg`]: mergedSize === 'large',
  168. [`${prefixCls}-group-wrapper-rtl`]: direction === 'rtl',
  169. [`${prefixCls}-group-wrapper-${variant}`]: enableVariantCls
  170. }, getStatusClassNames(`${prefixCls}-group-wrapper`, mergedStatus, hasFeedback), hashId)
  171. })
  172. }))));
  173. });
  174. if (process.env.NODE_ENV !== 'production') {
  175. Input.displayName = 'Input';
  176. }
  177. export default Input;