Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
react-datalist-input
Advanced tools
This package provides a react component as follows: an input field with a drop down menu to pick a possible option based on the current input.
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.
Have a look at w3schools.com to see how you can do something similar with pure html, css, and js. For more information about React and the ecosystem see this guide.
Check it out on my personal website!
Feel free to get inspired and more importantly please provide your feedback on structure and style.
This component is not compatible with server-side rendering since it has css bundled with it.
I created a plain version of this package without css. Find more information here.
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 performancenpm i react-datalist-input
import React, { useState, useMemo, useCallback } from "react";
import DataListInput from "react-datalist-input";
const YourComponent = ({ myValues }) => {
// selectedItem
const [item, setItem] = useState();
/**
* your callback function gets called if the user selects one option out of the drop down menu
* @param selectedItem object (the selected item / option)
*/
const onSelect = useCallback((selectedItem) => {
console.log("selectedItem", selectedItem);
}, []);
// the array you want to pass to the react-data-list component
// key and label are required properties
const items = useMemo(
() =>
myValues.map((oneItem) => ({
// required: what to show to the user
label: oneItem.name,
// required: key to identify the item within the array
key: oneItem.id,
// feel free to add your own app logic to access those properties in the onSelect function
someAdditionalValue: oneItem.someAdditionalValue,
// or just keep everything
...oneItem,
})),
[myValues]
);
return (
<DataListInput
placeholder="Select an option from the drop down menu..."
items={items}
onSelect={onSelect}
/>
);
};
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:
/**
* default function for matching the current input value (needle) and the values of the items array
* @param currentInput String (the current user input)
* @param item (one item of the items array)
* @returns {boolean}
*/
const match = (currentInput, item) =>
item.label.substr(0, currentInput.length).toUpperCase() ===
currentInput.toUpperCase();
const match = (currentInput, item) =>
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:
useEffect(() => {
// the parent component can pass its own value prop that will override the internally used currentInput
// this will happen only after we are have finished the current computing step and the dropdown is invisible
// (to avoid confusion of changing input values for the user)
/*
* we have to distinguish undefined and empty string value
* value == undefined => not set, use internal current input
* value !== undefined => value set, use value and override currentInput
* this enables value === '' to clear the input field
*/
const isValuePropSet = value !== undefined;
const isValueDifferent = currentInputRef.current !== value;
// is drop down visible or are we currently matching based on user input
const isMatchingRunning = visible || isMatchingDebounced;
if (isValuePropSet && isValueDifferent && !isMatchingRunning) {
setCurrentInput(value);
}
}, [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 inputFAQs
react-datalist-input provides a React datalist/combobox component called DatalistInput. The component contains an input field with a dropdown menu of suggestions based on the current input.
The npm package react-datalist-input receives a total of 476 weekly downloads. As such, react-datalist-input popularity was classified as not popular.
We found that react-datalist-input demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.