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: use KeyboardEvent.code, refactor tests to model RTL and user-event #43442

Merged
merged 23 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
7b09c10
FormTokenField: use KeyboardEvent.code instead of KetboardEvent.keyCode
ciampo Aug 19, 2022
e132326
Rewrite tests in TypeScript, modern RTL and using user-event
ciampo Aug 20, 2022
2d259b6
Fixtures to TypeScript
ciampo Aug 22, 2022
c9d05ae
Use fixtures object instead of inline variables
ciampo Aug 22, 2022
f84a2ae
Expand suggestions only when focused
ciampo Aug 22, 2022
b7ac512
Basic suggestions-related unit tests
ciampo Aug 22, 2022
67944d3
Move suggestions up, split special token tests to their own describe …
ciampo Aug 22, 2022
b8c9542
Reuse existing logic instead of new focus-related ref
ciampo Aug 22, 2022
7e98d47
Transform suggestion tests, improve label test
ciampo Aug 22, 2022
4c316b1
Move special characters under displayTransform
ciampo Aug 22, 2022
2846d92
Move "white space" tests to different secions
ciampo Aug 22, 2022
43bbebf
Tokens as objects
ciampo Aug 22, 2022
bc4ad28
More unescaped values tests
ciampo Aug 22, 2022
211c379
Move fixtures values inline
ciampo Aug 22, 2022
258a4bc
__experimentalAutoSelectFirstMatch tests
ciampo Aug 22, 2022
728405e
Remove unnecessary old test files
ciampo Aug 22, 2022
16ac6f0
__experimentalRenderItem, messages
ciampo Aug 22, 2022
f656971
CHANGELOG
ciampo Aug 22, 2022
82db99b
Apply suggestions from code review
ciampo Aug 23, 2022
7584271
Update packages/components/src/form-token-field/test/index.tsx
ciampo Aug 23, 2022
8e262b8
Use initialValue instead of adding values manually, where possible
ciampo Aug 23, 2022
1fff738
Use expectVisibleSuggestionsToBe
ciampo Aug 23, 2022
9435de3
Add example with special characters
ciampo Aug 23, 2022
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
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