From 92486ac7872a7bf9f36b7d7672d69cf52fda5198 Mon Sep 17 00:00:00 2001 From: RainyLiao Date: Mon, 25 Dec 2023 15:02:37 +0800 Subject: [PATCH] fix: trigger onChange twice when inputting using the input method --- src/Input.tsx | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/Input.tsx b/src/Input.tsx index abce2a9..5be23dd 100644 --- a/src/Input.tsx +++ b/src/Input.tsx @@ -68,6 +68,14 @@ const Input = forwardRef((props, ref) => { const valueLength = countConfig.strategy(formatValue); const isOutOfRange = !!mergedMax && valueLength > mergedMax; + const isExceed = (currentValue: string) => { + return ( + !compositionRef.current && + countConfig.exceedFormatter && + countConfig.max && + countConfig.strategy(currentValue) > countConfig.max + ); + }; // ======================= Ref ======================== useImperativeHandle(ref, () => ({ @@ -100,14 +108,9 @@ const Input = forwardRef((props, ref) => { ) => { let cutValue = currentValue; - if ( - !compositionRef.current && - countConfig.exceedFormatter && - countConfig.max && - countConfig.strategy(currentValue) > countConfig.max - ) { - cutValue = countConfig.exceedFormatter(currentValue, { - max: countConfig.max, + if (isExceed(currentValue)) { + cutValue = countConfig.exceedFormatter!(currentValue, { + max: countConfig.max!, }); if (currentValue !== cutValue) { @@ -138,7 +141,8 @@ const Input = forwardRef((props, ref) => { e: React.CompositionEvent, ) => { compositionRef.current = false; - triggerChange(e, e.currentTarget.value); + if (isExceed(e.currentTarget.value)) + triggerChange(e, e.currentTarget.value); onCompositionEnd?.(e); };