0.0.2 • Published 2 years ago
reactjs-autocomplete v0.0.2
React Autocomplete Input Component
import { AutoComplete } from 'reactjs-autocomplete';
<AutoComplete
list={[1, 'one', 2, 'two', 3, 'three']}
handleHighlight={(highlightedItem) => {
console.log(highlightedItem)
}}
onSelect={(selectedItem) => {
console.log(selectedItem)
}}
/>Demo
Install
npm
npm install --save reactjs-autocompleteProps
list: array
arrayof the values to be searched for a match to the user inputgetDisplayValueis needed iflistarray contains objects- can be passed in as an empty array and can always be changed dynamically
getDisplayValue: function (Optional)
- only needed if
listcontains objects functionused 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)
}}
/>handleHighlight: function (Optional)
- callback function invoked when the
highlighted itemchanges - its only argument is the
highlighted item'svalue from the original list - if the
highlighted itemis a property value from an object, the whole object is passed in
onSelect: function (Optional)
- callback function invoked when an item is selected
- its only argument is the
selected item'svalue from the original list - if the
selected itemis a property value from an object, the whole object is passed in
handleNewValue: function (Optional)
- callback function invoked in place of
onSelectwhen there is no matching value for the text input - the input value is its only Argument
- if it is not passed in, the
onSelectfunction will run with the text input being its only argument
import { AutoComplete } from 'reactjs-autocomplete';
<AutoComplete
list={[1, 'one', 2, 'two', 3, 'three']}
onSelect={(selectedItem) => {
console.log(selectedItem)
}}
handleNewValue={(inputValue) => {
console.log(inputValue)
}}
/>onSelectError: function (Optional)
- used if new values are not permitted
- callback function invoked if
onSelectfires when there is no match for the input value and thehandleNewValuefunction is not passed in - the input value is its only argument
noMatchMessage: string (Optional)
- 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"}
/>open : boolean (Optional)
- can be used to control the position of the dropdown
trueopens the dropdown andfalsecloses the dropdown
onDropDownChange: function (Optional)
- 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}
/>
</>
)disableOutsideClick : boolean (Optional)
false(default) the dropdown closes when mouse is clicked outside of the auto-complete wrapper div- setting to
truewill disable the feature - to ignore a specific element give the element a
classNameofignore
inputProps: Object (Optional)
- Sets HTMLInputElement properties with some exceptions
- autocomplete, onChange, onKeyDown, onFocus cannot be overridden
<AutoComplete
inputProps={{
placeholder: "search...",
onMouseOver: () => setOpenDropDown(true)
}}
showAll={true}
highlightFirstItem={false}
/>showAll: boolean (Optional)
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
highlightFirstItem: boolean (Optional)
true(default) - automatically highlights first item in dropdownfalse- highlight is hidden until arrow key is pressed or hover with mouse
submit : boolean (Optional)
- can be used with
controlSubmitto only fireonSelectorhandleNewValuewhen passed in astrue
controlSubmit: boolean (Optional)
- when set to
true,onSelectandhandleNewValuewill 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)
}}
/>
</>
)wrapperStyle: Object (Optional)
- Style Object for the
divwrapping the whole component - CSS can also be used with the class name
autocomplete-wrapper
inputStyle: Object (Optional)
- Style Object for the
inputelement - CSS can also be used with the class name
autocomplete-input
dropDownStyle: Object (Optional)
- Style Object for the dropdown container
div - CSS can also be used with the class name
dropdown-container
listItemStyle: Object (Optional)
- Style Object for each
item divin the dropdown - CSS can also be used with the class name
dropdown-item
highlightedItemStyle: Object (Optional)
- 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"
}}
/>Tests
Add to the tests: src/AutoComplete.test.js \ Run the tests: npm test
Available Scripts
In the project directory, you can run:
npm start
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.
npm test
Launches the test runner in the interactive watch mode.