简体   繁体   English

Mui v5 styleOverrides 不适用于主题

[英]Mui v5 styleOverrides not working with themes

I am trying to style using theme overrides as laid out in the documentation here :我正在尝试使用此处文档中所述的主题覆盖设置样式:

I have the following code sandbox :我有以下代码沙箱

import * as React from 'react';
import { ThemeProvider, createTheme } from '@mui/material/styles';
import Select from '@mui/material/Select'
import MenuItem from '@mui/material/MenuItem'

const theme = createTheme({
  components: {
    MuiSelect: {
      styleOverrides: {
        root: {
          background: '#000',
        },
      },
    },
  },
});

export default function GlobalThemeOverride() {
  return (
    <ThemeProvider theme={theme}>
          <Select
            labelId="demo-simple-select-label"
            id="demo-simple-select"
            variant="standard"
          >
            <MenuItem value={10}>Ten</MenuItem>
            <MenuItem value={20}>Twenty</MenuItem>
            <MenuItem value={30}>Thirty</MenuItem>
          </Select>
    </ThemeProvider>
  );
}

The Select box should have a background of #fff however no background is set at all.选择框的背景应该是#fff但是根本没有设置背景。 :( :(

This looks like a bug to me (or at least missing a feature that is reasonable for developers to expect to be there).这对我来说看起来像是一个错误(或者至少缺少开发人员期望存在的合理功能)。 The issue is that Select doesn't define any styles of its own at the root level, so it doesn't leverage the code (which would be a call to MUI's styled such as here for the select class ) that would take care of looking at the theme and applying the corresponding style overrides.问题是 Select 没有在根级别定义它自己的任何样式,因此它没有利用代码(这将是对 MUI styled的调用,例如此处为select)来处理查找在主题并应用相应的样式覆盖。 I recommend logging an issue.我建议记录一个问题。

There are a couple possible workarounds.有几种可能的解决方法。

Workaround 1 - Target the select CSS class解决方法 1 - 以select CSS 类为目标

This approach may work, but it depends on what all you are trying to do since this targets a child of the root element.这种方法可能有效,但这取决于您要尝试做什么,因为它针对的是根元素的子元素。

const theme = createTheme({
  components: {
    MuiSelect: {
      styleOverrides: {
        select: {
          backgroundColor: "#000",
          color: "#fff"
        }
      }
    }
  }
});

编辑 GlobalThemeOverride Material Demo(分叉)

Workaround 2 - Target the MuiInput-root CSS class解决方法 2 - 以MuiInput-root CSS 类为目标

The approach below targets the same element as MuiSelect-root by leveraging MuiInput-root (the rendering of the root element of the Select is delegated to the Input component when the variant is "standard") and then qualifying it via "&.MuiSelect-root" so that it only affects Select rather than all inputs.下面的方法通过利用MuiInput-root (当变体为“标准”时将 Select 的根元素的呈现委托给Input组件)MuiInput-rootMuiSelect-root相同的元素,然后通过"&.MuiSelect-root" ,因此它只影响Select而不是所有输入。

const theme = createTheme({
  components: {
    MuiInput: {
      styleOverrides: {
        root: {
          "&.MuiSelect-root": {
            backgroundColor: "#000",
            color: "#fff"
          }
        }
      }
    }
  }
});

编辑 GlobalThemeOverride Material Demo(分叉)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM