- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Material UI与 react 。试图覆盖使用全局主题设置的 TextField
组件样式。我为应用程序中的所有 TextField
组件设置了一个全局主题。
相关代码:
theme-engine.js
:
export const theme = brand => createMuiTheme({
palette: {
primary: {
main: Brand.getColors(brand).primary
},
secondary: {
main: Brand.getColors(brand).secondary
},
},
typography: {
fontFamily,
fontSize: 14,
htmlFontSize: 16
},
overrides: {
MuiInputBase: {
root: {
color: 'rgba(0, 0, 0, 0.87)',
fontSize: '14px',
fontFamily,
'&:after': {
borderBottom: Brand.getColors(brand).primary,
backgroundColor: Brand.getColors(brand).primary,
height: 1
},
'&:focused:not($disabled):not($error):before': {
borderBottom: Brand.getColors(brand).primary,
backgroundColor: Brand.getColors(brand).primary,
height: 1
},
'&$error:before': {
borderBottom: '#f44336',
backgroundColor: '#f44336',
height: 1
},
},
},
MuiInput: {
root: {
fontFamily
},
underline: {
'&:hover:not($disabled):not($error):not($focused):before': {
borderBottom: '#e0e0e0',
backgroundColor: '#e0e0e0',
height: 1
},
'&:not($disabled):not($error):after': {
borderBottom: Brand.getColors(brand).primary,
backgroundColor: Brand.getColors(brand).primary,
height: 1
},
'&$error:before': {
borderBottom: '#f44336',
backgroundColor: '#f44336',
height: 1
},
'&$error:after': {
borderBottom: '#f44336',
backgroundColor: '#f44336',
height: 1
},
},
},
MuiSvgIcon: {
colorPrimary: {
fill: '#74797b'
},
colorSecondary: {
fill: Brand.getColors(brand).primary,
}
},
}
});
container.js
:
render() {
return (
<MuiThemeProvider theme={theme(brand)}>
//stuff goes here
</MuiThemeProvider>
)
}
现在,在其中一个组件中,我为 TextField
使用了一个图标,并希望下划线也位于该图标下方。为此,我试图覆盖提供的主题,但它不起作用。来自 theme-engine
的样式已应用,但本地覆盖不起作用。
some-component.js
import TextField from '@material-ui/core/TextField';
import {withStyles} from '@material-ui/core/styles';
const TextFieldIcon = withStyles(theme => ({
root: {
underline: {
color: 'red',
height: 4,
'&:before': {
borderBottom: `1px solid #e0e0e0`,
bottom: '-8px',
left: '-32px'
},
'&:hover:not($disabled):not($error):not($focused):before': {
borderBottom: 'red',
backgroundColor: 'red',
height: 1,
bottom: '-8px',
left: '-32px'
},
'&:not($disabled):not($error):after': {
height: 2,
bottom: '-8px',
left: '-32px'
},
'&$error:before': {
height: 1,
bottom: '-8px',
left: '-32px'
},
'&$error:after': {
height: 1,
bottom: '-8px',
left: '-32px'
},
},
}
}))(TextField);
class SomeComponent extends Component{
//Lifecycle methods to make your life easier....or difficult.
render(){
return(
<TextFieldIcon {...assign props and stuff} /> //Styles are not applied
)
}
}
所以问题是,我想保留自定义全局主题,但也在我的组件中覆盖它的某些部分。任何意见表示赞赏。
最佳答案
我看到了一些问题:
root
下嵌套 underline
规则。您应该能够只删除外部 root
block $disabled
、$error
和 $focused
),需要定义这些规则在您传递给 withStyles
withStyles
生成的类是TextField
包裹的Input
组件的类,所以需要通过传递>InputProps
属性下面是您需要的语法的工作示例。我没有尝试评估这些样式是否按照您的意图进行,但它们确实得到了应用。
import React from "react";
import TextField from "@material-ui/core/TextField";
import { withStyles } from "@material-ui/core/styles";
const styles = theme => ({
underline: {
color: "red",
height: 4,
"&:before": {
borderBottom: `1px solid #e0e0e0`,
bottom: "-8px",
left: "-32px"
},
"&:hover:not($disabled):not($error):not($focused):before": {
borderBottom: "red",
backgroundColor: "red",
height: 1,
bottom: "-8px",
left: "-32px"
},
"&:not($disabled):not($error):after": {
height: 2,
bottom: "-8px",
left: "-32px"
},
"&$error:before": {
height: 1,
bottom: "-8px",
left: "-32px"
},
"&$error:after": {
height: 1,
bottom: "-8px",
left: "-32px"
}
},
disabled: {},
error: {},
focused: {}
});
const CustomTextField = ({ classes, ...other }) => {
return <TextField InputProps={{ classes: classes }} {...other} />;
};
export default withStyles(styles)(CustomTextField);
关于javascript - React + Material UI - 在组件中覆盖 MuiTheme withStyles,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56223892/
我正在使用 Material UI与 react 。试图覆盖使用全局主题设置的 TextField 组件样式。我为应用程序中的所有 TextField 组件设置了一个全局主题。 相关代码: theme
与 React Starter Kit ,我添加Material UI如下: npm install material-ui --save 并将以下内容导入到组件中: import RaisedBut
我正在尝试在 CodeSandbox 上测试 Material-UI 组件。并不断收到错误: TypeError undefined is not an object (evaluating '_co
我是一名优秀的程序员,十分优秀!