Gathering detailed insights and metrics for react-datalist-input
Gathering detailed insights and metrics for react-datalist-input
Gathering detailed insights and metrics for react-datalist-input
Gathering detailed insights and metrics for react-datalist-input
@avul/react-native-datalist-input
React Native TextInput Component with Datalist
react-plain-datalist-input
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.
react-input-datalist
HTML5 input/datalist combination component
reactspa-datalist
a single-page React application to render a data list associated with a specific input property
This package provides a reactjs component that contains an input field with a drop down menu to pick a possible option based on the current input.
npm install react-datalist-input
Typescript
Module System
Node Version
NPM Version
TypeScript (92.38%)
JavaScript (4.44%)
CSS (1.8%)
SCSS (1.38%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
27 Stars
133 Commits
12 Forks
1 Watchers
29 Branches
3 Contributors
Updated on Aug 07, 2024
Latest Version
3.2.1
Package Id
react-datalist-input@3.2.1
Unpacked Size
177.69 kB
Size
36.81 kB
File Count
10
NPM Version
8.19.2
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
26
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.
DatalistInput is intended to be easy to use and comes with default styling:
1import DatalistInput from 'react-datalist-input'; 2import 'react-datalist-input/dist/styles.css'; 3 4const YourComponent = () => ( 5 <DatalistInput 6 placeholder="Chocolate" 7 label="Select ice cream flavor" 8 onSelect={(item) => console.log(item.value)} 9 items={[ 10 { id: 'Chocolate', value: 'Chocolate' }, 11 { id: 'Coconut', value: 'Coconut' }, 12 { id: 'Mint', value: 'Mint' }, 13 { id: 'Strawberry', value: 'Strawberry' }, 14 { id: 'Vanilla', value: 'Vanilla' }, 15 ]} 16 /> 17);
But DatalistInput is also intended to be easy to extend:
1import DatalistInput, { useComboboxControls } from 'react-datalist-input'; 2 3const YourComponent = () => { 4 const { setValue, value } = useComboboxControls({ initialValue: 'Chocolate' }); 5 6 return ( 7 <DatalistInput 8 value={value} 9 setValue={setValue} 10 label="Select ice cream flavor" 11 showLabel={false} 12 items={[...]} 13 onSelect={(item) => { 14 setValue(''); // Custom behavior: Clear input field once a value has been selected 15 }} 16 /> 17 ); 18};
Note: React 18 required! Version 3.0.0 utilizes React 18. If you use React <=17, install react-datalist-input@2.2.1
instead! Find the documentation for version 2.2.1 here.
1npm i react-datalist-input
1yarn add react-datalist-input
TL;DR:
There are various kinds of dropdown UI controls. This package implements one of them - the combobox control - as defined by WAI-ARIA 1.2.
A combobox renders a list of suggested values based on an input field. The user can select an option of the list to autocomplete the input field or type in a value that is not in the list. This is the main difference to the select control, where the user must pick a value from the list. You can read more about the differences on vitalflux.com.
If you don't care about custom functionality or advanced styling, consider using the native datalist HTML5 element. If you require more control, then this package is for you!
You can also build something tailored to your own use case from scratch! Have a look at w3schools.com to see how to create a autocomplete control with pure HTML, CSS, and JS.
This package follows the WAI-ARIA 1.2 specification. Be aware that version 1.2 is still in development. If you require a more battle-tested ARIA implementation, consider using Reach UI instead.
This package does not implement the optional aria-activedescendant
property but rather programmatically shifts focus to the active item. This might be up to change in the future!
Please provide your feedback on GitHub!
The documentation below only applies to the latest version. Please find earlier versions of the documentation on GitHub, e.g. version 2.2.1.
Item
type definition to make it easier to extend the items array.useComoboxHelpersConfigParams
to UseComboboxHelpersConfigParams
.useComboboxContext
now exposes the currentInputValue
, which can be handy when implementing custom item components with highlighting.Version 3.1.0 changes the default filter from startsWith
to includes
to match the behavior of the datalist HTML 5 element. You can read more information about filtering and how to use a custom filter down below (Filtering).
Full refactor of react-datalist-input.
Note: Be aware that version 3.0.0 includes breaking changes. Version 3.0.0 deprecates some properties from the DatalistInput component such as requiredInputLength
. Instead of using custom properties for different use cases, you have now full control using the useComboboxControls
hook and the filters
property. Use plain React state and callbacks to control every aspect of the component's behavior!
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 performance1import React, { useState, useMemo, useCallback } from 'react'; 2// Import the DataListInput component 3import DataListInput from 'react-datalist-input'; 4// Integrate the css file if you want to use the default styling 5import 'react-datalist-input/dist/styles.css'; 6 7// Your data source 8const options = [ 9 { name: 'Chocolate' }, 10 { name: 'Coconut' }, 11 { name: 'Mint' }, 12 { name: 'Strawberry' }, 13 { name: 'Vanilla' }, 14]; 15 16const YourComponent = ({ options }) => { 17 const [item, setItem] = useState(); // The selected item will be stored in this state. 18 19 /** 20 * The onSelect callback function is called if the user selects one option out of the dropdown menu. 21 * @param selectedItem object (the selected item / option) 22 */ 23 const onSelect = useCallback((selectedItem) => { 24 console.log('selectedItem', selectedItem); 25 setItem(selectedItem); 26 }, []); 27 28 // Make sure each option has an unique id and a value 29 const items = useMemo( 30 () => 31 options.map((option) => ({ 32 // required: id and value 33 id: option.name, 34 value: option.name, 35 // optional: label, node 36 // label: option.name, // use a custom label instead of the value 37 // node: option.name, // use a custom ReactNode to display the option 38 ...option, // pass along any other properties to access in your onSelect callback 39 })), 40 [], 41 ); 42 43 return ( 44 <DatalistInput label="Select your favorite flavor" placeholder="Chocolate" items={items} onSelect={onSelect} /> 45 ); 46};
For simple use cases, you can use the default styling provided by this package: import 'react-datalist-input/dist/styles.css'
.
However, you can also customize the styling by providing your own CSS! Instead of importing the default stylesheet, create your own one. The following classes are available:
react-datalist-input__container
: For the container element.react-datalist-input__textbox
: For the input element.react-datalist-input__label
: For the label element.react-datalist-input__listbox
: For the dropdown list.react-datalist-input__listbox-option
: For each option in the dropdown list.Note: Use the focus and hover states of react-datalist-input__listbox-option
to show the user some visual feedback.
1.react-datalist-input__listbox-option:focus { 2 background-color: gray; 3}
Tip: To get up and running quickly, just copy-paste the default stylesheet and adapt the pieces you need.
Alternatively, you can also pass custom classes to each element of the DatalistInput component by using the following props:
className
: For the container element.inputProps["className"]
: For the input element.labelProps["className"]
: For the label element.listboxProps["className"]
: For the dropdown list.listboxOptionProps["className"]
: For each option in the dropdown list.isExpandedClassName
: Applied to the dropdown list if it is expanded.isCollapsedClassName
: Applied to the dropdown list if it is collapsed. !If provided, you must manage the hiding of the dropdown list yourself!You can also customize the rendering of each item in the dropdown list by providing a custom component.
1import { useMemo } from 'react'; 2import type { Item } from '../combobox'; 3import { DatalistInput, useComboboxControls, useComboboxContext } from '../combobox'; 4 5type CustomItem = Item & { 6 description: string; 7}; 8 9const items: Array<CustomItem> = [ 10 { id: 'Chocolate', value: 'Chocolate', description: 'Chocolate is delicious' }, 11 { id: 'Coconut', value: 'Coconut', description: 'Coconut is tasty but watch your head!' }, 12 { id: 'Mint', value: 'Mint', description: 'Mint is a herb?' }, 13 { id: 'Strawberry', value: 'Strawberry', description: 'Strawberries are red' }, 14 { id: 'Vanilla', value: 'Vanilla', description: 'Vanilla is a flavor' }, 15]; 16 17const CustomItem = ({ item }: { item: CustomItem }) => { 18 // get access to the combobox context for highlighting, etc. 19 const { currentInputValue, selectedItemId } = useComboboxContext(); 20 21 // Each item is wrapped in a li element, so we don't need to provide a custom li element here. 22 return ( 23 <div 24 style={{ 25 display: 'flex', 26 gap: '5px', 27 flexDirection: 'column', 28 background: item.id === selectedItemId ? 'gray' : 'white', 29 }} 30 > 31 <b>{item.value}</b> 32 <span>{item.description}</span> 33 </div> 34 ); 35}; 36 37export default function Index() { 38 const customItems = items.map((item) => ({ 39 // each item requires an id and value 40 ...item, 41 // but we can also add a custom component for the item 42 node: <CustomItem item={item} />, 43 })); 44 45 return ( 46 <DatalistInput 47 label={<div>Custom Label</div>} 48 placeholder="Chocolate" 49 items={customItems} 50 onSelect={(i) => console.log(i)} 51 /> 52 ); 53}
Note: Please note that by default the Item.value property is used for filtering. In case you want to filter over custom properties, make sure to implement a custom filter function.
By default, the DatalistInput component will filter the dropdown list based on the value of the input element using the includes
method. This follows the behavior of the datalist HTMl element. You can however provide your own filtering function by passing a custom filter
function to the DatalistInput component.
For instance, this package exposes a startsWith
alternative filter functions that you can use as follows:
1import { DatalistInput, startsWithValueFilter } from 'react-datalist-input'; 2 3const YourComponent = () => { 4 return <DatalistInput label="Select ice cream flavor" items={items} filters={[startsWithValueFilter]} />; 5};
Now, the dropdown list will only show items that start with the input value.
You can also implement a custom filter function:
1import type { Filter } from 'react-datalist-input'; 2import { DatalistInput } from 'react-datalist-input'; 3 4const YourComponent = () => { 5 // Custom filter: Display all values that are smaller or equal than the input value 6 const myFilterFunction: Filter = useCallback( 7 (items, value) => items.filter((item) => item.value <= value), 8 [selectedItems], 9 ); 10 11 return <DatalistInput label="Select ice cream flavor" items={items} filters={[myFilterFunction]} />; 12};
You can chain custom filters to filter the list of options displayed in the dropdown menu.
For instance, display only the first three items in the list:
1import type { Filter } from 'react-datalist-input'; 2// Import the default filter startWithValueFilter 3import { DatalistInput, includesValueFilter } from 'react-datalist-input'; 4 5const YourComponent = () => { 6 // Custom filter: Only display the first 3 items 7 const limitOptionsFilter: Filter = useCallback((items, value) => items.slice(0, 3), []); 8 9 // First we filter by the value using the default filter, then we add a custom filter. 10 const filters = [includesValueFilter, customFilter]; 11 12 return <DatalistInput label="Select ice cream flavor" items={items} filters={filters} />; 13};
Since all props of the input element are exposed, you can easily customize DatalistInput to act as an Select component.
Just set the input field to readonly, adjust the filter to always show all options, and set the selected item as the new value of the input field:
1import { DatalistInput, useComboboxControls } from 'react-datalist-input'; 2const items = [ 3 { id: 'Chocolate', value: 'Chocolate' }, 4 { id: 'Coconut', value: 'Coconut' }, 5 { id: 'Mint', value: 'Mint' }, 6 { id: 'Strawberry', value: 'Strawberry' }, 7 { id: 'Vanilla', value: 'Vanilla' }, 8]; 9function YourComponent() { 10 // The useComboboxControls hook provides useful states so you don't have to define them yourself. 11 const { value, setValue } = useComboboxControls({ initialValue: 'Chocolate' }); // Same as: const [value, setValue] = useState("Chocolate"); 12 return ( 13 <DatalistInput 14 value={value} 15 onSelect={(item) => setValue(item.value)} 16 label="Select ice cream flavor" 17 items={items} 18 filters={[(items) => items]} 19 inputProps={{ 20 title: 'Please select an ice cream flavor', 21 required: true, 22 pattern: `^(${items.map((i) => i.value).join('|')})$`, 23 readOnly: true, 24 }} 25 /> 26 ); 27}
Use the useComboboxControls
hook to get fine-grained control over the input value and the dropdown expansion states or just manage the value and expanded state yourself!
In this example, we utilize DatalistInput to act as a multi-select control:
1import { DatalistInput, useComboboxControls, startsWithValueFilter } from 'react-datalist-input'; 2 3const YourComponent = () => { 4 // useComboboxControls returns state and handlers for the input value and the dropdown expansion state 5 const { isExpanded, setIsExpanded, setValue, value } = useComboboxControls({ isExpanded: true }); 6 const [selectedItems, setSelectedItems] = useState<Item[]>([]); 7 8 useEffect(() => { 9 // Abitrary logic: Close select after 3 selections 10 if (selectedItems.length === 3) { 11 setIsExpanded(false); 12 } 13 }, [selectedItems]); 14 15 // Custom filter: Filter displayed items based on previous selections 16 const customFilter: Filter = useCallback( 17 (options, value) => { 18 return options.filter((o) => !selectedItems.find((s) => s.id === o.id)); 19 }, 20 [selectedItems], 21 ); 22 23 return ( 24 <DatalistInput 25 isExpanded={isExpanded} 26 setIsExpanded={setIsExpanded} 27 value={value} 28 setValue={setValue} 29 onSelect={(item) => { 30 if (selectedItems.length >= 3) setSelectedItems([item]); 31 else setSelectedItems((prevItems) => [...prevItems, item]); // Add the selected item to the list of selected items 32 setValue(''); // Clear the input value after selection 33 setIsExpanded(true); // Keep dropdown open after selection 34 }} 35 label="Select ice cream flavor" 36 items={items} 37 filters={[customFilter, startsWithValueFilter]} 38 /> 39 ); 40};
DatalistInput accepts the following properties:
items
: An array of objects of type Item
with the following properties:
label
: The label of the input. Can be of type ReactNode or string.showLabel
: Whether to show the label. If not provided, defaults to true. If false, the label will not be shown but the label property must be of type string and will be supplied to the aria-label
attribute.placeholder
: The placeholder of the input.value
: The value of the input.setValue
: A function to set the value of the input.isExpanded
: Whether the dropdown is expanded.setIsExpanded
: A function to set the expanded state of the dropdown.onSelect
: A callback function that is called when the user selects an option.filters
: An array of filters of type Filter
that are applied to the list of options displayed in the dropdown menu.selectedItem
: The currently selected item. Important for ARIA. DatalistInput keeps track of the last selected item. You only need to provide this prop if you want to change the selected item outside of the component.inputProps
: An object of props to pass to the combobox input element.listboxProps
: An object of props to pass to the listbox element.labelProps
: An object of props to pass to the label element.listboxOptionProps
: An object of props to pass to the listbox option elements.highlightProps
: An object of props to style the highlighted text.isExpandedClassName
: The class name applied to the listbox element if it is expanded.isCollapsedClassName
: The class name applied to the listbox element if it is collapsed.isExpandedStyle
: The inline style applied to the listbox element if it is expanded.isCollapsedStyle
: The inline style applied to the listbox element if it is collapsed.className
: The class name applied to the container element.The following utilities are exported together with the DatalistInput component:
includesValueFilter
: The default filter that filters the list of options based on the value of the input. This filter follows the behavior of the datalist HTML element.startsWithValueFilter
: An alternative filter that filters based on the start of the option's value.useComboboxControls
: A hook to get the state and handlers for the input value and the dropdown expansion state.useComboboxHelpers
: A low-level hook, which returns a set of callbacks and event handlers for the DatalistInput component.useFilters
: A hook that applies an array of filters to an array of items based on the current value.useComboboxContext
: A low-level hook that returns the Combobox context value.Combobox
: A low-level component which is wrapped by DatalistInput. It also acts as the Combobox ContextProvider.Combobox.ComboboxInput
: A low-level component that provides the input field. It has to be wrapped by the Combobox component.Combobox.Listbox
: A low-level component that provides the listbox. It has to be wrapped by the Combobox component.Combobox.ListboxOption
: A low-level component that provides one listbox option. It has to be wrapped by the Combobox component.Combobox.Highlight
: A low-level component that provides highlighting of the listbox option values based on the current input value. It has to be wrapped by the Combobox component.The following types are exported from react-datalist-input and available for use in your components:
Filter
: A filter which can be added to the filters property of the DatalistInput component.Item
: An item that can be added to the items property of the DatalistInput component.DatalistInputProps
: The props accepted by the DatalistInput component.ComboboxProps
: The props accepted by the low-level Combobox component.ComboboxInputProps
: The props accepted by the low-level ComboboxInput component.ListboxProps
: The props accepted by the low-level Listbox component.ListboxOptionProps
: The props accepted by the low-level ListboxOption component.HighlightProps
: The props accepted by the low-level Highlight component.UseComboboxHelpersConfigParams
: The params for the low-level useComboboxHelpers
hook.No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 0/28 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
27 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More