-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
FormTokenField: Fix duplicate input in IME composition #45607
Conversation
Open in CodeSandbox Web Editor | VS Code | VS Code Insiders |
|
||
switch ( event.key ) { | ||
case 'Comma': |
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.
Since there was a TODO for key events here, this was also addressed.
If it is unnecessary, I would like to revert it.
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.
I think the event.key
for the comma is ,
. See: https://www.toptal.com/developers/keycode
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.
Thanks for noticing.
You are correct, I have fixed it in 16190d6.
But it is strange that all tests have passed even though the value was wrong. Perhaps we should check for any missing tests as a follow-up to this PR.
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.
The "should add a token with the input's value when pressing the comma key" test should check for comma keypresses, maybe you could look a bit more into it and why it didn't flag a regression?
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.
I have tried various inputs to clarify this issue and have been unable to find a case where onKeyPress
occurs.
Also, the determination of the token by comma key appears to be controlled by this other event.
Is there any way to clarify what this event is provided for?
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.
Agreed, tokenization is also done with regex so the comma handling seems redundant.
const separator = tokenizeOnSpace ? /[ ,\t]+/ : /[,\t]+/; |
So it turns out the test is correct and it isn't a regression! We could bypass this entire onKeyPress handler and it wouldn't matter 😇
gutenberg/packages/components/src/form-token-field/index.tsx
Lines 214 to 223 in 8c9af51
function onKeyPress( event: KeyboardEvent ) { | |
let preventDefault = false; | |
// TODO: replace to event.code; | |
switch ( event.charCode ) { | |
case 44: // Comma. | |
preventDefault = handleCommaKey(); | |
break; | |
default: | |
break; | |
} |
Digging into the history, apparently the original code was copied from wp-calypso, and we can see that the regex was added after the keyPress comma handling was already in place, in the context of supporting copy-pasted strings. From that, I would guess that the redundancy is just an oversight. If anything, relying on the regex for commas is probably more reliable given that there was some weirdness with keyCode/charCode differences.
Co-authored-by: Lena Morita <[email protected]>
I accessed the local site where I built this PR from my iPhone and it seems to work as expected in Chrome on my iPhone. iphone.mp4 |
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.
Tested on macOS using Chrome, Safari, and Firefox with English/Georgian keyboards. I couldn't spot any regressions, FormTokenField
works as before.
It would be great to get confirmations from folks using other OS to backport this into a minor release.
Tested on Windows 10 using Chrome and Firefox with Japanese. It looks like work fine. |
Co-authored-by: Lena Morita <[email protected]>
To play it safe, I'm wondering if we should ship the hotfix simply as a revert to the Unfortunately I don't have a Virtualbox setup anymore and can't easily test for those environments. We might not be able to get testers before the deadline. |
I committed this suggestion because I thought the method suggested in this comment was the best so far. Considering that there is not much time left before the RC release, it may be safe to revert to the previous implementation. This is also true for #45626 regarding |
Cool, let's keep this PR to merge into trunk, and open a separate |
Thank you both for the feedback! I agree; that's probably the safest fix we can ship with the minor release. @t-hamano, do you have time to create a revert to the |
Yes, I'll work on them 👍 |
I had something to check. |
6.1 will work as well. Thanks! |
|
As for the FormTokenField, I will continue to look into this as the unit test is failing. |
Thank you all for working on a solution for this, really appreciate the collaboration. Excuses also for not having tested my |
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.
Retested on Mac Chrome/Firefox/Safari, and it works as expected.
I'm ok to merge this into trunk so we can get more field testing in the plugin track 👍
What?
This PR fixes a problem where duplicate values are set when characters are entered into
FormTokenField
in languages that use IME composition.Why?
This is because the
enter
key, which is used to confirm the entry of a character, is considered a token decision.For more context, see here.
How?
Replaced
event.code
withevent.key
.Testing Instructions
Screenshots or screencast
195cc8d319b16958d11e6fead9ac1a47.mp4