Gathering detailed insights and metrics for react-plain-datalist-input
Gathering detailed insights and metrics for react-plain-datalist-input
Gathering detailed insights and metrics for react-plain-datalist-input
Gathering detailed insights and metrics for react-plain-datalist-input
npm install react-plain-datalist-input
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
1 Stars
12 Commits
1 Watchers
9 Branches
1 Contributors
Updated on Mar 08, 2023
Latest Version
2.2.1
Package Id
react-plain-datalist-input@2.2.1
Unpacked Size
37.78 kB
Size
10.47 kB
File Count
5
NPM Version
8.1.0
Node Version
16.13.0
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
3
29
react-datalist-input now works with server-side rendering. So the plain version is no longer required. Please have a look at the react-datalist-input documentation to learn how to use it.
This package provides a single react component. The component contains an input field with a drop down menu to pick a possible option based on the current input as a react component.
This is the plain component without any styling. In most cases you want to use react-datalist-input
This version allows server-side rendering. I was having issues using the react-datalist-input package server-side (e.g. in a gatsby project) since the css & js was minified together in one file.
Just do it yourself by using the className props described below.
You can also make use of the classes already set by this component. Find a list of all set classes here:
div
has the class: datalist-input
input
field has the class: autocomplete-input
div
dropdown container has the class: datalist-items
div
dropdown item has the class: datalist-active-item
Or copy-paste the default css from here.
Check it out on my personal website!
Feel free to get inspired and more importantly please provide your feedback on structure and style.
The documentation below mainly applies for both versions but will be updated based on version 2.x.x updates in the future.
Motivation: issue 23
Offer optional value prop, in case the user requires full control to change/clear the input value based on side effects
Changes:
initialValue
propvalue
prop instead (default undefined)clearOnClickInput
prop (default false)onClick
lifecycle method prop (default empty function)Changes:
useStateRef
to reduce re-renders and boost performance1npm i react-plain-datalist-input
1import React, { useState, useMemo, useCallback } from "react"; 2import DataListInput from "react-datalist-input"; 3 4const YourComponent = ({ myValues }) => { 5 // selectedItem 6 const [item, setItem] = useState(); 7 8 /** 9 * your callback function gets called if the user selects one option out of the drop down menu 10 * @param selectedItem object (the selected item / option) 11 */ 12 const onSelect = useCallback((selectedItem) => { 13 console.log("selectedItem", selectedItem); 14 }, []); 15 16 // the array you want to pass to the react-data-list component 17 // key and label are required properties 18 const items = useMemo( 19 () => 20 myValues.map((oneItem) => ({ 21 // required: what to show to the user 22 label: oneItem.name, 23 // required: key to identify the item within the array 24 key: oneItem.id, 25 // feel free to add your own app logic to access those properties in the onSelect function 26 someAdditionalValue: oneItem.someAdditionalValue, 27 // or just keep everything 28 ...oneItem, 29 })), 30 [myValues] 31 ); 32 33 return ( 34 <DataListInput 35 placeholder="Select an option from the drop down menu..." 36 items={items} 37 onSelect={onSelect} 38 /> 39 ); 40};
Prop | Type | Required/Optional | Default Value |
---|---|---|---|
items | array | required | - |
onSelect | function | required | - |
match | function | optional | internal matching function |
onDropdownOpen | function | optional | - |
onDropdownClose | function | optional | - |
placeholder | string | optional | '' |
itemClassName | string | optional | - |
activeItemClassName | string | optional | - |
inputClassName | string | optional | - |
dropdownClassName | string | optional | - |
requiredInputLength | number | optional | 0 |
clearInputOnSelect | boolean | optional | false |
clearInputOnClick | boolean | optional | false |
suppressReselect | boolean | optional | true |
dropDownLength | number | optional | infinite |
value | string | optional | undefined |
debounceTime | number | optional | 0 |
debounceLoader | string | optional | 'Loading...' |
onInput | function | optional | - |
onClick | function | optional | - |
Pass a match function as stated above for creating your own matching algorithm for the autocomplete functionality.
Parameter: (currentInput, item)
Default match function:
1/** 2 * default function for matching the current input value (needle) and the values of the items array 3 * @param currentInput String (the current user input) 4 * @param item (one item of the items array) 5 * @returns {boolean} 6 */ 7const match = (currentInput, item) => 8 item.label.substr(0, currentInput.length).toUpperCase() === 9 currentInput.toUpperCase();
1const match = (currentInput, item) => 2 item.label.toLowerCase().includes(currentInput.toLowerCase());
requiredInputLength=3
, only if the user input is longer than 2 characters, the dropdown menu will appear.value
is set, you have to use the lifecycle method onSelect
and set your value state on your own.value
is set, you have to use the lifecycle method onClick
and set your value state on your own.dropDownLength
matches in the dropdown. Useful if the array is really big.initialValue
is deprecated, use value
insteadvalue
can be used to specify and override the value of the input fieldvalue="hello world"
will print hello world
into the input fieldvalue
of empty string.value
only if you want complete control over the value of the input field. react-datalist-input
will priotize whatever value is set over anything the user selects or has selected. If you use value
, you will have to update it on your own using the onClick
, onInput
, andonSelect
lifecycle methods.clearInputOnSelect
and clearInputOnClick
won't work and have to be implemented via the mentioned lifecycle methods.The following useEffect
is used to decide if the component should update with the new value
property:
1useEffect(() => { 2 // the parent component can pass its own value prop that will override the internally used currentInput 3 // this will happen only after we are have finished the current computing step and the dropdown is invisible 4 // (to avoid confusion of changing input values for the user) 5 /* 6 * we have to distinguish undefined and empty string value 7 * value == undefined => not set, use internal current input 8 * value !== undefined => value set, use value and override currentInput 9 * this enables value === '' to clear the input field 10 */ 11 const isValuePropSet = value !== undefined; 12 const isValueDifferent = currentInputRef.current !== value; 13 // is drop down visible or are we currently matching based on user input 14 const isMatchingRunning = visible || isMatchingDebounced; 15 if (isValuePropSet && isValueDifferent && !isMatchingRunning) { 16 setCurrentInput(value); 17 } 18}, [visible, isMatchingDebounced, value, setCurrentInput, currentInputRef]);
debounceTime
to define a debounce timeout time (in milliseconds) before the matching algorithm should be calleddebounceTime={1000}
will call the matching algorithm one second after the last user inputitems
is very large and/or the match
-algorithm is doing some heavier operationsdebounceTime
may improve the user experience by reducing lag times as it reduces the calls to the matching and rendering of the dropdown.debounceTime={3000}
or higher, you might want to consider using another package / user input instead. Think about a "search/look-up"-button next to your input field or even consider running the search functionality in a dedicated backend.newValue
of type string from event.target.value
clearOnClickInput
on your own if you pass the value
propcurrentInput
of type string based on clearOnClickInput
and the last user inputNo vulnerabilities found.
No security vulnerabilities found.