-
Notifications
You must be signed in to change notification settings - Fork 56
fix: use committed count value during IME composition #179
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -60,11 +60,14 @@ const Input = forwardRef<InputRef, InputProps>((props, ref) => { | |||||||||||||||||||||||||||
| props.defaultValue, | ||||||||||||||||||||||||||||
| props.value, | ||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||
| // Count-related state should reflect the committed value during IME composition | ||||||||||||||||||||||||||||
| // instead of the intermediate phonetic text. | ||||||||||||||||||||||||||||
| const [countValue, setCountValue] = useState(formatValue); | ||||||||||||||||||||||||||||
|
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. 看了一下原 issue,感觉这并不是 bug。IME 状态下也是要统计字数的,原生的 validity.tooLong 也是按照输入长度来的。 我把结论 ref 过去,这个不用修。
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. 附 Codesandbox 原生的 validity: https://codesandbox.io/p/sandbox/55fv42?file=%2Findex.html%3A276%2C35-276%2C51 |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const countConfig = useCount(count, showCount); | ||||||||||||||||||||||||||||
| const { isOutOfRange, dataCount } = useCountDisplay({ | ||||||||||||||||||||||||||||
| countConfig, | ||||||||||||||||||||||||||||
| value: formatValue, | ||||||||||||||||||||||||||||
| value: countValue, | ||||||||||||||||||||||||||||
| maxLength, | ||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||
| const getExceedValue = useCountExceed({ | ||||||||||||||||||||||||||||
|
|
@@ -99,6 +102,12 @@ const Input = forwardRef<InputRef, InputProps>((props, ref) => { | |||||||||||||||||||||||||||
| setFocused((prev) => (prev && disabled ? false : prev)); | ||||||||||||||||||||||||||||
| }, [disabled]); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||||||||||||
| if (!compositionRef.current) { | ||||||||||||||||||||||||||||
| setCountValue(formatValue); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| }, [formatValue]); | ||||||||||||||||||||||||||||
|
Comment on lines
+105
to
+109
Contributor
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. |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const triggerChange = ( | ||||||||||||||||||||||||||||
| e: | ||||||||||||||||||||||||||||
| | React.ChangeEvent<HTMLInputElement> | ||||||||||||||||||||||||||||
|
|
@@ -114,6 +123,9 @@ const Input = forwardRef<InputRef, InputProps>((props, ref) => { | |||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| setValue(cutValue); | ||||||||||||||||||||||||||||
| if (!compositionRef.current) { | ||||||||||||||||||||||||||||
| setCountValue(cutValue); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
Comment on lines
123
to
+128
Contributor
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. Update
Suggested change
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if (inputRef.current) { | ||||||||||||||||||||||||||||
| resolveOnChange(inputRef.current, e, onChange, cutValue); | ||||||||||||||||||||||||||||
|
|
@@ -227,6 +239,7 @@ const Input = forwardRef<InputRef, InputProps>((props, ref) => { | |||||||||||||||||||||||||||
| type={type} | ||||||||||||||||||||||||||||
| onCompositionStart={(e) => { | ||||||||||||||||||||||||||||
| compositionRef.current = true; | ||||||||||||||||||||||||||||
| setCountValue(formatValue); | ||||||||||||||||||||||||||||
| onCompositionStart?.(e); | ||||||||||||||||||||||||||||
| }} | ||||||||||||||||||||||||||||
|
Comment on lines
240
to
244
Contributor
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. Set
Suggested change
|
||||||||||||||||||||||||||||
| onCompositionEnd={onInternalCompositionEnd} | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -60,10 +60,13 @@ const TextArea = React.forwardRef<TextAreaRef, TextAreaProps>( | |||||||||||||||
| const getTextArea = () => resizableTextAreaRef.current?.textArea || null; | ||||||||||||||||
|
|
||||||||||||||||
| const { setValue, formatValue } = useMergedValue(defaultValue, customValue); | ||||||||||||||||
| // Count-related state should reflect the committed value during IME composition | ||||||||||||||||
| // instead of the intermediate phonetic text. | ||||||||||||||||
| const [countValue, setCountValue] = React.useState(formatValue); | ||||||||||||||||
|
Comment on lines
+63
to
+65
Contributor
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. Similarly to
Suggested change
|
||||||||||||||||
| const countConfig = useCount(count, showCount); | ||||||||||||||||
| const { isOutOfRange, dataCount } = useCountDisplay({ | ||||||||||||||||
| countConfig, | ||||||||||||||||
| value: formatValue, | ||||||||||||||||
| value: countValue, | ||||||||||||||||
| maxLength, | ||||||||||||||||
| }); | ||||||||||||||||
| const getExceedValue = useCountExceed({ | ||||||||||||||||
|
|
@@ -88,6 +91,12 @@ const TextArea = React.forwardRef<TextAreaRef, TextAreaProps>( | |||||||||||||||
| setFocused((prev) => !disabled && prev); | ||||||||||||||||
| }, [disabled]); | ||||||||||||||||
|
|
||||||||||||||||
| useEffect(() => { | ||||||||||||||||
| if (!compositionRef.current) { | ||||||||||||||||
| setCountValue(formatValue); | ||||||||||||||||
| } | ||||||||||||||||
| }, [formatValue]); | ||||||||||||||||
|
Comment on lines
+94
to
+98
Contributor
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.
Comment on lines
+94
to
+98
|
||||||||||||||||
|
|
||||||||||||||||
| // ============================== Count =============================== | ||||||||||||||||
| // ============================== Change ============================== | ||||||||||||||||
| const triggerChange = ( | ||||||||||||||||
|
|
@@ -98,6 +107,9 @@ const TextArea = React.forwardRef<TextAreaRef, TextAreaProps>( | |||||||||||||||
| ) => { | ||||||||||||||||
| const cutValue = getExceedValue(currentValue, compositionRef.current); | ||||||||||||||||
| setValue(cutValue); | ||||||||||||||||
| if (!compositionRef.current) { | ||||||||||||||||
| setCountValue(cutValue); | ||||||||||||||||
| } | ||||||||||||||||
|
Comment on lines
+110
to
+112
Contributor
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. |
||||||||||||||||
|
|
||||||||||||||||
| resolveOnChange(e.currentTarget, e, onChange, cutValue); | ||||||||||||||||
| }; | ||||||||||||||||
|
|
@@ -107,6 +119,7 @@ const TextArea = React.forwardRef<TextAreaRef, TextAreaProps>( | |||||||||||||||
| HTMLTextAreaElement | ||||||||||||||||
| > = (e) => { | ||||||||||||||||
| compositionRef.current = true; | ||||||||||||||||
| setCountValue(formatValue); | ||||||||||||||||
| onCompositionStart?.(e); | ||||||||||||||||
|
Comment on lines
121
to
123
Contributor
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. |
||||||||||||||||
| }; | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
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.
Instead of maintaining a separate
countValuestate and syncing it withformatValueviauseEffect, we can derivecountValuedirectly during render. By tracking only thecompositionStartValue(the value when IME composition starts), we can simplify the state logic, eliminate theuseEffectentirely, and avoid potential double-renders or out-of-sync issues.