Accessible, extensible, Autocomplete for React.js
npm install react-autocompleteAccessible, extensible, Autocomplete for React.js.
``jsx`
items={[
{ label: 'apple' },
{ label: 'banana' },
{ label: 'pear' }
]}
renderItem={(item, isHighlighted) =>
{item.label}
}
value={value}
onChange={(e) => value = e.target.value}
onSelect={(val) => value = val}
/>
Check out more examples and get stuck right in with the online editor.
`bash`
npm install --save react-autocomplete
`bash`
yarn add react-autocomplete
* Development: https://unpkg.com/react-autocomplete@1.8.0/dist/react-autocomplete.js
* Production: https://unpkg.com/react-autocomplete@1.8.0/dist/react-autocomplete.min.js
#### getItemValue: Functionitem: Any
Arguments:
Used to read the display value from each entry in items.
#### items: Array
The items to display in the dropdown menu
#### renderItem: Functionitem: Any, isHighlighted: Boolean, styles: Object
Arguments:
Invoked for each entry in items that also passes shouldItemRender tostyles
generate the render tree for each item in the dropdown menu. is
an optional set of styles that can be applied to improve the look/feel
of the items in the dropdown menu.
#### autoHighlight: Boolean (optional)true
Default value:
Whether or not to automatically highlight the top match in the dropdown
menu.
#### inputProps: Object (optional){}
Default value:
Props passed to props.renderInput. By default these props will be
applied to the element rendered by Autocomplete, unless youprops.renderInput
have specified a custom value for . Any propertiesHTMLInputElement
supported by can be specified, apart from theAutocomplete
following which are set by : value, autoComplete, role,inputProps
aria-autocomplete. is commonly used for (but not limited to)
placeholder, event handlers (onFocus, onBlur, etc.), autoFocus, etc..
#### isItemSelectable: Function (optional)function() { return true }
Default value:
Arguments: item: Any
Invoked when attempting to select an item. The return value is used to
determine whether the item should be selectable or not.
By default all items are selectable.
#### menuStyle: Object (optional)`
Default value:jsx`
{
borderRadius: '3px',
boxShadow: '0 2px 12px rgba(0, 0, 0, 0.1)',
background: 'rgba(255, 255, 255, 0.9)',
padding: '2px 0',
fontSize: '90%',
position: 'fixed',
overflow: 'auto',
maxHeight: '50%', // TODO: don't cheat, let it flow to the bottom
}
Styles that are applied to the dropdown menu in the default renderMenurenderMenu
implementation. If you override and you want to usemenuStyle you must manually apply them (this.props.menuStyle).
#### onChange: Function (optional)function() {}
Default value:
Arguments: event: Event, value: String
Invoked every time the user changes the input's value.
#### onMenuVisibilityChange: Function (optional)function() {}
Default value:
Arguments: isOpen: Boolean
Invoked every time the dropdown menu's visibility changes (i.e. every
time it is displayed/hidden).
#### onSelect: Function (optional)function() {}
Default value:
Arguments: value: String, item: Any
Invoked when the user selects an item from the dropdown menu.
#### open: Boolean (optional)onMenuVisibilityChange
Used to override the internal logic which displays/hides the dropdown
menu. This is useful if you want to force a certain state based on your
UX/business logic. Use it together with for
fine-grained control over the dropdown menu dynamics.
#### renderInput: Function (optional)`
Default value:jsx`
function(props) {
return
}
Arguments: props: Object
Invoked to generate the input element. The props argument is the resultprops.inputProps
of merging with a selection of props that are requiredprops.ref
both for functionality and accessibility. At the very least you need to
apply and all props.on event handlers. Failing to doAutocomplete
this will cause to behave unexpectedly.
#### renderMenu: Function Arguments: Invoked to generate the render tree for the dropdown menu. Ensure the #### Whether or not to automatically select the highlighted item when the #### Invoked for each entry in #### The function which is used to sort #### The value to display in the input field #### Props that are applied to the element which wraps the #### This is a shorthand for In addition to the props there is an API available on the mounted element which is similar to that of Run them: Write them: Check your work: (optional)`
Default value:jsx
function(items, value, style) {
return
}
`items: Arrayitems
returned tree includes every entry in or else the highlight orderstyles
and keyboard navigation logic will break. will containselectOnBlur: Boolean
{ top, left, minWidth } which are the coordinates of the top-left corner
and the width of the dropdown menu. (optional)false
Default value: loses focus.shouldItemRender: Function (optional)item: Any, value: String
Arguments: items and its return value is used tosortItems: Function
determine whether or not it should be displayed in the dropdown menu.
By default all items are always rendered. (optional)itemA: Any, itemB: Any, value: String
Arguments: items before display.value: Any (optional)''
Default value: wrapperProps: Object (optional){}
Default value: andAutocomplete
dropdown menu elements rendered by .wrapperStyle: Object (optional)`
Default value:jsx`
{
display: 'inline-block'
}wrapperProps={{ style: .wrapperStyle
Note that is applied before wrapperProps, so the latterstyle
will win if it contains a entry.HTMLInputElement$3
. In other words: you can access most of the common methods directly on an Autocomplete instance. An example:`jsx`
class MyComponent extends Component {
componentDidMount() {
// Focus the input and select "world"
this.input.focus()
this.input.setSelectionRange(6, 11)
}
render() {
return (
value="hello world"
...
/>
)
}
}npm startDevelopment
You can start a local development environment with . This command starts a static file server on localhost:8080 which serves the examples in examples/. Hot-reload mechanisms are in place which means you don't have to refresh the page or restart the build for changes to take effect.npm testTests!
lib/__tests__/Autocomplete-test.jsnpm run coveragenpm run Scripts
Run with