Autocomplete 组件用于为子输入组件提供自动完成支持,主要应用于 WordPress 区块编辑器。它通过配置 props 和 completer 接口来控制行为,允许开发者自定义自动完成逻辑。
( function () {
// Define the completer
const fruits = {
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 },
{ visual: '🥭', name: 'Mango', id: 4 },
{ visual: '🍓', name: 'Strawberry', id: 5 },
{ visual: '🫐', name: 'Blueberry', id: 6 },
{ visual: '🍒', name: 'Cherry', id: 7 },
],
// Returns a label for an option like "🍊 Orange"
getOptionLabel: ( option ) => `${ option.visual } ${ option.name }`,
// 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 ) => option.visual,
};
// Define a callback that will add the custom completer to the list of completers
function appendTestCompleters( completers, blockName ) {
return blockName === 'core/paragraph'
? [ ...completers, fruits ]
: completers;
}
// Trigger our callback on the `editor.Autocomplete.completers` hook
wp.hooks.addFilter(
'editor.Autocomplete.completers',
'fruit-test/fruits',
appendTestCompleters,
11
);
} )();This component is used to provide autocompletion support for a child input component.
The following props are used to control the behavior of the component.
The rich text value object the autocompleter is being applied to.
RichTextValueA function to be called when an option is selected to insert into the existing text.
( value: string ) => voidA function to be called when an option is selected to replace the existing text.
( values: RichTextValue[] ) => voidAn array of all of the completers to apply to the current element.
Array< WPCompleter >A ref containing the editable element that will serve as the anchor for Autocomplete‘s Popover.
MutableRefObject< HTMLElement | undefined >A function that returns nodes to be rendered within the Autocomplete.
FunctionWhether or not the Autocomplete component is selected, and if its Popover should be displayed.
BooleanAutocompleters enable us to offer users options for completing text input. For example, Gutenberg includes a user autocompleter that provides a list of user names and completes a selection with a user mention like @mary.
Each completer declares:
In addition, a completer may optionally declare:
The name of the completer. Useful for identifying a specific completer to be overridden via extensibility hooks. Each completer should have a unique name.
StringThe raw options for completion. May be an array, a function that returns an array, or a function that returns a promise for an array.
Options may be of any type or shape. The completer declares how those options are rendered and what their completions should be when selected.
Array|FunctionThe string prefix that should trigger the completer. For example, Gutenberg’s block completer is triggered when the ‘/’ character is entered.
StringA function that returns the label for a given option. A label may be a string or a mixed array of strings, elements, and components.
FunctionA function that returns the keywords for the specified option.
FunctionA function that returns whether or not the specified option should be disabled. Disabled options cannot be selected.
FunctionA function that takes an option and responds with how the option should be completed. By default, the result is a value to be inserted in the text. However, a completer may explicitly declare how a completion should be treated by returning an object with action and value properties. The action declares what should be done with the value.
There are currently two supported actions:
value into the text (the default completion action).value property.A function that takes a Range before and a Range after the autocomplete trigger and query text and returns a boolean indicating whether the completer should be considered for that context.
FunctionA class name to apply to the autocompletion popup menu.
StringWhether to debounce the autocompleter. This setting only affects the options argument; it is ignored by the useItems.
BooleanThe Autocomplete component is not currently intended to be used as a standalone component. It is used by other packages to provide autocompletion support for the block editor.
The block editor provides a separate, wrapped version of Autocomplete that supports the addition of custom completers via a filter.
To implement your own completer in the block editor you will:
1. Define the completer
2. Write a callback that will add your completer to the list of existing completers
3. Add a filter to the editor.Autocomplete.completers hook that will call your callback
The following example will add a contrived “fruits” autocompleter to the block editor. Note that in the callback it’s possible to limit this new completer to a specific block type. In this case, our “fruits” completer will only be available from the “core/paragraph” block type.
( function () {
// Define the completer
const fruits = {
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 },
{ visual: '🥭', name: 'Mango', id: 4 },
{ visual: '🍓', name: 'Strawberry', id: 5 },
{ visual: '🫐', name: 'Blueberry', id: 6 },
{ visual: '🍒', name: 'Cherry', id: 7 },
],
// Returns a label for an option like "🍊 Orange"
getOptionLabel: ( option ) => `${ option.visual } ${ option.name }`,
// 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 ) => option.visual,
};
// Define a callback that will add the custom completer to the list of completers
function appendTestCompleters( completers, blockName ) {
return blockName === 'core/paragraph'
? [ ...completers, fruits ]
: completers;
}
// Trigger our callback on the `editor.Autocomplete.completers` hook
wp.hooks.addFilter(
'editor.Autocomplete.completers',
'fruit-test/fruits',
appendTestCompleters,
11
);
} )();
Finally, enqueue your JavaScript file as you would any other, as in the following plugin example:
<?php
/**
* Plugin Name: Fruit Autocompleter
* Plugin URI: https://github.com/WordPress/gutenberg
* Author: Gutenberg Team
*/
/**
* Registers a custom script for the plugin.
*/
function enqueue_fruit_autocompleter_plugin_script() {
wp_enqueue_script(
'fruit-autocompleter',
plugins_url( '/index.js', __FILE__ ),
array(
'wp-hooks',
),
);
}
add_action( 'init', 'enqueue_fruit_autocompleter_plugin_script' );