M
M
Mad Coder2020-07-21 17:54:56
React
Mad Coder, 2020-07-21 17:54:56

How to prevent scrolling in autocomplete checkbox material ui?

There is an Autocomplete component from material ui

import React from 'react';
import TextField from '@material-ui/core/TextField';
import Autocomplete from '@material-ui/lab/Autocomplete';
import Checkbox from '@material-ui/core/Checkbox';
import CheckBoxOutlineBlankIcon from '@material-ui/icons/CheckBoxOutlineBlank';
import CheckBoxIcon from '@material-ui/icons/CheckBox';
import { useStyles } from './style';

const icon = <CheckBoxOutlineBlankIcon fontSize="small" />;
const checkedIcon = <CheckBoxIcon fontSize="small" />;

const SelectCheckbox = ({label, twoLineLabel, options, changeHandler, value, readonly, selectedCheck, errorData, gray, noLabel, ind, state, name}) => {

    const classes = useStyles({readonly, gray, noLabel});

    const returnValue = () => {
        if(!selectedCheck) return '';
        return  selectedCheck === 1 ? value[0].name : `Выбрано ${selectedCheck} из ${options.length}`;
    };

    return (
        <>
            <Autocomplete
                name={name}
                multiple
                noOptionsText={'Не найдено'}
                disableClearable
                value={value}
                options={options}
                getOptionLabel={(option) => option.name}
                disableCloseOnSelect
                renderTags={() => null}
                style={{ width: '100%' }}
                classes={{
                    root: classes.vpAutocompleteRoot,
                    listbox: classes.vp_listbox
                }}
                renderOption={(option, { selected }) => {
                    return (
                        <React.Fragment>
                            <Checkbox
                                classes={{
                                    root: classes.rootCheckbox
                                }}
                                color={"primary"}
                                icon={icon}
                                checkedIcon={checkedIcon}
                                disableRipple
                                style={{ marginRight: 8 }}
                                checked={state.some(elem => elem.name === option.name)}
                                onChange={changeHandler(option, name, ind)}
                                onClick={(event) => event.stopPropagation()}
                            />
                            {option.name}
                        </React.Fragment>
                    )
                }}
                renderInput={(params) =>
                    <TextField
                        {...params}
                        error={Boolean(errorData)}
                        label={label}
                        variant="filled"
                        margin="dense"//делает ниже
                        inputProps={{
                            ...params.inputProps,
                            value: returnValue()
                        }}
                        InputLabelProps={{
                            classes: {
                                root: twoLineLabel ? classes.vp_labelRootTwoLine : classes.vp_labelRoot,
                                shrink: classes.vp_shrink,
                                error: classes.vp_errorLabel
                            }
                        }}
                        InputProps={{
                            ...params.InputProps,
                            disableUnderline: true, //пропадает нижняя линия
                            classes: {
                                root: classes.vp_filedRoot,
                                focused: classes.vp_focused,
                                error: classes.vp_errorFiled,
                            },
                            readOnly: Boolean(readonly),
                        }}
                    />
                }
            />
        </>
    )
};

export default SelectCheckbox;

Visually, it looks like this:
5f17011594009013598994.png
The problem is that, for example, I have the first and last element from the list checked. So when I uncheck one checkbox, it will scroll to the element that is checked. Based on the described situation, either to the very top or to the very bottom. How can I avoid this, I would not want to have this scrolling. Thanks in advance!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question