【问题标题】:how to change the height of Material-UI textfield without using react hooks如何在不使用反应钩子的情况下更改 Material-UI 文本字段的高度
【发布时间】:2019-11-17 11:27:27
【问题描述】:

我需要将一个类组件中的多行material-UI TextField的高度改长一些,但是我在SO上找到的之前的examples似乎使用了功能组件和钩子。

我的代码可以在下面或这个sandbox中找到

import React, { Component } from "react";
import ReactDOM from "react-dom";
import TextField from "@material-ui/core/TextField";


class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      year: "2010",
      otherAttributes: ""
    };
  }

  handleChangefor = (propertyName) => (event) => {
    this.setState({
      ...this.state,
      [propertyName]: event.target.value
    })
  }

  render() {
    return (
      <div>
        <p>text field below </p>

        <TextField
          id="outlined-multiline-flexible"
          label="year"
          multiline
          rowsMax="10"
          value={this.state.year}
          onChange={this.handleChangefor("year")}
          margin="normal"
          helperText=""
          variant="filled"
        />
      </div>
    );
  }
}


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

文档指向 'makeStyle' 和 'useStyle' hoc,但我找不到它们用于类组件的示例。

【问题讨论】:

    标签: reactjs material-ui


    【解决方案1】:

    您应该可以使用withStyles - 我必须使用minHeight ...仅使用height 对我不起作用..

    编辑:因为您在一个班级中询问多个TextFields,所以我更新了我的答案。

    带有单个文本字段的单个类:

    import React, { Component } from "react";
    import ReactDOM from "react-dom";
    import TextField from "@material-ui/core/TextField";
    import { withStyles } from "@material-ui/core/styles";
    
    const styles = {
      someTextField: {
        minHeight: 420
      }
    };
    
    
    class App extends Component {
      constructor(props) {
        super(props);
        this.state = {
          year: "2010",
          otherAttributes: "",
        };
      }
    
      handleChangefor = (propertyName) => (event) => {
        this.setState({
          ...this.state,
          [propertyName]: event.target.value
        })
      }
    
      render() {
        return (
          <div>
            <p>text field below </p>
    
            <TextField
              id="outlined-multiline-flexible"
              label="year"
              multiline
              rowsMax="10"
              value={this.state.year}
              onChange={this.handleChangefor("year")}
              margin="normal"
              helperText=""
              variant="filled"
              InputProps={{ classes: { input: this.props.classes.someTextField } }}
            />
          </div>
        );
      }
    }
    
    const StyledTextFieldApp = withStyles(styles)(App)
    
    const rootElement = document.getElementById("root");
    ReactDOM.render(<StyledTextFieldApp />, rootElement);
    

    具有多个文本字段的单个类:

    import React, { Component } from "react";
    import ReactDOM from "react-dom";
    import TextField from "@material-ui/core/TextField";
    import { withStyles } from "@material-ui/core/styles";
    
    const styles = {
      someTextField: {
        minHeight: 420
      },
      someOtherTextField: {
        minHeight: 120,
      }
    };
    
    
    class App extends Component {
      constructor(props) {
        super(props);
        this.state = {
          year: "2010",
          otherAttributes: "",
        };
      }
    
      handleChangefor = (propertyName) => (event) => {
        this.setState({
          ...this.state,
          [propertyName]: event.target.value
        })
      }
    
      render() {
        return (
          <div>
            <p>text field below </p>
    
            <TextField
              id="outlined-multiline-flexible"
              label="year"
              multiline
              rowsMax="10"
              value={this.state.year}
              onChange={this.handleChangefor("year")}
              margin="normal"
              helperText=""
              variant="filled"
              InputProps={{ classes: { input: this.props.classes.someTextField } }}
            />
            <TextField
              id="outlined-multiline-flexible"
              label="year"
              multiline
              rowsMax="10"
              value={this.state.year}
              onChange={this.handleChangefor("year")}
              margin="normal"
              helperText=""
              variant="filled"
              InputProps={{ classes: { input: this.props.classes.someOtherTextField } }}
            />
          </div>
        );
      }
    }
    
    const StyledTextFieldApp = withStyles(styles)(App)
    
    const rootElement = document.getElementById("root");
    ReactDOM.render(<StyledTextFieldApp />, rootElement);
    

    【讨论】:

    • 谢谢!这是否意味着 TextField 必须是它自己的组件,因此如果我打算将多个此类组件放在一个类中,它可以被包装在 withStyles(styles)(App) 中?
    • 如果你有一个包含多个 TextFields 的类,并且你希望每个 TextFields 有自己的风格 - 你只需要使用特定的风格来定位每个 TextFields CSS 类)名称。只需将const styles = { someTextField: {...} } 视为一个包含CSS 类的对象。This example shows how you could accomplish that
    • @santoku 没问题!乐意效劳。我还更新了我的答案,以反映如何使用单个类和多个 TextField 组件来处理这个问题。干杯!
    猜你喜欢
    • 2021-10-29
    • 2020-02-16
    • 2021-08-30
    • 2020-08-08
    • 1970-01-01
    • 2012-06-16
    • 2022-12-10
    • 2019-05-31
    • 2020-03-09
    相关资源
    最近更新 更多