3

I'm using material ui and I have a multiple select component. I have 2 problems, which I'm just unable to solve:

  1. when selecting an item the dropdown jumps around (I've already tried solutions like MenuProps={{ variant: "menu", getContentAnchorEl: null }} -> see code)
  2. I'd like the dropdown to open on hover -> I've checked the the Select API and Menu Props API but I can't find a solution for that.

Could someone point me in the right direction?

Here's my code:

<FormControl
        id={title}
        className={classes.formControl}
        variant="filled"
        size="small"
>
        <InputLabel id={title}>
          {title}
        </InputLabel>
        <Select
          MenuProps={
            PaperProps: {
              style: {
                maxHeight: ITEM_HEIGHT * 4 + ITEM_PADDING_TOP,
                width: 270,
              },
            },
            variant: "menu",
            getContentAnchorEl: null,
          }
          disableUnderline
          labelId={title}
          id={includes}
          multiple
          value={selectedValues}
          input={<Input />}
          renderValue={(selected) => (
            <div>
              {selected.map((value) => (
                <Chip key={value} label={value}/>
              ))}
            </div>
          )}
        >
              <MenuItem
                key={filter.categoryNameWebsite}
                value={filter.categoryNameWebsite}
              >
               //some logic
              </MenuItem>
        </Select>
</FormControl>

EDIT:

Question 1: With the help of NearHuscarl I found out, that the jumping is not related to material UI. My problem was: With each item that was selected from the dropdown the URL path changed. Since I'm using next.js router (useRouter) the "scroll" option is by default "true". I had to change it to "false". Now the jumping is finally gone :)

Question 2: See solution provided by NearHuscarl.

6
  • What do you mean by jump? your code looks good to me. Commented Oct 25, 2021 at 16:14
  • I mean that the dropdown opens and as soon as I click on an item in the dropdown it moves up or down on my screen (basically the indow moves to the very top of the screen). I think it might have something to do with autofocus? I've tried to set {autoFocus: false} as well, but it didn't work...
    – Ewax_Du
    Commented Oct 25, 2021 at 19:46
  • The multiple select component is not at the very top of the page - I think this is why the dropdown jumps around. I hope you understand what I mean..
    – Ewax_Du
    Commented Oct 25, 2021 at 20:05
  • So this is what made the Select jumpy? I removed the margin top in my codesandbox and it still works, weird... Commented Oct 25, 2021 at 20:08
  • If you can put the jumpy Select in a codesandbox, maybe somebody can fix it. Maybe it's because of your custom styles? Commented Oct 25, 2021 at 20:10

1 Answer 1

3

To answer your second question. If you want to show/hide the MenuList on hover/unhover, you can attach the listeners to know when the user hovers the Input and leaves the Popover like below:

<Select
  open={open}
  onMouseEnter={handleOpen}
  onClose={handleClose}
  onOpen={handleOpen}
  MenuProps={{
    PaperProps: {
      onMouseLeave: handleClose,
      {...}
  }}
  {...}
>

Codesandbox Demo

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.