From 3afa7a9bfa8e912eac3000bfa65bb7670d25b60d Mon Sep 17 00:00:00 2001 From: Moksh Upadhyay Date: Wed, 2 Dec 2026 07:41:55 +0530 Subject: [PATCH 1/2] fix: avoid triggering TextArea onChange twice on compositionEnd --- src/TextArea.tsx | 16 ++++++++++++++-- tests/TextArea.count.test.tsx | 11 +++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/TextArea.tsx b/src/TextArea.tsx index 964b2c7..2f20435 100644 --- a/src/TextArea.tsx +++ b/src/TextArea.tsx @@ -8,6 +8,7 @@ import useCountDisplay from './hooks/useCountDisplay'; import useCountExceed from './hooks/useCountExceed'; import useMergedValue from './hooks/useMergedValue'; import type { + ChangeEventInfo, ResizableTextAreaRef, TextAreaProps, TextAreaRef, @@ -96,8 +97,15 @@ const TextArea = React.forwardRef( | React.ChangeEvent | React.CompositionEvent, currentValue: string, + info: ChangeEventInfo, ) => { const cutValue = getExceedValue(currentValue, compositionRef.current); + + if (info.source === 'compositionEnd' && currentValue === cutValue) { + // Avoid triggering twice + // https://github.com/ant-design/ant-design/issues/46587 + return; + } setValue(cutValue); resolveOnChange(e.currentTarget, e, onChange, cutValue); @@ -115,12 +123,16 @@ const TextArea = React.forwardRef( HTMLTextAreaElement > = (e) => { compositionRef.current = false; - triggerChange(e, e.currentTarget.value); + triggerChange(e, e.currentTarget.value, { + source: 'compositionEnd', + }); onCompositionEnd?.(e); }; const onInternalChange = (e: React.ChangeEvent) => { - triggerChange(e, e.target.value); + triggerChange(e, e.target.value, { + source: 'change', + }); }; const handleKeyDown = (e: React.KeyboardEvent) => { diff --git a/tests/TextArea.count.test.tsx b/tests/TextArea.count.test.tsx index f58def5..1350c65 100644 --- a/tests/TextArea.count.test.tsx +++ b/tests/TextArea.count.test.tsx @@ -165,4 +165,15 @@ describe('TextArea.Count', () => { container.querySelector('textarea')?.getAttribute('maxlength'), ).toEqual('33'); }); + + it('textarea using the input method should trigger onChange once', () => { + const onChange = jest.fn(); + const { container } = render(