Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Positioning of <MenuItems> under the Material-ui <Select> component

Tags:

material-ui

By design, clicking on a material-ui <Select> component (or alternatively a <TextField select> component), causes the menu items to appear in a position which covers the originating Select/TextField element.

<Select ...>   <MenuItem value={10}>Ten</MenuItem>   <MenuItem value={20}>Twenty</MenuItem>   <MenuItem value={30}>Thirty</MenuItem> </Select> 

Is it possible to override this positioning (whether with JSS or with a new composition) such that the menu items align themselves directly under the originating Select/TextField element?

Any guidance would be appreciated. Thank you.

like image 469
KenB Avatar asked Sep 06 '19 04:09

KenB


People also ask

How to set position of menu in Material UI?

By design, clicking on a material-ui <Select> component (or alternatively a <TextField select> component), causes the menu items to appear in a position which covers the originating Select/TextField element.

How do I make Mui select required?

Material UI has other types of Select(native) also where you can just use plain HTML required attribute to mark the element as required. It also works with non-native Select, as it is placed in the FormControl tag. When I place the "required" in the FormControl I get the " * " so it looks required.

What is anchorEl menu?

anchorEl: It is used to set the position of the menu.

What is material UI menu?

Menus display a list of choices on temporary surfaces. A menu displays a list of choices on a temporary surface. It appears when the user interacts with a button, or other control.


2 Answers

Use MenuProps attribute of Select element

     <Select             MenuProps={{               anchorOrigin: {                 vertical: "bottom",                 horizontal: "left"               },               transformOrigin: {                 vertical: "top",                 horizontal: "left"               },               getContentAnchorEl: null             }}      >       <MenuItem value={10}>Ten</MenuItem>       <MenuItem value={20}>Twenty</MenuItem>       <MenuItem value={30}>Thirty</MenuItem>     </Select> 
like image 174
Akhil Xavier Avatar answered Sep 23 '22 07:09

Akhil Xavier


Mui v5 :

The position of <MenuItems> is default under the Material-ui <Select> in Mui v5.

If want to change position, code sample :

Edit TextField selectionStart

Mui v4 :

  1. Use <Select> and set menu items position:
        <Select           ...           MenuProps={{             anchorOrigin: {               vertical: "bottom",               horizontal: "left"             },             getContentAnchorEl: null           }}         > 
  1. Use <TextField select> and set menu items positon:
      <TextField         ...         select={true}         SelectProps={{           MenuProps: {             anchorOrigin: {               vertical: "bottom",               horizontal: "left"             },             getContentAnchorEl: null           }         }}       > 

Code Sample :

Edit TextField selectionStart

Hope to help you !

like image 24
Jay Lu Avatar answered Sep 22 '22 07:09

Jay Lu