Skip to content

Commit

Permalink
fix: trigger onChange twice when inputting using the input method
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian-Zephyr-Liao committed Dec 25, 2023
1 parent 9640649 commit 92486ac
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ const Input = forwardRef<InputRef, InputProps>((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, () => ({
Expand Down Expand Up @@ -100,14 +108,9 @@ const Input = forwardRef<InputRef, InputProps>((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) {
Expand Down Expand Up @@ -138,7 +141,8 @@ const Input = forwardRef<InputRef, InputProps>((props, ref) => {
e: React.CompositionEvent<HTMLInputElement>,
) => {
compositionRef.current = false;
triggerChange(e, e.currentTarget.value);
if (isExceed(e.currentTarget.value))
triggerChange(e, e.currentTarget.value);
onCompositionEnd?.(e);
};

Expand Down

0 comments on commit 92486ac

Please sign in to comment.