import {InlineAutocomplete} from '@primer/react/drafts'
The InlineAutocomplete
component extends an Input
or Textarea
component to provide inline suggestions, similar to those provided by a code editor.
Input components must always be accompanied by a corresponding label to improve support for assistive technologies. Examples below are provided for conciseness and may not reflect accessibility best practices.
InlineAutocomplete
can be used with the FormControl
component to render a corresponding label.
Try typing a #
symbol to see suggestions. Use Enter
or click to apply a suggestion.
const options = ['javascript', 'typescript', 'css', 'html', 'webassembly']const SimpleExample = () => {const [suggestions, setSuggestions] = React.useState([])return (<InlineAutocompletetriggers={[{triggerChar: '#'}]}suggestions={suggestions}onShowSuggestions={({query}) => setSuggestions(options.filter(tag => tag.includes(query)))}onHideSuggestions={() => setSuggestions([])}><Textarea /></InlineAutocomplete>)}render(SimpleExample)
const options = ['javascript', 'typescript', 'css', 'html', 'webassembly']const SimpleExample = () => {const [suggestions, setSuggestions] = React.useState([])return (<InlineAutocompletetriggers={[{triggerChar: '#'}]}suggestions={suggestions}onShowSuggestions={({query}) => setSuggestions(options.filter(tag => tag.includes(query)))}onHideSuggestions={() => setSuggestions([])}><TextInput /></InlineAutocomplete>)}render(SimpleExample)
const options = ['javascript', 'typescript', 'css', 'html', 'webassembly']const SimpleExample = () => {const [suggestions, setSuggestions] = React.useState([])return (<FormControl><FormControl.Label>Example</FormControl.Label><InlineAutocompletetriggers={[{triggerChar: '#'}]}suggestions={suggestions}onShowSuggestions={({query}) => setSuggestions(options.filter(tag => tag.includes(query)))}onHideSuggestions={() => setSuggestions([])}><Textarea /></InlineAutocomplete></FormControl>)}render(SimpleExample)
Name | Type | Default | Description |
---|---|---|---|
children Required | React.ReactNode | An `input` or `textarea` compatible component to extend. A compatible component is any component that forwards a ref and props to an underlying `input` or `textarea` element, including but not limited to `Input`, `TextArea`, `input`, `textarea`, `styled.input`, and `styled.textarea`. If the child is not compatible, a runtime `TypeError` will be thrown. | |
triggers Required | Array<Trigger> | Register the triggers that can cause suggestions to appear. | |
onShowSuggestions Required | (event: ShowSuggestionsEvent) => void | Called when a valid suggestion query is updated. This should be handled by setting the `suggestions` prop accordingly. | |
onShowSuggestions Required | () => void | Called when suggestions should be hidden. Set `suggestions` to `null` or an empty array in this case. | |
suggestions Required | Suggestion[] | null | 'loading' | The currently visible list of suggestions. If `loading`, a loading indicator will be shown. If `null` or empty, the list will be hidden. Suggestion sort will be preserved. Typically, this should not contain more than five or so suggestions. | |
tabInsertsSuggestions | boolean | false | If `true`, suggestions will be applied with both `Tab` and `Enter`, instead of just `Enter`. This may be expected behavior for users used to IDEs, but use caution when hijacking browser tabbing capability. |
sx | SystemStyleObject | Style overrides to apply to the component. See also overriding styles. |