【发布时间】:2019-03-21 18:07:26
【问题描述】:
我正在尝试学习如何使用 JSS 进行样式设置。我想在 InputLabel 聚焦时更改标签的颜色。我最终让它在下面的代码中工作,但我不明白它为什么工作:
import React from 'react'
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles'
import { FormControl, InputLabel, Select, } from '@material-ui/core/'
const styles = theme => ({
inputLabel: {
'&$focused': {
color: 'red',
},
},
focused: {
},
})
class Test extends React.Component {
render() {
const { classes } = this.props;
return(
<div>
<FormControl>
<InputLabel className={classes.inputLabel} FormLabelClasses={ classes }>Select</InputLabel>
<Select/>
</FormControl>
</div>
)
}
}
Test.propTypes = {
classes: PropTypes.object.isRequired,
}
export default withStyles(styles)(Test)
具体来说,我不明白为什么我不能在外部 focused 字段中将颜色设置为红色。我也不确定我是否理解&$focused 的作用——我认为只是引用外部focused 字段,但如果是这样,为什么我不能将外部focused 字段设置为{ color: 'red' }?我试过了,但它不起作用。此外,如果我只是删除外部 focused 字段,它会停止将颜色设置为红色!为什么有必要?我也不明白将classes 传递给FormLabelClasses 的意义何在——如果我再次删除它,它不会导致焦点标签变红。
【问题讨论】:
标签: css reactjs material-ui jss