How to override selected MenuItem style?

人盡茶涼 提交于 2020-01-04 05:48:20

问题


I'm using the Material-ui-next Select component in a project.

Most of the styles are being overriden using the classes prop. But the selected key can't get to work even using MuiThemeProvider.

Here is the relevant parts of the code:

...

const theme = createMuiTheme({
  overrides: {
    MuiMenuItem: {
      selected: {
        backgroundColor: 'transparent',
      }
    }
  }
});

...

class SelectMUI extends Component {

  render() {

  const {className, name} = this.props

  return (
    <MuiThemeProvider theme={theme}>
      <Select
        classes={{root: 'muisc-root', icon: 'muisc-icon', select: 'muisc-select', selectMenu: 'muisc-select-menu'}}
        className={`mui-select-custom ${className}`}
        endAdornment={<Caret className='mui-select-caret'/>}
        MenuProps={{classes: {paper: 'muisc-menu-paper'}}}
        {...this.props}
      >
        {this.props.children}
      </Select>
  </MuiThemeProvider>
)

} }

So, as you can see, while I'm importing the MenuItems as props, I'm using the MuiThemeProvider to inject style in the items.

And here is a screenshot of the applied style in a selected item:

How to override this selector that is combining two classes from the same element?


回答1:


ok, I figure it out how to override a combined selector. Here is the solution:

   MuiMenuItem: {
      root: {
        background: 'transparent',
        '&$selected': { // <-- mixing the two classes
          backgroundColor: 'transparent'
        }
      }
    }


来源:https://stackoverflow.com/questions/49439266/how-to-override-selected-menuitem-style

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