Search code examples
htmlcssreactjsbootstrap-4bootstrap-multiselect

How to achieve the multi select items from the drop-down or list in react js web


hello friend i am new in react js web. I want to select multiple cities from the drop-down or list like this.image1

and after selecting multiple cities user click on apply and then cites looks like thisimage 2

thanks in advance


Solution

  • I have provided an example using material ui. Please check it.

    import React from 'react';
    import {makeStyles, useTheme} from '@material-ui/core/styles';
    import Input from '@material-ui/core/Input';
    import InputLabel from '@material-ui/core/InputLabel';
    import MenuItem from '@material-ui/core/MenuItem';
    import FormControl from '@material-ui/core/FormControl';
    import ListItemText from '@material-ui/core/ListItemText';
    import Select from '@material-ui/core/Select';
    import Checkbox from '@material-ui/core/Checkbox';
    
    const useStyles = makeStyles((theme) => ({
        formControl: {
            margin: theme.spacing(1),
            minWidth: 120,
            maxWidth: 300,
        },
        chips: {
            display: 'flex',
            flexWrap: 'wrap',
        },
        chip: {
            margin: 2,
        },
        noLabel: {
            marginTop: theme.spacing(3),
        },
    }));
    
    const ITEM_HEIGHT = 48;
    const ITEM_PADDING_TOP = 8;
    const MenuProps = {
        PaperProps: {
            style: {
                maxHeight: ITEM_HEIGHT * 4.5 + ITEM_PADDING_TOP,
                width: 250,
            },
        },
    };
    
    const names = [
        'Oliver Hansen',
        'Van Henry',
        'April Tucker',
        'Ralph Hubbard',
        'Omar Alexander',
        'Carlos Abbott',
        'Miriam Wagner',
        'Bradley Wilkerson',
        'Virginia Andrews',
        'Kelly Snyder',
    ];
    
    function getStyles(name, personName, theme) {
        return {
            fontWeight:
                personName.indexOf(name) === -1
                    ? theme.typography.fontWeightRegular
                    : theme.typography.fontWeightMedium,
        };
    }
    
    export default function MultipleSelect() {
        const classes = useStyles();
        const theme = useTheme();
        const [personName, setPersonName] = React.useState([]);
    
        const handleChange = (event) => {
            setPersonName(event.target.value);
        };
    
        return (
            <div>
                <FormControl className={classes.formControl}>
                    <InputLabel id="demo-mutiple-checkbox-label">Tag</InputLabel>
                    <Select
                        labelId="demo-mutiple-checkbox-label"
                        id="demo-mutiple-checkbox"
                        multiple
                        value={personName}
                        onChange={handleChange}
                        input={<Input/>}
                        renderValue={(selected) => selected.join(', ')}
                        MenuProps={MenuProps}
                    >
                        {names.map((name) => (
                            <MenuItem key={name} value={name}>
                                <Checkbox checked={personName.indexOf(name) > -1}/>
                                <ListItemText primary={name}/>
                            </MenuItem>
                        ))}
                    </Select>
                </FormControl>
            </div>
        );
    }