TextArea.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 * as React from 'react';
  11. import { forwardRef } from 'react';
  12. import classNames from 'classnames';
  13. import RcTextArea from 'rc-textarea';
  14. import getAllowClear from '../_util/getAllowClear';
  15. import { getMergedStatus, getStatusClassNames } from '../_util/statusUtils';
  16. import { devUseWarning } from '../_util/warning';
  17. import { useComponentConfig } from '../config-provider/context';
  18. import DisabledContext from '../config-provider/DisabledContext';
  19. import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
  20. import useSize from '../config-provider/hooks/useSize';
  21. import { FormItemInputContext } from '../form/context';
  22. import useVariant from '../form/hooks/useVariants';
  23. import { useCompactItemContext } from '../space/Compact';
  24. import { triggerFocus } from './Input';
  25. import { useSharedStyle } from './style';
  26. import useStyle from './style/textarea';
  27. const TextArea = /*#__PURE__*/forwardRef((props, ref) => {
  28. var _a;
  29. const {
  30. prefixCls: customizePrefixCls,
  31. bordered = true,
  32. size: customizeSize,
  33. disabled: customDisabled,
  34. status: customStatus,
  35. allowClear,
  36. classNames: classes,
  37. rootClassName,
  38. className,
  39. style,
  40. styles,
  41. variant: customVariant,
  42. showCount,
  43. onMouseDown,
  44. onResize
  45. } = props,
  46. rest = __rest(props, ["prefixCls", "bordered", "size", "disabled", "status", "allowClear", "classNames", "rootClassName", "className", "style", "styles", "variant", "showCount", "onMouseDown", "onResize"]);
  47. if (process.env.NODE_ENV !== 'production') {
  48. const {
  49. deprecated
  50. } = devUseWarning('TextArea');
  51. deprecated(!('bordered' in props), 'bordered', 'variant');
  52. }
  53. const {
  54. getPrefixCls,
  55. direction,
  56. allowClear: contextAllowClear,
  57. autoComplete: contextAutoComplete,
  58. className: contextClassName,
  59. style: contextStyle,
  60. classNames: contextClassNames,
  61. styles: contextStyles
  62. } = useComponentConfig('textArea');
  63. // =================== Disabled ===================
  64. const disabled = React.useContext(DisabledContext);
  65. const mergedDisabled = customDisabled !== null && customDisabled !== void 0 ? customDisabled : disabled;
  66. // ==================== Status ====================
  67. const {
  68. status: contextStatus,
  69. hasFeedback,
  70. feedbackIcon
  71. } = React.useContext(FormItemInputContext);
  72. const mergedStatus = getMergedStatus(contextStatus, customStatus);
  73. // ===================== Ref ======================
  74. const innerRef = React.useRef(null);
  75. React.useImperativeHandle(ref, () => {
  76. var _a;
  77. return {
  78. resizableTextArea: (_a = innerRef.current) === null || _a === void 0 ? void 0 : _a.resizableTextArea,
  79. focus: option => {
  80. var _a, _b;
  81. triggerFocus((_b = (_a = innerRef.current) === null || _a === void 0 ? void 0 : _a.resizableTextArea) === null || _b === void 0 ? void 0 : _b.textArea, option);
  82. },
  83. blur: () => {
  84. var _a;
  85. return (_a = innerRef.current) === null || _a === void 0 ? void 0 : _a.blur();
  86. }
  87. };
  88. });
  89. const prefixCls = getPrefixCls('input', customizePrefixCls);
  90. // ==================== Style =====================
  91. const rootCls = useCSSVarCls(prefixCls);
  92. const [wrapSharedCSSVar, hashId, cssVarCls] = useSharedStyle(prefixCls, rootClassName);
  93. const [wrapCSSVar] = useStyle(prefixCls, rootCls);
  94. // ================= Compact Item =================
  95. const {
  96. compactSize,
  97. compactItemClassnames
  98. } = useCompactItemContext(prefixCls, direction);
  99. // ===================== Size =====================
  100. const mergedSize = useSize(ctx => {
  101. var _a;
  102. return (_a = customizeSize !== null && customizeSize !== void 0 ? customizeSize : compactSize) !== null && _a !== void 0 ? _a : ctx;
  103. });
  104. const [variant, enableVariantCls] = useVariant('textArea', customVariant, bordered);
  105. const mergedAllowClear = getAllowClear(allowClear !== null && allowClear !== void 0 ? allowClear : contextAllowClear);
  106. // ==================== Resize ====================
  107. // https://github.com/ant-design/ant-design/issues/51594
  108. const [isMouseDown, setIsMouseDown] = React.useState(false);
  109. // When has wrapper, resize will make as dirty for `resize: both` style
  110. const [resizeDirty, setResizeDirty] = React.useState(false);
  111. const onInternalMouseDown = e => {
  112. setIsMouseDown(true);
  113. onMouseDown === null || onMouseDown === void 0 ? void 0 : onMouseDown(e);
  114. const onMouseUp = () => {
  115. setIsMouseDown(false);
  116. document.removeEventListener('mouseup', onMouseUp);
  117. };
  118. document.addEventListener('mouseup', onMouseUp);
  119. };
  120. const onInternalResize = size => {
  121. var _a, _b;
  122. onResize === null || onResize === void 0 ? void 0 : onResize(size);
  123. // Change to dirty since this maybe from the `resize: both` style
  124. if (isMouseDown && typeof getComputedStyle === 'function') {
  125. const ele = (_b = (_a = innerRef.current) === null || _a === void 0 ? void 0 : _a.nativeElement) === null || _b === void 0 ? void 0 : _b.querySelector('textarea');
  126. if (ele && getComputedStyle(ele).resize === 'both') {
  127. setResizeDirty(true);
  128. }
  129. }
  130. };
  131. // ==================== Render ====================
  132. return wrapSharedCSSVar(wrapCSSVar(/*#__PURE__*/React.createElement(RcTextArea, Object.assign({
  133. autoComplete: contextAutoComplete
  134. }, rest, {
  135. style: Object.assign(Object.assign({}, contextStyle), style),
  136. styles: Object.assign(Object.assign({}, contextStyles), styles),
  137. disabled: mergedDisabled,
  138. allowClear: mergedAllowClear,
  139. className: classNames(cssVarCls, rootCls, className, rootClassName, compactItemClassnames, contextClassName,
  140. // Only for wrapper
  141. resizeDirty && `${prefixCls}-textarea-affix-wrapper-resize-dirty`),
  142. classNames: Object.assign(Object.assign(Object.assign({}, classes), contextClassNames), {
  143. textarea: classNames({
  144. [`${prefixCls}-sm`]: mergedSize === 'small',
  145. [`${prefixCls}-lg`]: mergedSize === 'large'
  146. }, hashId, classes === null || classes === void 0 ? void 0 : classes.textarea, contextClassNames.textarea, isMouseDown && `${prefixCls}-mouse-active`),
  147. variant: classNames({
  148. [`${prefixCls}-${variant}`]: enableVariantCls
  149. }, getStatusClassNames(prefixCls, mergedStatus)),
  150. affixWrapper: classNames(`${prefixCls}-textarea-affix-wrapper`, {
  151. [`${prefixCls}-affix-wrapper-rtl`]: direction === 'rtl',
  152. [`${prefixCls}-affix-wrapper-sm`]: mergedSize === 'small',
  153. [`${prefixCls}-affix-wrapper-lg`]: mergedSize === 'large',
  154. [`${prefixCls}-textarea-show-count`]: showCount || ((_a = props.count) === null || _a === void 0 ? void 0 : _a.show)
  155. }, hashId)
  156. }),
  157. prefixCls: prefixCls,
  158. suffix: hasFeedback && /*#__PURE__*/React.createElement("span", {
  159. className: `${prefixCls}-textarea-suffix`
  160. }, feedbackIcon),
  161. showCount: showCount,
  162. ref: innerRef,
  163. onResize: onInternalResize,
  164. onMouseDown: onInternalMouseDown
  165. }))));
  166. });
  167. export default TextArea;