【问题标题】:Reactjs material-ui TextField change color label and underline activity field inputReactjs material-ui TextField 更改颜色标签和下划线活动字段输入
【发布时间】:2019-11-10 04:41:41
【问题描述】:

<TextField
    id="standard-full-width"
    label="Password"
    style={{ margin: 8 }}
    fullWidth
    margin="normal"
    placeholder="*******"
/>

当焦点在输入字段上激活时,我无法弄清楚如何更改标签和下划线的颜色。

一些建议?

【问题讨论】:

标签: javascript reactjs input material-ui


【解决方案1】:

您可以通过classes 属性提供样式来否决样式。我添加了一个使用makeStyles 挂钩的示例,但该属性也可以与withStyles HOC 提供的类一起使用。

import React from "react";
import { TextField } from "@material-ui/core";
import { makeStyles } from "@material-ui/core/styles";

const useStyles = makeStyles(theme => ({
  root: {
    "& label.Mui-focused": {
      color: "orange"
    },
    "& .MuiInput-underline:after": {
      borderBottomColor: "orange"
    }
  }
}));

function App() {
  const classes = useStyles();
  return <TextField label="My label" classes={classes} />;
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

所以,如果您使用的是Component,它将是这样的:

import React from "react";
import { TextField } from "@material-ui/core";
import { withStyles } from "@material-ui/core/styles";

const styles = theme => ({
    root: {
        "& label.Mui-focused": {
          color: "orange"
        },
        "& .MuiInput-underline:after": {
          borderBottomColor: "orange"
        }
    }
})

class App extends React.Component {
    render() {
        return (
            <TextField label="My label" classes={this.props.classes} />
        )
    }
}

export default withStyles(styles)(App)

要了解如何自定义 TextField 组件,请查看以下示例:https://material-ui.com/components/text-fields/#customized-inputs

【讨论】:

  • 错误:无效的挂钩调用。钩子只能在函数组件的主体内部调用。这可能是由于以下原因之一:.....
  • 1.你可能有不匹配的 React 版本和渲染器(例如 React DOM) 2. 你可能违反了 Hooks 规则 3. 你可能在同一个应用程序中拥有多个 React 副本
  • 在你做的例子中被声明为一个App函数,我使用的被声明为一个类App扩展React.Component,因为里面我有一个状态,其他方法和渲染。
  • 是的,这就是为什么我说要使用withStyles 版本(并链接到文档)。但是我已经编辑了答案以包含组件所需的示例代码。希望这行得通。
  • 没有别的办法,问题我已经在项目中使用redux了。导出默认值 (LoginComponent = connect(mapStateToProps)(LoginComponent));
猜你喜欢
  • 1970-01-01
  • 2019-12-14
  • 2020-07-08
  • 2019-02-10
  • 2018-09-11
  • 1970-01-01
  • 1970-01-01
  • 2019-07-15
  • 1970-01-01
相关资源
最近更新 更多