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

FormTokenField: Fix duplicate input in IME composition #45607

Merged
merged 5 commits into from
Nov 12, 2022
Merged
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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Bug Fix

- `FormTokenField`: Fix duplicate input in IME composition ([#45607](https://github.com/WordPress/gutenberg/pull/45607)).
- `Autocomplete`: Fix unexpected block insertion during IME composition ([#45510](https://github.com/WordPress/gutenberg/pull/45510)).

### Internal
Expand Down
18 changes: 13 additions & 5 deletions packages/components/src/form-token-field/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,18 @@ export function FormTokenField( props: FormTokenFieldProps ) {
function onKeyDown( event: KeyboardEvent ) {
let preventDefault = false;

if ( event.defaultPrevented ) {
if (
event.defaultPrevented ||
// Ignore keydowns from IMEs
event.nativeEvent.isComposing ||
// Workaround for Mac Safari where the final Enter/Backspace of an IME composition
// is `isComposing=false`, even though it's technically still part of the composition.
// These can only be detected by keyCode.
event.keyCode === 229
) {
return;
}
switch ( event.code ) {
switch ( event.key ) {
case 'Backspace':
preventDefault = handleDeleteKey( deleteTokenBeforeInput );
break;
Expand Down Expand Up @@ -213,9 +221,9 @@ export function FormTokenField( props: FormTokenFieldProps ) {

function onKeyPress( event: KeyboardEvent ) {
let preventDefault = false;
// TODO: replace to event.code;
switch ( event.charCode ) {
case 44: // Comma.

switch ( event.key ) {
case ',':
preventDefault = handleCommaKey();
break;
default:
Expand Down