Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: trigger onChange twice when inputting using the input method #60

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 27 additions & 17 deletions src/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,47 +98,57 @@ const Input = forwardRef<InputRef, InputProps>((props, ref) => {
| React.CompositionEvent<HTMLInputElement>,
currentValue: string,
) => {
let cutValue = currentValue;
setValue(currentValue);
if (inputRef.current) {
resolveOnChange(inputRef.current, e, onChange, currentValue);
}
};

React.useEffect(() => {
if (selection) {
inputRef.current?.setSelectionRange(...selection);
}
}, [selection]);

const onInternalFormatter = (currentValue: string) => {
let cutValue = currentValue;
let isExceed = false;
if (
!compositionRef.current &&
countConfig.exceedFormatter &&
countConfig.max &&
countConfig.strategy(currentValue) > countConfig.max
) {
cutValue = countConfig.exceedFormatter(currentValue, {
max: countConfig.max,
isExceed = true;
cutValue = countConfig.exceedFormatter!(currentValue, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里不要使用!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isExceed 里确实判断,看有没有更好的代替?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我目前能想到的还有一个思路是:
将formatter相关的代码抽离出来,返回一个cutValue和isExceed
const { cutValue, isExceed } = onInternalFormatter(e.currentTarget.value);
onInternalChange的事件中,formatter后直接触发triggerChange
onInternalCompositionEnd的事件中,判断是否isExceed,如果为true就触发triggerChange

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感觉这样会容易混淆逻辑 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我认为fomatter是一个独立的事情,isExceed和cutValue都是和formatter相关的内容,表示格式化后是否超出限制和最终裁剪的值。目前的处境是在change的时候需要格式化,在compositionEnd的时候需要格式化并且条件调用change

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改改看

max: countConfig.max!,
});

if (currentValue !== cutValue) {
setSelection([
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一个看似计算的方法,内部确在调用setSelection, 有点奇怪

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

副作用的意思。

inputRef.current?.selectionStart || 0,
inputRef.current?.selectionEnd || 0,
]);
}
}
setValue(cutValue);

if (inputRef.current) {
resolveOnChange(inputRef.current, e, onChange, cutValue);
}
return {
cutValue,
isExceed,
};
};

React.useEffect(() => {
if (selection) {
inputRef.current?.setSelectionRange(...selection);
}
}, [selection]);

const onInternalChange: React.ChangeEventHandler<HTMLInputElement> = (e) => {
triggerChange(e, e.target.value);
const { cutValue } = onInternalFormatter(e.target.value);
triggerChange(e, cutValue);
};

const onInternalCompositionEnd = (
e: React.CompositionEvent<HTMLInputElement>,
) => {
compositionRef.current = false;
triggerChange(e, e.currentTarget.value);
const { cutValue, isExceed } = onInternalFormatter(e.currentTarget.value);
if (isExceed) {
triggerChange(e, cutValue);
}
onCompositionEnd?.(e);
};

Expand Down
39 changes: 39 additions & 0 deletions tests/count.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,45 @@ describe('Input.Count', () => {
expect(setSelectionRange).toHaveBeenCalledWith(2, 2);
});

it('input using the input method should trigger onChange once', () => {
const onChange = jest.fn();
const { container } = render(<Input onChange={onChange} />);
const input = container.querySelector('input')!;
fireEvent.compositionStart(input);
fireEvent.compositionUpdate(input, { data: '你' });
fireEvent.compositionEnd(input, { data: '你好' });
fireEvent.input(input, { target: { value: '你好' } });
expect(onChange).toHaveBeenCalledTimes(1);
});

it('using the input method to enter the cropped content should trigger onChange twice', () => {
const onChange = jest.fn();
const onCompositionEnd = jest.fn();
const { container } = render(
<Input
count={{
show: true,
max: 3,
exceedFormatter: (val, { max }) =>
getSegments(val)
.filter((seg) => seg.index + seg.segment.length <= max)
.map((seg) => seg.segment)
.join(''),
}}
onChange={onChange}
onCompositionEnd={onCompositionEnd}
/>,
);
const input = container.querySelector('input')!;
fireEvent.compositionStart(input);
fireEvent.compositionUpdate(input, { target: { value: '你' } });
fireEvent.compositionUpdate(input, { target: { value: '你好' } });
fireEvent.compositionUpdate(input, { target: { value: '你好世' } });
fireEvent.compositionUpdate(input, { target: { value: '你好世界' } });
fireEvent.compositionEnd(input, { target: { value: '你好世界' } });
expect(input?.value).toEqual('你好世');
});

describe('cls', () => {
it('raw', () => {
const { container } = render(
Expand Down
Loading