import { AutoComplete } from 'react-autocomplete-input-component';
<AutoComplete
list={[1, 'one', 2, 'two', 3, 'three']}
handleHighlight={(highlightedItem) => {
console.log(highlightedItem)
}}
onSelect={(selectedItem) => {
console.log(selectedItem)
}}
/>
npm install --save react-autocomplete-input-component
array
of the values to be searched for a match to the user inputgetDisplayValue
is needed iflist
array contains objects- can be passed in as an empty array and can always be changed dynamically
- only needed if
list
contains objects function
used to filter out the property value to be displayed in the dropdown
<AutoComplete
list={[
{ name: 'Tom', id: 3233 },
{ name: 'Tommy', id: 3445 },
{ name: 'Thomas', id: 3663 }
]}
getDisplayValue={(list) => {
return list.map((listItem) => listItem.name)
}}
/>
- callback function invoked when the
highlighted item
changes - its only argument is the
highlighted item's
value from the original list - if the
highlighted item
is a property value from an object, the whole object is passed in
- callback function invoked when an item is selected
- its only argument is the
selected item's
value from the original list - if the
selected item
is a property value from an object, the whole object is passed in
- callback function invoked in place of
onSelect
when there is no matching value for the text input - the input value is its only Argument
- if it is not passed in, the
onSelect
function will run with the text input being its only argument
import { AutoComplete } from 'react-autocomplete-input-component';
<AutoComplete
list={[1, 'one', 2, 'two', 3, 'three']}
onSelect={(selectedItem) => {
console.log(selectedItem)
}}
handleNewValue={(inputValue) => {
console.log(inputValue)
}}
/>
- used if new values are not permitted
- callback function invoked if
onSelect
fires when there is no match for the input value and thehandleNewValue
function is not passed in
- message displayed in the dropdown when there is no match for the current input value
default
- will show no message- if a string is passed in - it will be the message shown
<AutoComplete
onSelectError={() => window.alert("TRY AGAIN")}
noMatchMessage={"No matches found"}
/>
- can be used to control the position of the dropdown
true
opens the dropdown andfalse
closes the dropdown
- callback function invoked whenever dropdown is opened or closed
- its only argument is the current position of the dropdown
const [openDropDown, setOpenDropDown] = useState()
const toggleDropDown = () => {
setOpenDropDown(openDropDown ? false : true)
}
return(
<>
<button className='ignore' onClick={toggleDropDown} />
<AutoComplete
onDropDownChange={(isOpen) => setOpenDropDown(isOpen)}
open={openDropDown}
/>
</>
)
false
(default) the dropdown closes when mouse is clicked outside of the auto-complete wrapper div- setting to
true
will disable the feature - to ignore a specific element give the element a
className
ofignore
- Sets HTMLInputElement properties with some exceptions
- autocomplete, onChange, onKeyDown, onFocus cannot be overridden
<AutoComplete
inputProps={{
placeholder: "search...",
onMouseOver: () => setOpenDropDown(true)
}}
showAll={true}
highlightFirstItem={false}
/>
false
(default) dropdown doesn't appear until input value matches an item's prefixtrue
- If the input is focused and empty the dropdown displays all list items
true
(default) - automatically highlights first item in dropdownfalse
- highlight is hidden until arrow key is pressed or hover with mouse
- can be used with
controlSubmit
to only fireonSelect
orhandleNewValue
when passed in astrue
- when set to
true
,onSelect
andhandleNewValue
will only fire when submit is passed in astrue
const [submit, setSubmit] = useState(false);
const toggleSubmit = (() => {
setSubmit(true)
})
return(
<>
<button className='ignore' onClick={toggleSubmit}>SUBMIT</button>
<AutoComplete
controlSubmit={true}
submit={submit}
onSelect={(selectedItem) => {
console.log(selectedItem)
setSubmit(false)
}}
/>
</>
)
- Style Object for the
div
wrapping the whole component - CSS can also be used with the class name
autocomplete-wrapper
- Style Object for the
input
element - CSS can also be used with the class name
autocomplete-input
- Style Object for the dropdown container
div
- CSS can also be used with the class name
dropdown-container
- Style Object for each
item div
in the dropdown - CSS can also be used with the class name
dropdown-item
- Style Object for the
highlighted item
- CSS can also be used with the class name
highlighted-item
<AutoComplete
highlightedItemStyle={{
backgroundColor: "dodgerBlue"
}}
listItemStyle={{
cursor: "pointer",
padding: "5px"
}}
dropDownStyle={{
backgroundColor: "antiquewhite",
width: "215px"
}}
/>
Add to the tests: src/AutoComplete.test.js
Run the tests: npm test \
In the project directory, you can run:
Runs the app in the development mode.
Open http://localhost:3000 to view it in the browser.
The page will reload if you make edits.
You will also see any lint errors in the console.
Launches the test runner in the interactive watch mode.