如何更改Material UI React输入组件的轮廓颜色?

J. *_*son 5 reactjs material-ui

在文档和其他SO问题中,我一直在高低搜索答案.

createMuiTheme在单独的JS文件中使用该选项来覆盖某些默认样式,但我很难理解该overrides选项的工作原理.

目前我的按钮看起来像这样: 在此输入图像描述 我得到的代码看起来像这样:

const theme = createMuiTheme({
    ...other code,
    overrides: {
    MuiFormControlLabel: {
        focused: {
            color: '#4A90E2'
        }
    },
    MuiOutlinedInput: {
        focused: {
                border: '1px solid #4A90E2'
        },
        notchedOutline: {
            border: '1px solid #4A90E2'
        },
    },
    MuiFormLabel: {
        focused: {
            color: '1px solid #4A90E2'
        }
    }
}
)};
Run Code Online (Sandbox Code Playgroud)

然后在我的组件中,我正在使用它:

import theme from './styles/ThemeStyles';
import { withStyles } from '@material-ui/core/styles';

class SignInForm extends Component {
render() {
    const { classes } = this.props;
    <form className={classes.container} noValidate autoComplete='off'>
        <TextField
            id="outlined-email-input"
            label="Email"
            className={classes.textField}
            type="email"
            name="email"
            autoComplete="email"
            margin="normal"
            variant="outlined"
        />
    </form>
}}
Run Code Online (Sandbox Code Playgroud)

我的问题是,我错过了什么让我的组件看起来如此时髦?在将来,我如何知道overridesThemeProvider选项中的目标是什么,以便我不会遇到类似的情况?

J. *_*son 12

感谢Rudolf Olah的帮助,并为我指明了正确的方向!我可以使用以下代码解决此问题:

overrides: {
    MuiOutlinedInput: {
        root: {
            position: 'relative',
            '& $notchedOutline': {
                borderColor: 'rgba(0, 0, 0, 0.23)',
            },
            '&:hover:not($disabled):not($focused):not($error) $notchedOutline': {
                borderColor: '#4A90E2',
                // Reset on touch devices, it doesn't add specificity
                '@media (hover: none)': {
                    borderColor: 'rgba(0, 0, 0, 0.23)',
                },
            },
            '&$focused $notchedOutline': {
                borderColor: '#4A90E2',
                borderWidth: 1,
            },
        },
    },
    MuiFormLabel: {
        root: {
            '&$focused': {
                color: '#4A90E2'
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 干得好!从 Material UI 文档中并不明显可以使用各种 CSS 选择器。 (2认同)

Rud*_*lah 7

要查找可以更改的类名和CSS属性,Component API的文档会显示一个列表.

TextField虽然是一种特殊情况,因为它组合并呈现多个子组件,它允许您将CSS属性传递给Input组件和FormHelperText组件.

OutlinedInput是一个非常特殊的情况,因为它实际上使用NotchedInput作为具有自己的CSS属性的输入元素.

查看OutlinedInput代码,您可以看到正在使用的子选择器:

root: {
  position: 'relative',
  '& $notchedOutline': {
    borderColor,
},
// ...
Run Code Online (Sandbox Code Playgroud)

看起来问题是OutlinedInput没有正确设置NotchedOutline的样式

你可能会有一些运气:

const theme = createMuiTheme({
  // ...other code,
  overrides: {
    // ...
    MuiOutlinedInput: {
      focused: {
        border: '1px solid #4A90E2'
      },
      '& $notchedOutline': {
        border: '1px solid #4A90E2'
      },
    },
    // ...
  }
});
Run Code Online (Sandbox Code Playgroud)


Aci*_*ier 6

这在此处的文档中得到了很好的介绍。

单击标有“自定义 CSS”的字段内进行演示。

以下是如何使用您的原始TextField组件完成此操作:

import React from 'react'
import PropTypes from 'prop-types'
import { withStyles } from '@material-ui/core/styles'
import TextField from '@material-ui/core/TextField'

const styles = theme => ({
  textField: {
    marginLeft: theme.spacing.unit * 3,
    marginBottom: '0px',
  },
  label: {
    '&$focused': {
      color: '#4A90E2'
    },
  },
  focused: {},
  outlinedInput: {
    '&$focused $notchedOutline': {
      border: '1px solid #4A90E2'
    },
  },
  notchedOutline: {},
})

const CustomOutline = ({classes}) => (
  <TextField
    id="outlined-email-input"
    label="Email"
    className={classes.textField}
    type="email"
    name="email"
    autoComplete="email"
    margin="normal"
    variant="outlined"
    InputLabelProps={{
      classes: {
        root: classes.label,
        focused: classes.focused,
      },
    }}
    InputProps={{
      classes: {
        root: classes.outlinedInput,
        focused: classes.focused,
        notchedOutline: classes.notchedOutline,
      },
    }}
  />
)

CustomOutline.propTypes = {
  classes: PropTypes.object.isRequired,
}

export default withStyles(styles)(CustomOutline)
Run Code Online (Sandbox Code Playgroud)