css - 在 Material UI 中将变量发送到 withStyles?

标签 css reactjs material-ui wrapper higher-order-components

我有以下内容:

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;

因此,当用户单击该字段时,它成功地采用了 Material UI 文本字段并将底栏更改为白色,而不是蓝色。太棒了!

...然而...

我真正想做的是像

<this.Styled color={someDefinedColor}>{this.props.children}</this.Styled>

Styled 看起来像这样:

Styled = (color) => withStyles(this.styles(color))(this.div)

这样我就可以动态地将颜色传递给 Styled 属性。显然这是伪代码——我一直在玩它,但无法让它失败。作为一般性声明,material-ui 在动态更改颜色方面有点令人不快,所以我想知道是否有人知道如何让它工作。

谢谢!

最佳答案

这是一个如何使用新的 hook syntax 执行此操作的示例:

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;

Edit useStyles with variable

如果需要,您可以将此 useStyles 方法放在它自己的文件中,并将其重新用作自定义 Hook ,以使其生成的类(仍具有变量支持)可用于多个组件(而不仅仅是 StyledComponent)。

关于css - 在 Material UI 中将变量发送到 withStyles?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54832736/

相关文章:

css - 使用 3 个图像创建按钮

css - 为什么标签在输入框后面?

reactjs - "dispatch"未定义

javascript - 同构 React 与 Gatsby(静态站点)React

reactjs - TypeError : theme. 间距不是函数

reactjs - 如何在 React 自定义元素中为 MUI Material v5 创建插入点以在 shadow dom 中挂载样式

javascript - 如何制作根据特定输入数字变量变化的进度条?

javascript - Chrome 扩展中的 JQuery .animate 真的很不稳定

javascript - 更改路由时在 react.js 中获取一个非常神秘的消息

reactjs - Material UI Drawer 组件中如何包含主组件的宽度?