ReactJS multi select dropdown
npm install @dmitriig/react-selectA react dropdown multiselect component powered by react-virtualized for ability to display a long list of values
``yarn add @dmitriig/react-select``$3
typescript
// Options should be provided as {value,label} pairs
export interface IItem {
value: number,
label: string
}
// Component props
export interface IReactSelectProps {
// possible options
options: IItem[],
// string placeholder
placeholder: string,
// callback on selection change
onChange: (items: IItem[]) => void,
// custom cell renderer
rowRenderer?: (item: any, index: number) => void,
// custom container style
style?: any,
// custom input style
inputStyle?: any,
// custom list dropdown style
listStyle?: any,
// custom list row height
listRowHeight?: number,
// custom input font size
inputFontSize?: number,
// custom message for no search results
noResultsMessage?: string
// controlled selection array
selection?: IItem[]
}
`
typescript
// Format data into value and label pair
const options = [];
for (let i = 0; i < 10000; i++) {
options.push({
value: i,
label: Math.random().toString(36).substring(7)
})
}return onChange={(items)=>console.log(items)}/>
`Demo: https://codesandbox.io/embed/hopeful-jang-zzv8e?fontsize=14
##### Controlled
`javascript
function Controlled(props) {
const {options} = props;
// use an initial selection
const [selection] = React.useState(options.slice(0, index));
const [flag, setFlag] = React.useState(false); // dynamically update the selection
function handleAdd() {
selection.push(options[index++]);
setFlag(!flag);
}
// dynamically update the selection
function handleRemove() {
selection.splice(0, 1);
setFlag(!flag);
}
return
Add a token
Remove a token
onChange={action('changed')}/>
}
``