Autocomplete example in README.md gives error
Is there an existing issue for this?
- [X] I have searched the existing issues
Have you tried deactivating all plugins except Gutenberg?
- [X] I have tested with all plugins deactivated.
Have you tried replicating the bug using a default theme e.g. Twenty Twenty?
- [X] I have tested with a default theme.
Description
The code in the Usage section of the Autocomplete component raises TypeError: e is undefined.
Here are all of the Wordpress packages I am using:
{
"@wordpress/api-fetch": "^4.0.0",
"@wordpress/block-editor": "^5.2.2",
"@wordpress/block-library": "^3.2.8",
"@wordpress/blocks": "^6.25.2",
"@wordpress/data": "^4.27.3",
"@wordpress/i18n": "^3.17.0"
}
Step-by-step reproduction instructions
Paste the code snippet below into any block.
Expected Behavior
An autocomplete field appears and is usable.
Current Behavior
TypeError: e is undefined
$ http://localhost:10008/wp-includes/js/dist/rich-text.min.js?ver=dc66b38a90bdf10456e113646934eb2f:2
e http://localhost:10008/wp-includes/js/dist/components.min.js?ver=05cdf30cf2623cd4539a5c19832b0114:7
we http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:84
zj http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:226
Th http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:152
tj http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:152
Te http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:146
Pg http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:61
unstable_runWithPriority http://localhost:10008/wp-includes/js/dist/vendor/react.min.js?ver=16.13.1:25
Da http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:60
react-dom.min.js:125:24
Me http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:125
callback http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:139
Wg http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:67
oj http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:127
Aj http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:160
unstable_runWithPriority http://localhost:10008/wp-includes/js/dist/vendor/react.min.js?ver=16.13.1:25
Da http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:60
ab http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:154
Te http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:146
Pg http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:61
unstable_runWithPriority http://localhost:10008/wp-includes/js/dist/vendor/react.min.js?ver=16.13.1:25
Da http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:60
Pg http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:61
ha http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:60
Ja http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:224
ch http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:93
e http://localhost:10008/wp-includes/js/dist/data.min.js?ver=943087ae96d075f126df689839bb96b9:2
t http://localhost:10008/wp-includes/js/dist/data.min.js?ver=943087ae96d075f126df689839bb96b9:2
w http://localhost:10008/wp-includes/js/dist/data.min.js?ver=943087ae96d075f126df689839bb96b9:2
p http://localhost:10008/wp-includes/js/dist/data.min.js?ver=943087ae96d075f126df689839bb96b9:2
e http://localhost:10008/wp-admin/post.php?post=2&action=edit:1
s Redux
M http://localhost:10008/wp-includes/js/dist/data.min.js?ver=943087ae96d075f126df689839bb96b9:2
U http://localhost:10008/wp-includes/js/dist/data.min.js?ver=943087ae96d075f126df689839bb96b9:2
dispatch http://localhost:10008/wp-includes/js/dist/data.min.js?ver=943087ae96d075f126df689839bb96b9:2
Redux 41
M http://localhost:10008/wp-includes/js/dist/data.min.js?ver=943087ae96d075f126df689839bb96b9:2
U http://localhost:10008/wp-includes/js/dist/data.min.js?ver=943087ae96d075f126df689839bb96b9:2
d http://localhost:10008/wp-includes/js/dist/data.min.js?ver=943087ae96d075f126df689839bb96b9:2
Gc http://localhost:10008/wp-includes/js/dist/editor.min.js?ver=dbf1015c024ecc3b432a2a5d7898e51d:12
Bh http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:126
oj http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:126
Aj http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:160
unstable_runWithPriority http://localhost:10008/wp-includes/js/dist/vendor/react.min.js?ver=16.13.1:25
Da http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:60
ab http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:154
Te http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:146
Pg http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:61
unstable_runWithPriority http://localhost:10008/wp-includes/js/dist/vendor/react.min.js?ver=16.13.1:25
Da http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:60
Pg http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:61
ha http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:60
Ja http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:224
ch http://localhost:10008/wp-includes/js/dist/vendor/react-dom.min.js?ver=16.13.1:93
e http://localhost:10008/wp-includes/js/dist/data.min.js?ver=943087ae96d075f126df689839bb96b9:2
t http://localhost:10008/wp-includes/js/dist/data.min.js?ver=943087ae96d075f126df689839bb96b9:2
w http://localhost:10008/wp-includes/js/dist/data.min.js?ver=943087ae96d075f126df689839bb96b9:2
p http://localhost:10008/wp-includes/js/dist/data.min.js?ver=943087ae96d075f126df689839bb96b9:2
e http://localhost:10008/wp-admin/post.php?post=2&action=edit:1
s Redux
M http://localhost:10008/wp-includes/js/dist/data.min.js?ver=943087ae96d075f126df689839bb96b9:2
U http://localhost:10008/wp-includes/js/dist/data.min.js?ver=943087ae96d075f126df689839bb96b9:2
dispatch http://localhost:10008/wp-includes/js/dist/data.min.js?ver=943087ae96d075f126df689839bb96b9:2
Screenshots or screen recording (optional)
No response
Code snippet (optional)
<Autocomplete completers={[
{
name: 'fruit',
// The prefix that triggers this completer
triggerPrefix: '~',
// The option data
options: [
{ visual: '🍎', name: 'Apple', id: 1 },
{ visual: '🍊', name: 'Orange', id: 2 },
{ visual: '🍇', name: 'Grapes', id: 3 },
],
// Returns a label for an option like "🍊 Orange"
getOptionLabel: option => (
<span>
<span className="icon" >{ option.visual }</span>{ option.name }
</span>
),
// Declares that options should be matched by their name
getOptionKeywords: option => [ option.name ],
// Declares that the Grapes option is disabled
isOptionDisabled: option => option.name === 'Grapes',
// Declares completions should be inserted as abbreviations
getOptionCompletion: option => (
<abbr title={ option.name }>{ option.visual }</abbr>
),
}
]}>
{ ( { isExpanded, listBoxId, activeId } ) => (
<div
contentEditable
suppressContentEditableWarning
aria-autocomplete="list"
aria-expanded={ isExpanded }
aria-owns={ listBoxId }
aria-activedescendant={ activeId }
>
</div>
) }
</Autocomplete>
WordPress Information
Version | 5.7.2 (Latest version: 5.8)
Site Language | en_US
User Language | en_US
Timezone | +00:00
Home URL | http://localhost:10008
Site URL | http://localhost:10008
Permalink structure | /%postname%/
Is this site using HTTPS? | No
Is this a multisite? | No
Can anyone register on this site? | No
Is this site discouraging search engines? | No
Default comment status | Open
Environment type | local
User count | 4
Communication with WordPress.org | WordPress.org is reachable
Gutenberg Information
9.9
What browsers are you seeing the problem on?
Firefox, Chrome
Device Information
Desktop
Operating System Information
OS X 10.14.6
Confirmed. The component seems to be expecting a record, which I think is a text selection start/end, and as such is closely coupled with the RichText component.
I don't know if the intention is that it should be used in conjunction with useRichText, and as such the documentation needs updating, or that is can be used on its own and the component needs updating.
Noting that the error is in this code:
if ( isCollapsed( record ) ) {
textContent = getTextContent( slice( record, 0 ) );
}
Where record is undefined. The record value comes from the unstable component useRichText.
related: https://github.com/WordPress/gutenberg/issues/16624
Hey everyone! Thanks for reporting this. Potential update to the Autocomplete README is in the works in #49965.
TL;DR - Autocomplete is integrated with other packages/components in a way that don't make it the best fit for standalone usage. Instead, it is possible to filter the editor's available completers, much like @JordanPak describes above. And example is being added to the README in the pending PR.
Thanks for the clarification @chad1008
In case anyone is looking for a direct link to his reference, here it is. 😄
🤦 ...and thank you for saving us all from my copy/paste failure 😆
fixed in https://github.com/WordPress/gutenberg/pull/49965