【问题标题】:How to change Material-UI TextField/Select error color via createMuiTheme?如何通过 createMuiTheme 更改 Material-UI TextField/Select 错误颜色?
【发布时间】:2021-06-29 17:23:15
【问题描述】:

我的应用程序中有一个自定义输入样式,用于文本字段和选择,由ThemeProvider 提供。主题文件如下所示:

...
overrides: {
    MuiInput: {
      input: {
        color: '#404040',
        background: 'white',
        fontSize: '14px',
        borderColor: '#c0c0c0',
        borderStyle: 'solid',
        borderWidth: '1.5px',
        borderRadius: '4px',
        paddingLeft: '10px',
        paddingTop: '10px',
        paddingBottom: '10px',
        '&:hover': {
          borderColor: blue[900],
        },
        '&:focus': {
          borderRadius: '4px',
          background: 'white',
          backgroundColor: 'white',
          borderColor: blue[900],
        },
      },
    },
    ...
},

我能够在焦点和悬停时编辑样式,但是当错误属性处于活动状态时我无法编辑组件。我的目标是在这种情况下显示一个红色边框。

您对如何使用 createMuiTheme 和覆盖有什么建议吗?如果不可能,如果您有使用任何其他样式选项的解决方案,我也会很高兴。

【问题讨论】:

    标签: reactjs material-ui


    【解决方案1】:

    TextField 使用FormHelperText 在TextField 下方显示帮助文本。当error 属性设置为true 时,这也是错误消息。默认错误消息颜色为theme.palette.error.main。所以这就是你覆盖错误颜色的方式:

    const theme = createMuiTheme({
      palette: {
        error: {
          main: "#ff00ff", // change the error color to pink
        }
      }
    });
    
    <ThemeProvider theme={theme}>
      <TextField
        label="Outlined"
        variant="outlined"
        error
        helperText="My error message"
      />
    </ThemeProvider>
    

    现场演示

    【讨论】:

      猜你喜欢
      • 2021-12-07
      • 2021-12-01
      • 2020-07-08
      • 1970-01-01
      • 2019-11-30
      • 2019-11-07
      • 2019-05-31
      • 2021-08-15
      • 1970-01-01
      相关资源
      最近更新 更多