|
| 1 | +--- |
| 2 | +title: InlineAutocomplete |
| 3 | +componentId: inline_autocomplete |
| 4 | +status: Draft |
| 5 | +description: Provides inline auto completion suggestions for an input or textarea. |
| 6 | +source: https://github.com/primer/react/tree/main/src/InlineAutocomplete |
| 7 | +storybook: '/react/storybook?path=/story/forms-inlineautocomplete--default' |
| 8 | +--- |
| 9 | + |
| 10 | +```js |
| 11 | +import {InlineAutocomplete} from '@primer/react/drafts' |
| 12 | +``` |
| 13 | + |
| 14 | +The `InlineAutocomplete` component extends an `Input` or `Textarea` component to provide inline suggestions, similar to those provided by a code editor. |
| 15 | + |
| 16 | +## Examples |
| 17 | + |
| 18 | +<Note variant="warning"> |
| 19 | + |
| 20 | +Input components **must always** be accompanied by a corresponding label to improve support for assistive |
| 21 | +technologies. Examples below are provided for conciseness and may not reflect accessibility best practices. |
| 22 | + |
| 23 | +`InlineAutocomplete` can be used with the [`FormControl`](/FormControl) component to render a corresponding label. |
| 24 | + |
| 25 | +</Note> |
| 26 | + |
| 27 | +### Multi-line input |
| 28 | + |
| 29 | +Try typing a `#` symbol to see suggestions. Use `Enter` or click to apply a suggestion. |
| 30 | + |
| 31 | +```javascript live noinline drafts |
| 32 | +const options = ['javascript', 'typescript', 'css', 'html', 'webassembly'] |
| 33 | + |
| 34 | +const SimpleExample = () => { |
| 35 | + const [suggestions, setSuggestions] = React.useState([]) |
| 36 | + |
| 37 | + return ( |
| 38 | + <InlineAutocomplete |
| 39 | + triggers={[{triggerChar: '#'}]} |
| 40 | + suggestions={suggestions} |
| 41 | + onShowSuggestions={({query}) => setSuggestions(options.filter(tag => tag.includes(query)))} |
| 42 | + onHideSuggestions={() => setSuggestions([])} |
| 43 | + > |
| 44 | + <Textarea /> |
| 45 | + </InlineAutocomplete> |
| 46 | + ) |
| 47 | +} |
| 48 | + |
| 49 | +render(SimpleExample) |
| 50 | +``` |
| 51 | + |
| 52 | +### Single-line input |
| 53 | + |
| 54 | +```javascript live noinline drafts |
| 55 | +const options = ['javascript', 'typescript', 'css', 'html', 'webassembly'] |
| 56 | + |
| 57 | +const SimpleExample = () => { |
| 58 | + const [suggestions, setSuggestions] = React.useState([]) |
| 59 | + |
| 60 | + return ( |
| 61 | + <InlineAutocomplete |
| 62 | + triggers={[{triggerChar: '#'}]} |
| 63 | + suggestions={suggestions} |
| 64 | + onShowSuggestions={({query}) => setSuggestions(options.filter(tag => tag.includes(query)))} |
| 65 | + onHideSuggestions={() => setSuggestions([])} |
| 66 | + > |
| 67 | + <TextInput /> |
| 68 | + </InlineAutocomplete> |
| 69 | + ) |
| 70 | +} |
| 71 | + |
| 72 | +render(SimpleExample) |
| 73 | +``` |
| 74 | + |
| 75 | +### Labelled |
| 76 | + |
| 77 | +```javascript live noinline drafts |
| 78 | +const options = ['javascript', 'typescript', 'css', 'html', 'webassembly'] |
| 79 | + |
| 80 | +const SimpleExample = () => { |
| 81 | + const [suggestions, setSuggestions] = React.useState([]) |
| 82 | + |
| 83 | + return ( |
| 84 | + <FormControl> |
| 85 | + <FormControl.Label>Example</FormControl.Label> |
| 86 | + <InlineAutocomplete |
| 87 | + triggers={[{triggerChar: '#'}]} |
| 88 | + suggestions={suggestions} |
| 89 | + onShowSuggestions={({query}) => setSuggestions(options.filter(tag => tag.includes(query)))} |
| 90 | + onHideSuggestions={() => setSuggestions([])} |
| 91 | + > |
| 92 | + <Textarea /> |
| 93 | + </InlineAutocomplete> |
| 94 | + </FormControl> |
| 95 | + ) |
| 96 | +} |
| 97 | + |
| 98 | +render(SimpleExample) |
| 99 | +``` |
| 100 | + |
| 101 | +## Props |
| 102 | + |
| 103 | +<PropsTable> |
| 104 | + <PropsTableRow |
| 105 | + name="children" |
| 106 | + required |
| 107 | + type="React.ReactNode" |
| 108 | + description="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." |
| 109 | + /> |
| 110 | + <PropsTableRow |
| 111 | + name="triggers" |
| 112 | + required |
| 113 | + type="Array<Trigger>" |
| 114 | + description="Register the triggers that can cause suggestions to appear." |
| 115 | + /> |
| 116 | + <PropsTableRow |
| 117 | + name="onShowSuggestions" |
| 118 | + type="(event: ShowSuggestionsEvent) => void" |
| 119 | + required |
| 120 | + description="Called when a valid suggestion query is updated. This should be handled by setting the `suggestions` prop accordingly." |
| 121 | + /> |
| 122 | + <PropsTableRow |
| 123 | + name="onShowSuggestions" |
| 124 | + type="() => void" |
| 125 | + required |
| 126 | + description="Called when suggestions should be hidden. Set `suggestions` to `null` or an empty array in this case." |
| 127 | + /> |
| 128 | + <PropsTableRow |
| 129 | + name="suggestions" |
| 130 | + type="Suggestion[] | null | 'loading'" |
| 131 | + required |
| 132 | + description="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." |
| 133 | + /> |
| 134 | + <PropsTableRow |
| 135 | + name="tabInsertsSuggestions" |
| 136 | + type="boolean" |
| 137 | + defaultValue="false" |
| 138 | + description="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." |
| 139 | + /> |
| 140 | + <PropsTableSxRow /> |
| 141 | +</PropsTable> |
| 142 | + |
| 143 | +## Status |
| 144 | + |
| 145 | +<ComponentChecklist |
| 146 | + items={{ |
| 147 | + propsDocumented: false, |
| 148 | + noUnnecessaryDeps: true, |
| 149 | + adaptsToThemes: true, |
| 150 | + adaptsToScreenSizes: true, |
| 151 | + fullTestCoverage: false, |
| 152 | + usedInProduction: true, |
| 153 | + usageExamplesDocumented: false, |
| 154 | + hasStorybookStories: false, |
| 155 | + designReviewed: false, |
| 156 | + a11yReviewed: false, |
| 157 | + stableApi: false, |
| 158 | + addressedApiFeedback: false, |
| 159 | + hasDesignGuidelines: false, |
| 160 | + hasFigmaComponent: false |
| 161 | + }} |
| 162 | +/> |
0 commit comments