Skip to content

Commit

Permalink
FormTokenField: use KeyboardEvent.code, refactor tests to model RTL a…
Browse files Browse the repository at this point in the history
…nd user-event (#43442)

* FormTokenField: use KeyboardEvent.code instead of KetboardEvent.keyCode

* Rewrite tests in TypeScript, modern RTL and using user-event

* Fixtures to TypeScript

* Use fixtures object instead of inline variables

* Expand suggestions only when focused

* Basic suggestions-related unit tests

* Move suggestions up, split special token tests to their own describe call

* Reuse existing logic instead of new focus-related ref

* Transform suggestion tests, improve label test

* Move special characters under displayTransform

* Move "white space" tests to different secions

* Tokens as objects

* More unescaped values tests

* Move fixtures values inline

* __experimentalAutoSelectFirstMatch tests

* Remove unnecessary old test files

* __experimentalRenderItem, messages

* CHANGELOG

* Apply suggestions from code review

Co-authored-by: Marin Atanasov <[email protected]>

* Update packages/components/src/form-token-field/test/index.tsx

Co-authored-by: Marin Atanasov <[email protected]>

* Use initialValue instead of adding values manually, where possible

* Use expectVisibleSuggestionsToBe

* Add example with special characters

Co-authored-by: Marin Atanasov <[email protected]>
  • Loading branch information
ciampo and tyxla authored Aug 23, 2022
1 parent 567081f commit 69d2e59
Show file tree
Hide file tree
Showing 6 changed files with 2,124 additions and 626 deletions.
2 changes: 1 addition & 1 deletion packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
- `NavigableMenu`: Refactor away from `_.includes()` ([#43518](https://github.com/WordPress/gutenberg/pull/43518/)).
- `Tooltip`: Refactor away from `_.includes()` ([#43518](https://github.com/WordPress/gutenberg/pull/43518/)).
- `TreeGrid`: Refactor away from `_.includes()` ([#43518](https://github.com/WordPress/gutenberg/pull/43518/)).

- `FormTokenField`: use `KeyboardEvent.code`, refactor tests to modern RTL and `user-event` ([#43442](https://github.com/WordPress/gutenberg/pull/43442/)).

### Experimental

Expand Down
40 changes: 17 additions & 23 deletions packages/components/src/form-token-field/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,6 @@ import { useEffect, useRef, useState } from '@wordpress/element';
import { __, _n, sprintf } from '@wordpress/i18n';
import { useDebounce, useInstanceId, usePrevious } from '@wordpress/compose';
import { speak } from '@wordpress/a11y';
import {
BACKSPACE,
ENTER,
UP,
DOWN,
LEFT,
RIGHT,
SPACE,
DELETE,
ESCAPE,
} from '@wordpress/keycodes';
import isShallowEqual from '@wordpress/is-shallow-equal';

/**
Expand Down Expand Up @@ -127,6 +116,11 @@ export function FormTokenField( props: FormTokenFieldProps ) {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ incompleteTokenValue ] );

useEffect( () => {
updateSuggestions();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ __experimentalAutoSelectFirstMatch ] );

if ( disabled && isActive ) {
setIsActive( false );
setIncompleteTokenValue( '' );
Expand Down Expand Up @@ -179,35 +173,34 @@ export function FormTokenField( props: FormTokenFieldProps ) {
if ( event.defaultPrevented ) {
return;
}
// TODO: replace to event.code;
switch ( event.keyCode ) {
case BACKSPACE:
switch ( event.code ) {
case 'Backspace':
preventDefault = handleDeleteKey( deleteTokenBeforeInput );
break;
case ENTER:
case 'Enter':
preventDefault = addCurrentToken();
break;
case LEFT:
case 'ArrowLeft':
preventDefault = handleLeftArrowKey();
break;
case UP:
case 'ArrowUp':
preventDefault = handleUpArrowKey();
break;
case RIGHT:
case 'ArrowRight':
preventDefault = handleRightArrowKey();
break;
case DOWN:
case 'ArrowDown':
preventDefault = handleDownArrowKey();
break;
case DELETE:
case 'Delete':
preventDefault = handleDeleteKey( deleteTokenAfterInput );
break;
case SPACE:
case 'Space':
if ( tokenizeOnSpace ) {
preventDefault = addCurrentToken();
}
break;
case ESCAPE:
case 'Escape':
preventDefault = handleEscapeKey( event );
break;
default:
Expand Down Expand Up @@ -533,8 +526,9 @@ export function FormTokenField( props: FormTokenFieldProps ) {
getMatchingSuggestions( incompleteTokenValue );
const hasMatchingSuggestions = matchingSuggestions.length > 0;

const shouldExpandIfFocuses = hasFocus() && __experimentalExpandOnFocus;
setIsExpanded(
__experimentalExpandOnFocus ||
shouldExpandIfFocuses ||
( inputHasMinimumChars && hasMatchingSuggestions )
);

Expand Down
Loading

0 comments on commit 69d2e59

Please sign in to comment.