Skip to content

Commit 92486ac

Browse files
fix: trigger onChange twice when inputting using the input method
1 parent 9640649 commit 92486ac

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/Input.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ const Input = forwardRef<InputRef, InputProps>((props, ref) => {
6868
const valueLength = countConfig.strategy(formatValue);
6969

7070
const isOutOfRange = !!mergedMax && valueLength > mergedMax;
71+
const isExceed = (currentValue: string) => {
72+
return (
73+
!compositionRef.current &&
74+
countConfig.exceedFormatter &&
75+
countConfig.max &&
76+
countConfig.strategy(currentValue) > countConfig.max
77+
);
78+
};
7179

7280
// ======================= Ref ========================
7381
useImperativeHandle(ref, () => ({
@@ -100,14 +108,9 @@ const Input = forwardRef<InputRef, InputProps>((props, ref) => {
100108
) => {
101109
let cutValue = currentValue;
102110

103-
if (
104-
!compositionRef.current &&
105-
countConfig.exceedFormatter &&
106-
countConfig.max &&
107-
countConfig.strategy(currentValue) > countConfig.max
108-
) {
109-
cutValue = countConfig.exceedFormatter(currentValue, {
110-
max: countConfig.max,
111+
if (isExceed(currentValue)) {
112+
cutValue = countConfig.exceedFormatter!(currentValue, {
113+
max: countConfig.max!,
111114
});
112115

113116
if (currentValue !== cutValue) {
@@ -138,7 +141,8 @@ const Input = forwardRef<InputRef, InputProps>((props, ref) => {
138141
e: React.CompositionEvent<HTMLInputElement>,
139142
) => {
140143
compositionRef.current = false;
141-
triggerChange(e, e.currentTarget.value);
144+
if (isExceed(e.currentTarget.value))
145+
triggerChange(e, e.currentTarget.value);
142146
onCompositionEnd?.(e);
143147
};
144148

0 commit comments

Comments
 (0)