File size: 1,502 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import { TextControl } from '@wordpress/components';
import { useMemo, useState } from 'react';
import Suggestions from '..';
export default function SuggestionsExample() {
const [ query, setQuery ] = useState( '' );
const suggestions = useMemo( () => {
if ( ! query ) {
return [];
}
const allSuggestions = [ 'Foo', 'Bar', 'Baz' ].map( ( s ) => ( { label: s } ) );
const r = new RegExp( query, 'i' );
return allSuggestions.filter( ( { label } ) => r.test( label ) );
}, [ query ] );
return (
<div className="docs__suggestions-container">
<div>
<TextControl
value={ query }
onChange={ setQuery }
autoComplete="off"
autoCorrect="off"
autoCapitalize="off"
spellCheck={ false }
placeholder="Type Foo, Bar or Baz…"
__nextHasNoMarginBottom
__next40pxDefaultSize
/>
<p>
<span role="img" aria-label="Warning">
⚠️
</span>
The above input is for demonstration. It is not part of the{ ' ' }
<code>{ '<Suggestions />' }</code> component.
</p>
<p>
<span role="img" aria-label="Tip">
💡
</span>
Check your browser console to observe the `suggest` callback.
</p>
</div>
<Suggestions
query={ query }
suggestions={ suggestions }
suggest={ ( ...args ) => {
// eslint-disable-next-line no-console
console.log( 'Suggest callback invoked with args: %o', args );
} }
/>
</div>
);
}
SuggestionsExample.displayName = 'SuggestionsExample';
|