README
react-autocomplete-input
Autocomplete input field for React
Demo
Demo and playground are available here
Usage Example
import TextInput from 'react-autocomplete-input';
import 'react-autocomplete-input/dist/bundle.css';
<TextInput options={["apple", "apricot", "banana", "carrot"]} />
Features
- Supports both keyboard and mouse for option selection
- Supports responsiveness and works on every device
- Supports lazy-loading and dynamic option list updates
- Supports all major browsers including IE 8+
Configurable Props
Note: All props are optional.
Component : string or func
"textarea"
Default value: Widget for rendering input field
defaultValue : string
""
Default value: Initial text for input
disabled : boolean
false
Default value: Disables widget, i.e. during form submission
maxOptions : number
6
Default value: Defines how many options can be listed simultaneously. Show all matched options if maxOptions equals 0.
onRequestOptions : func
() => {}
Default value: Callback for requesting new options to support lazy-loading. If requestOnlyIfNoOptions
is true, then onRequestOptions
called only if no options are currently available. Otherwise onRequestOptions
is called every time text is changed and trigger
is found.
import React from 'react';
import TextInput from 'react-autocomplete-input';
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.handleRequestOptions = this.handleRequestOptions.bind(this);
this.state = { options: ["apple", "apricot", "banana", "carror"] };
}
// text in input is "I want @ap"
handleRequestOptions(part) {
console.log(part); // -> "ap", which is part after trigger "@"
this.setState({ options: SOME_NEW_OPTION_ARRAY });
}
render() {
return <TextInput onRequestOptions={this.handleRequestOptions} options={this.state.options} />;
}
}
matchAny: boolean
Default value: false
If true, will match options in the middle of the word as well
offsetX: number
Default value: 0
Popup horizontal offset
offsetY: number
Default value: 0
Popup vertical offset
options : array
[]
Default value: List of available options for autocomplete
regex : string
^[a-zA-Z0-9_\-]+$
Default value: This regular expression checks if text after trigger
can be autocompleted or not. I.e. "@ap" matches the default regex as "ap" matches the regex, therefore library will try to find appropriate option. "@a$p" fails to match the regex as there is not "