Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 9 additions & 5 deletions packages/components/src/form-token-field/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -516,14 +516,18 @@ export function FormTokenField( props: FormTokenFieldProps ) {
match = match.normalize( 'NFKC' ).toLocaleLowerCase();

_suggestions.forEach( ( suggestion ) => {
const index = suggestion
const normalizedSuggestion = suggestion
.normalize( 'NFKC' )
.toLocaleLowerCase()
.indexOf( match );
.toLocaleLowerCase();
const normalizedSuggestionLabel = displayTransform( suggestion )
.normalize( 'NFKC' )
.toLocaleLowerCase();
const index = normalizedSuggestion.indexOf( match );
const labelIndex = normalizedSuggestionLabel.indexOf( match );
Comment thread
elazzabi marked this conversation as resolved.
Outdated
if ( normalizedValue.indexOf( suggestion ) === -1 ) {
if ( index === 0 ) {
if ( index === 0 || labelIndex === 0 ) {
startsWithMatch.push( suggestion );
} else if ( index > 0 ) {
} else if ( index > 0 || labelIndex > 0 ) {
containsMatch.push( suggestion );
}
}
Expand Down
3 changes: 3 additions & 0 deletions packages/components/src/form-token-field/suggestions-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ export function SuggestionsList<
.normalize( 'NFKC' )
.toLocaleLowerCase()
.indexOf( matchText );
if ( indexOfMatch < 0 ) {
return null;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What scenario is this new condition meant to handle?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this as an early fail mechanism. Otherwise, the subsequent check will be:

transformedSuggestion.substring(
	0,
	-1
)

Which might produce weird results.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but when would that actually happen, and why wasn't it a problem before?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, this would trigger when there is no match. I only added it because I thought it made sense, but it's true that it wasn't there before, and there were no known side effects. So let's drop it (cc1190b)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this would be a good example of a thing where it'd be good to have regression testing coverage covering the scenario if it were in-fact an issue.


return {
suggestionBeforeMatch: transformedSuggestion.substring(
Expand Down
7 changes: 4 additions & 3 deletions packages/dataviews/src/field-types/stories/index.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ const data: DataType[] = [
mediaWithElements:
'https://live.staticflickr.com/7398/9458193857_e1256123e3_z.jpg',
array: [ 'item1', 'item2', 'item3' ],
arrayWithElements: [ 'item1', 'item2', 'item3' ],
// Use short values here so it's easy to verify searching by value still works.
arrayWithElements: [ 'a', 'b', 'item3' ],
Comment thread
oandregal marked this conversation as resolved.
Outdated
notype: 'No type',
notypeWithElements: 'No type',
priceWithPrefix: '25.99',
Expand Down Expand Up @@ -461,8 +462,8 @@ const fields: Field< DataType >[] = [
label: 'Array (with elements)',
description: 'Help for array with elements.',
elements: [
{ value: 'item1', label: 'Item 1' },
{ value: 'item2', label: 'Item 2' },
{ value: 'a', label: 'Item 1' },
{ value: 'b', label: 'Item 2' },
{ value: 'item3', label: 'Item 3' },
],
},
Expand Down
Loading