-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Fix FormTokenField rendering #14819
Merged
Merged
Fix FormTokenField rendering #14819
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,13 +48,20 @@ class FormTokenField extends Component { | |
this.onInputChange = this.onInputChange.bind( this ); | ||
this.bindInput = this.bindInput.bind( this ); | ||
this.bindTokensAndInput = this.bindTokensAndInput.bind( this ); | ||
this.updateSuggestions = this.updateSuggestions.bind( this ); | ||
} | ||
|
||
componentDidUpdate() { | ||
componentDidUpdate( prevProps ) { | ||
// Make sure to focus the input when the isActive state is true. | ||
if ( this.state.isActive && ! this.input.hasFocus() ) { | ||
this.input.focus(); | ||
} | ||
|
||
const { suggestions, value } = this.props; | ||
const suggestionsDidUpdate = suggestions !== prevProps.suggestions; | ||
if ( suggestionsDidUpdate || value !== prevProps.value ) { | ||
this.updateSuggestions( suggestionsDidUpdate ); | ||
} | ||
} | ||
|
||
static getDerivedStateFromProps( props, state ) { | ||
|
@@ -193,38 +200,14 @@ class FormTokenField extends Component { | |
const separator = this.props.tokenizeOnSpace ? /[ ,\t]+/ : /[,\t]+/; | ||
const items = text.split( separator ); | ||
const tokenValue = last( items ) || ''; | ||
const inputHasMinimumChars = tokenValue.trim().length > 1; | ||
const matchingSuggestions = this.getMatchingSuggestions( tokenValue ); | ||
const hasVisibleSuggestions = inputHasMinimumChars && !! matchingSuggestions.length; | ||
|
||
if ( items.length > 1 ) { | ||
this.addNewTokens( items.slice( 0, -1 ) ); | ||
} | ||
|
||
this.setState( { | ||
incompleteTokenValue: tokenValue, | ||
selectedSuggestionIndex: -1, | ||
selectedSuggestionScroll: false, | ||
isExpanded: false, | ||
} ); | ||
this.setState( { incompleteTokenValue: tokenValue }, this.updateSuggestions ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technically we could avoid |
||
|
||
this.props.onInputChange( tokenValue ); | ||
|
||
if ( inputHasMinimumChars ) { | ||
this.setState( { | ||
isExpanded: hasVisibleSuggestions, | ||
} ); | ||
|
||
if ( !! matchingSuggestions.length ) { | ||
this.props.debouncedSpeak( sprintf( _n( | ||
'%d result found, use up and down arrow keys to navigate.', | ||
'%d results found, use up and down arrow keys to navigate.', | ||
matchingSuggestions.length | ||
), matchingSuggestions.length ), 'assertive' ); | ||
} else { | ||
this.props.debouncedSpeak( __( 'No results.' ), 'assertive' ); | ||
} | ||
} | ||
} | ||
|
||
handleDeleteKey( deleteToken ) { | ||
|
@@ -467,6 +450,38 @@ class FormTokenField extends Component { | |
return this.props.saveTransform( this.state.incompleteTokenValue ).length > 0; | ||
} | ||
|
||
updateSuggestions( resetSelectedSuggestion = true ) { | ||
const { incompleteTokenValue } = this.state; | ||
|
||
const inputHasMinimumChars = incompleteTokenValue.trim().length > 1; | ||
const matchingSuggestions = this.getMatchingSuggestions( incompleteTokenValue ); | ||
const hasMatchingSuggestions = matchingSuggestions.length > 0; | ||
|
||
const newState = { | ||
isExpanded: inputHasMinimumChars && hasMatchingSuggestions, | ||
}; | ||
if ( resetSelectedSuggestion ) { | ||
newState.selectedSuggestionIndex = -1; | ||
newState.selectedSuggestionScroll = false; | ||
} | ||
|
||
this.setState( newState ); | ||
|
||
if ( inputHasMinimumChars ) { | ||
const { debouncedSpeak } = this.props; | ||
|
||
const message = hasMatchingSuggestions ? | ||
sprintf( _n( | ||
'%d result found, use up and down arrow keys to navigate.', | ||
'%d results found, use up and down arrow keys to navigate.', | ||
matchingSuggestions.length | ||
), matchingSuggestions.length ) : | ||
__( 'No results.' ); | ||
|
||
debouncedSpeak( message, 'assertive' ); | ||
} | ||
} | ||
|
||
renderTokensAndInput() { | ||
const components = map( this.props.value, this.renderToken ); | ||
components.splice( this.getIndexOfInput(), 0, this.renderInput() ); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This probably updates more than you're expecting it to, at least based on the shape of
suggestions
being an array, and the array being generated as a new value on each render.Essentially, it comes down to:
Related: http://adripofjavascript.com/blog/drips/object-equality-in-javascript.html
At least in how this is used by
FlatTermSelector
, the array is a result ofArray#map
, so would be a new value on each render:gutenberg/packages/editor/src/components/post-taxonomies/flat-term-selector.js
Line 211 in 8960509
gutenberg/packages/editor/src/components/post-taxonomies/flat-term-selector.js
Line 229 in 8960509
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
Options:
@wordpress/is-shallow-equal
). There is a small performance overhead to this, if it's worth considering the costliness vs.updateSuggestions
(or whetherupdateSuggestions
must only be called when in-fact it changes)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.
Hey @aduth - thanks for the feedback, and sorry for the super late reply. I missed this comment (and only read the review one further down). 🤦♂
You are absolutely right, and I now implemented
isShallowEqual
to compare suggestions. 👍Is this PR all good now? (Build and tests are still pasing.) 🙂