Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 57
fix: trigger onChange twice when inputting using the input method#60
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
92486ac2b01a6901f47baa8a9b4eFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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, { | ||
| max: countConfig.max!, | ||
| }); | ||
| if (currentValue !== cutValue) { | ||
| setSelection([ | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 一个看似计算的方法,内部确在调用 Member There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里不要使用
!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isExceed里确实判断,看有没有更好的代替?There was a problem hiding this comment.
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就触发triggerChangeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
感觉这样会容易混淆逻辑 🤔
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
改改看