Remove / Override default styles from materialui components

心不动则不痛 提交于 2020-05-30 03:37:26

问题


I'm trying to change the background color of the menuitem popover. But I am unable to remove paddingtop and paddingBottom from menuitem. It's kind of annoying because some materialui components inherit styles from paper, list, menu and etc. Is there a clean and efficient way to work around this? For eg, using overrides in theme and etc.

I have experiment and I know it can be done using inline styles/classes but i do not wish to use that method. I've tried using ListProps={{disablePadding: true}}, MenuProps={{{disablePadding: true}}. But it Doesn't work.

       <FormControl className={classes.formControl}>
            <Select
            value={value.groupId}
            onChange={handleChange}
            MenuProps={{
                getContentAnchorEl: null,
                anchorOrigin: {
                  vertical: "bottom",
                  horizontal: "left",
                },
              }}
            classes={{
                icon:  isDarkMode ?  classes.iconLight :classes.icon,

            }}
            ListProps={{disablePadding: true}}
            inputProps={{
                name: 'groupId',
                id: 'group-machines',
              }}
            >

        {
            equipmentgroups.map(equipmentgroup =>
            <MenuItem 
            style={isDarkMode ? {backgroundColor: theme.palette.primary.dark} : 
            {backgroundColor: theme.palette.secondary.main}}
            className={classes.menuItemDisplay}  
            value={equipmentgroup.groupId}
            key={equipmentgroup.groupId}
            >{equipmentgroup.groupName}</MenuItem>
            )

        }

            </Select>
        </FormControl>

I'm still GETTING this when I'm inspecting the element.

        .MuiList-padding-370 {
             padding-top: 8px;
             padding-bottom: 8px;
         }

回答1:


You need MenuProps={{ MenuListProps: { disablePadding: true } }}.

Here's a working example:

import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import InputLabel from "@material-ui/core/InputLabel";
import MenuItem from "@material-ui/core/MenuItem";
import FormControl from "@material-ui/core/FormControl";
import Select from "@material-ui/core/Select";

const useStyles = makeStyles(theme => ({
  root: {
    display: "flex",
    flexWrap: "wrap"
  },
  formControl: {
    margin: theme.spacing(1),
    minWidth: 120
  },
  selectEmpty: {
    marginTop: theme.spacing(2)
  }
}));

function SimpleSelect() {
  const classes = useStyles();
  const [values, setValues] = React.useState({
    age: "",
    name: "hai"
  });

  const inputLabel = React.useRef(null);

  function handleChange(event) {
    setValues(oldValues => ({
      ...oldValues,
      [event.target.name]: event.target.value
    }));
  }

  return (
    <form className={classes.root} autoComplete="off">
      <FormControl className={classes.formControl}>
        <InputLabel htmlFor="age-simple">Age</InputLabel>
        <Select
          value={values.age}
          onChange={handleChange}
          MenuProps={{ MenuListProps: { disablePadding: true } }}
          inputProps={{
            name: "age",
            id: "age-simple"
          }}
        >
          <MenuItem value="">
            <em>None</em>
          </MenuItem>
          <MenuItem value={10}>Ten</MenuItem>
          <MenuItem value={20}>Twenty</MenuItem>
          <MenuItem value={30}>Thirty</MenuItem>
        </Select>
      </FormControl>
    </form>
  );
}

export default SimpleSelect;



来源:https://stackoverflow.com/questions/56352255/remove-override-default-styles-from-materialui-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!