简体   繁体   English

我无法在 Material-UI 中编辑文本字段

[英]I can't edit Text Field in Material-UI

I developed a React App using Material-UI then I tried to create independent Components ,我使用 Material-UI 开发了一个 React 应用程序,然后我尝试创建独立的组件

check the below independent components( <PanelDiv/> ),检查以下独立组件( <PanelDiv/> ),

render() {
        return (
            <div className="panelDiv-component" style={{display:this.props.display}}>
                <div className="panel-field-content">
                    <TextField
                        floatingLabelText={this.props.heading}
                        type={this.props.inputType}
                        value={this.props.value}
                    />
                   {/* <input  defaultValue className="form-control"></input>*/}
                </div>
            </div>
        );
    }

I tried to use the component like this,我尝试使用这样的组件,

<PanelDiv
    heading='name'
    inputType="text"
    value={this.state.userData.name}
    display={this.state.display[0]}
/>

But I can't update input field in this way.Also there is no error.但是我不能以这种方式更新输入字段。也没有错误。 How can i solve this?我该如何解决这个问题? I want to update my input field.我想更新我的输入字段。

Please check my input filed in the below image :请检查我在下图中提交的输入:

在此处输入图像描述

Because you are controlling the value of TextField by using value attribute but you are not updating the value by using onChange function, Since value of TextField is not changing so it becomes read only.因为您通过使用value属性来控制TextFieldvalue但您没有使用onChange函数更新该值,因为TextField值没有改变,所以它变为只读。

Solution:解决方法:

Specify the onChange function with TextField and update the value inside that, Like this:使用TextField指定onChange函数并更新其中的值,如下所示:

<TextField
    floatingLabelText={this.props.heading}
    type={this.props.inputType}
    value={this.props.value}
    onChange={this.props._change}
/>

Inside parent component:父组件内部:

_Change(event, value){
   //update the value here
}


<PanelDiv
    heading='name'
    inputType="text"
    value={this.state.userData.name}
    _change={this._change}
    display={this.state.display[0]}
/>

Had the same error.有同样的错误。 I was not able to key in anything and it was because there was no name props included.我无法键入任何内容,这是因为没有包含名称道具。 example:例子:

<TextField
  type="email"
  name='email'/>

Look at this example - https://jsfiddle.net/y857yeLq/看这个例子 - https://jsfiddle.net/y857yeLq/

You should define a function, which is handles of text change and pass it to onChange property.您应该定义一个函数,它是文本更改的句柄并将其传递给onChange属性。 In this example I used state for storing current text field value.在这个例子中,我使用 state 来存储当前的文本字段值。 I see, that you use props for that, but the principle is the same - function should update props in your case.我明白,您为此使用了道具,但原理是相同的 - 函数应该在您的情况下更新道具。

const { TextField, MuiThemeProvider, getMuiTheme } = MaterialUI;

 class SliderExampleControlled extends React.Component {

  state = {
    value: '',
  }

  render() {
    console.log(this.state);
    return (
      <div style={{width: '50%', margin: '0 auto'}}>
        <TextField
          hintText="Type here"
          value={this.state.value}
          onChange={(e) => this.setState(e.target.value)}
        />
      </div>
    );
  }

}

const App = () => (
  <MuiThemeProvider muiTheme={getMuiTheme()}>
    <SliderExampleControlled />
  </MuiThemeProvider>
);

ReactDOM.render(
  <App />,
  document.getElementById('container')
);

If you pass value as a prop to TextField you can't change that text!如果您将 value 作为道具传递给 TextField,则无法更改该文本! On Material-UI official documentation they have used defaultValue="default val" as a prop.Material-UI 官方文档中,他们使用 defaultValue="default val" 作为道具。 So I used defaultValue as a prop!所以我使用 defaultValue 作为道具! It worked fine for me!它对我来说很好用!

<TextField 
   type="text"
   defaultValue={this.props.val}
   onChange={handleChange} 
  />

I was running into this problem as well and I needed the onClick function to take more than just the event I also needed it to take the row number because my state had an array representing the rows in a table.我也遇到了这个问题,我需要 onClick 函数来获取更多的事件,我还需要它来获取行号,因为我的状态有一个表示表中行的数组。 I was able to do that using this chunk of code我能够使用这段代码做到这一点

onChange={(e) => this.nameChange(e.target.value, row.row_num)}

then in my function called nameChange I was able to update the value然后在我的名为 nameChange 的函数中,我能够更新值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM