diff --git a/components/input/TextArea.tsx b/components/input/TextArea.tsx index 6a105bddfc..45c3009b83 100644 --- a/components/input/TextArea.tsx +++ b/components/input/TextArea.tsx @@ -17,7 +17,6 @@ import type { SizeType } from '../config-provider/SizeContext'; import { FormItemInputContext } from '../form/context'; import useVariant from '../form/hooks/useVariants'; import { useCompactItemContext } from '../space/Compact'; -import useHandleResizeWrapper from './hooks/useHandleResizeWrapper'; import type { InputFocusOptions } from './Input'; import { triggerFocus } from './Input'; import { useSharedStyle } from './style'; @@ -57,6 +56,8 @@ const TextArea = forwardRef((props, ref) => { styles, variant: customVariant, showCount, + onMouseDown, + onResize, ...rest } = props; @@ -76,11 +77,11 @@ const TextArea = forwardRef((props, ref) => { styles: contextStyles, } = useComponentConfig('textArea'); - // ===================== Disabled ===================== + // =================== Disabled =================== const disabled = React.useContext(DisabledContext); const mergedDisabled = customDisabled ?? disabled; - // ===================== Status ===================== + // ==================== Status ==================== const { status: contextStatus, hasFeedback, @@ -88,7 +89,7 @@ const TextArea = forwardRef((props, ref) => { } = React.useContext(FormItemInputContext); const mergedStatus = getMergedStatus(contextStatus, customStatus); - // ===================== Ref ===================== + // ===================== Ref ====================== const innerRef = React.useRef(null); React.useImperativeHandle(ref, () => ({ @@ -101,12 +102,12 @@ const TextArea = forwardRef((props, ref) => { const prefixCls = getPrefixCls('input', customizePrefixCls); - // ===================== Style ===================== + // ==================== Style ===================== const rootCls = useCSSVarCls(prefixCls); const [wrapSharedCSSVar, hashId, cssVarCls] = useSharedStyle(prefixCls, rootClassName); const [wrapCSSVar] = useStyle(prefixCls, rootCls); - // ===================== Compact Item ===================== + // ================= Compact Item ================= const { compactSize, compactItemClassnames } = useCompactItemContext(prefixCls, direction); // ===================== Size ===================== @@ -116,8 +117,39 @@ const TextArea = forwardRef((props, ref) => { const mergedAllowClear = getAllowClear(allowClear ?? contextAllowClear); - const handleResizeWrapper = useHandleResizeWrapper(); + // ==================== Resize ==================== + // https://github.com/ant-design/ant-design/issues/51594 + const [isMouseDown, setIsMouseDown] = React.useState(false); + // When has wrapper, resize will make as dirty for `resize: both` style + const [resizeDirty, setResizeDirty] = React.useState(false); + + const onInternalMouseDown: typeof onMouseDown = (e) => { + setIsMouseDown(true); + onMouseDown?.(e); + + const onMouseUp = () => { + setIsMouseDown(false); + document.removeEventListener('mouseup', onMouseUp); + }; + + document.addEventListener('mouseup', onMouseUp); + }; + + const onInternalResize: RcTextAreaProps['onResize'] = (size) => { + onResize?.(size); + + // Change to dirty since this maybe from the `resize: both` style + if (isMouseDown && typeof getComputedStyle === 'function') { + const ele = innerRef.current?.nativeElement?.querySelector('textarea'); + + if (ele && getComputedStyle(ele).resize === 'both') { + setResizeDirty(true); + } + } + }; + + // ==================== Render ==================== return wrapSharedCSSVar( wrapCSSVar( ((props, ref) => { rootClassName, compactItemClassnames, contextClassName, + // Only for wrapper + resizeDirty && `${prefixCls}-textarea-affix-wrapper-resize-dirty`, )} classNames={{ ...classes, @@ -146,6 +180,7 @@ const TextArea = forwardRef((props, ref) => { hashId, classes?.textarea, contextClassNames.textarea, + isMouseDown && `${prefixCls}-mouse-active`, ), variant: classNames( { @@ -170,10 +205,8 @@ const TextArea = forwardRef((props, ref) => { } showCount={showCount} ref={innerRef} - onResize={(size) => { - rest.onResize?.(size); - showCount && handleResizeWrapper(innerRef.current); - }} + onResize={onInternalResize} + onMouseDown={onInternalMouseDown} />, ), ); diff --git a/components/input/__tests__/__snapshots__/demo-extend.test.ts.snap b/components/input/__tests__/__snapshots__/demo-extend.test.ts.snap index 7a2939bb75..44e0eab6d5 100644 --- a/components/input/__tests__/__snapshots__/demo-extend.test.ts.snap +++ b/components/input/__tests__/__snapshots__/demo-extend.test.ts.snap @@ -11956,6 +11956,26 @@ Array [ , +
, + +