我有以下几点:
class StyledInput extends React.Component{ styles = (color, theme) => ({ underline: { borderBottom: `2px solid ${color}`, '&:after': { borderBottom: `2px solid ${color}`, } } }) div = props => ( <TextField placeholder="temp input" InputProps={{ classes:{ root: props.classes.underline }, style:{ height: '1.5rem', fontSize:'1rem', marginTop: '-1rem', } }} > <div> {props.children} </div> </TextField> ) Styled = withStyles(this.styles('white'))(this.div) render(){ return( <div> <this.Styled>{this.props.children}</this.Styled> </div> ); } } export default StyledInput;
因此,它的作用是当用户单击该字段时,它成功地获取了一个实质性的UI文本字段并将底部栏更改为白色,而不是蓝色。大!
…然而…
我真正想做的是
<this.Styled color={someDefinedColor}>{this.props.children}</this.Styled>
其中,Styled那么应该是这样的:
Styled
Styled = (color) => withStyles(this.styles(color))(this.div)
这样我就可以动态地将颜色传递给Styled属性。显然,这是伪代码-我一直在使用它,但无法通过它。通常来说,material- ui会动态更改颜色,这实在让人不知所措,所以我想知道是否有人知道如何使它生效。
谢谢!
解决方案 …虽然不是很好。
我不确定如何将伪元素与makeStyles功能组件一起使用,但这是一种对我有用的解决方案。为了后代在此添加,以防其他人发现它有用:
makeStyles
class StyledInputInner extends React.Component{ styles = (color, theme) => ({ underline: { '&:before': { borderBottom: `2px solid ${color}`, }, '&:after': { borderBottom: `2px solid ${color}`, }, '&:hover:not($disabled):before': { backgroundColor: `2px solid ${color}` }, '&$focused:after': { borderBottom: `2px solid ${color}` }, } }) textField = props => ( <TextField placeholder="temp input" InputProps={{ classes:{ root: props.classes.underline }, style:{ height: '1.5rem', fontSize:'1rem', marginTop: '-1rem', } }} onClick={()=>this.props.onClick()} onChange={(evt)=>{this.props.onChange(evt)}} > <div> {props.children} </div> </TextField> ) StyledTextField = withStyles(this.styles(this.props.color))(this.textField) render(){ return( <div> <this.StyledTextField>{this.props.children}</this.StyledTextField> </div> ); } } class StyledInput extends Component { render(){ return( <MainContext.Consumer> {stateData => { return( <StyledInputInner color={stateData.state.InputColor} onChange={(evt)=>this.props.onChange(evt)} onClick={this.props.onClick} > {this.props.children} </StyledInputInner> ) }} </MainContext.Consumer> ) } } export default StyledInput;
这是使用新的钩子语法执行此操作的示例:
index.js
import React from "react"; import ReactDOM from "react-dom"; import StyledComponent from "./StyledComponent"; const CustomComponent = ({ children, className }) => { return ( <p className={className}> Just showing passing in the component to use rather than automatically using a div. <br /> {children} </p> ); }; function App() { return ( <div className="App"> <StyledComponent color="green"> Here's my content with green underline </StyledComponent> <StyledComponent component={CustomComponent} color="blue" hoverColor="orange" > Here's my content with blue underline that changes to orange on hover. </StyledComponent> </div> ); } const rootElement = document.getElementById("root"); ReactDOM.render(<App />, rootElement);
StyledComponent.js
import React from "react"; import { makeStyles } from "@material-ui/styles"; const useStyles = makeStyles({ root: { borderBottom: ({ color }) => `2px solid ${color}`, "&:hover": { borderBottom: ({ color, hoverColor }) => { const borderColor = hoverColor ? hoverColor : color; return `2px solid ${borderColor}`; } } } }); const StyledComponent = ({ component: ComponentProp = "div", children, color, hoverColor }) => { const classes = useStyles({ color, hoverColor }); return <ComponentProp className={classes.root}>{children}</ComponentProp>; }; export default StyledComponent;
如果您愿意,可以将此useStyles方法放在自己的文件中,然后将其用作自定义钩子,以使它生成的类(仍然具有变量支持)可用于多个组件(而不仅仅是StyledComponent)。
useStyles
StyledComponent