-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Prevent autocompleter from letting users insert two More blocks #7166
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,13 @@ const { ENTER, ESCAPE, UP, DOWN, LEFT, RIGHT, SPACE } = keycodes; | |
* @returns {string[]} list of key words to search. | ||
*/ | ||
|
||
/** | ||
* @callback FnIsOptionDisabled | ||
* @param {CompleterOption} option a completer option. | ||
* | ||
* @returns {string[]} whether or not the given option is disabled. | ||
*/ | ||
|
||
/** | ||
* @callback FnGetOptionLabel | ||
* @param {CompleterOption} option a completer option. | ||
|
@@ -92,6 +99,7 @@ const { ENTER, ESCAPE, UP, DOWN, LEFT, RIGHT, SPACE } = keycodes; | |
* @property {String} triggerPrefix the prefix that will display the menu. | ||
* @property {(CompleterOption[]|FnGetOptions)} options the completer options or a function to get them. | ||
* @property {?FnGetOptionKeywords} getOptionKeywords get the keywords for a given option. | ||
* @property {?FnIsOptionDisabled} isOptionDisabled get whether or not the given option is disabled. | ||
* @property {FnGetOptionLabel} getOptionLabel get the label for a given option. | ||
* @property {?FnAllowNode} allowNode filter the allowed text nodes in the autocomplete. | ||
* @property {?FnAllowContext} allowContext filter the context under which the autocomplete activates. | ||
|
@@ -242,6 +250,10 @@ export class Autocomplete extends Component { | |
const { open, range, query } = this.state; | ||
const { getOptionCompletion } = open || {}; | ||
|
||
if ( option.isDisabled ) { | ||
return; | ||
} | ||
|
||
this.reset(); | ||
|
||
if ( getOptionCompletion ) { | ||
|
@@ -345,6 +357,7 @@ export class Autocomplete extends Component { | |
value: optionData, | ||
label: completer.getOptionLabel( optionData ), | ||
keywords: completer.getOptionKeywords ? completer.getOptionKeywords( optionData ) : [], | ||
isDisabled: completer.isOptionDisabled ? completer.isOptionDisabled( optionData ) : false, | ||
} ) ); | ||
|
||
const filteredOptions = filterOptions( this.state.search, keyedOptions ); | ||
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. It may be a pathological case, but since we show a limited number of options at a time, it is technically possible for all shown options to be disabled. Maybe we should update 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. Really like this idea, but let's punt on it until disabled items are a more common occurrence. Right now it's really only the More block case that we have to support. |
||
|
@@ -604,6 +617,7 @@ export class Autocomplete extends Component { | |
id={ `components-autocomplete-item-${ instanceId }-${ option.key }` } | ||
role="option" | ||
aria-selected={ index === selectedIndex } | ||
disabled={ option.isDisabled } | ||
className={ classnames( 'components-autocomplete__result', className, { | ||
'is-selected': index === selectedIndex, | ||
} ) } | ||
|
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.
I wondered why this was necessary due to the button actually being disabled. Now I see it is due to the fact that our keyboard listener calls
select
with indiscriminately when ENTER is pressed. I wonder if it would be clearer to conditionally callselect
inhandleKeyDown
. Just sharing my experience in case you concur.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.
Yeah, we re-use
select()
in both thekeyDown
andonClick
handlers. I think it's OK to leave as is.