如何使用材料-ui对模型进行反应?

时间:2015-12-07 18:46:41

标签: reactjs material-ui

我尝试使用react和material-ui。使用此代码进行开发后:

'use strict';

var React   = require('react'),
ReactDOM    = require('react-dom'),
Router      = require('react-router'),
Icon        = require('react-fa'),
Firebase    = require('firebase'),
ReactFire   = require('reactfire'),
PeopleDao   = require('../../../common/dao'),
mui         = require('material-ui'),
IconButton  = mui.IconButton,
Checkbox    = mui.Checkbox,
TextArea    = mui.TextArea,
TextField   = mui.TextField,
SelectField = mui.SelectField;

require('./style');

module.exports = React.createClass({
    mixins: [Router.Navigation, Router.State, ReactFire],
    setApplicationDetails: function(pageData, item) {
        this.transitionTo('/people/view');
    },
    back: function() {
        event.preventDefault();
        this.transitionTo('/people/view');
    },
    save: function() {
        event.preventDefault();
        //console.log(self.getState());
        console.log(this.refs.txName.getValue());
    },
    getInitialState: function() {
        return {
            code: '',
            name: '',
            observation: '',
            genere: '0',
            active: true
        };
    },
    componentDidMount: function() {
        var self = this;
        var peopleId = this.props.params.peopleId ? parseInt(this.props.params.peopleId) : 0;
        var table = 'people';

        PeopleDao.findByCode(table, peopleId, function(object){
            self.setState(object.val());
        });
    },
    render: function() {
        var self = this;
        var peopleId = this.props.params.peopleId;

        return(
            <div className="editor">
                <TextField ref="txName" value={self.state.name} hintText="Nome" floatingLabelText="Nome" className="full-width"/><br/>
            </div>
        );
    }
});

enter image description here

当打开页面并编辑字段&#34; Nome&#34;时,这将被禁用。什么是我的错误?

2 个答案:

答案 0 :(得分:3)

当您指定value时,React会将该输入设为只读,除非您还添加onChange回调,例如:

<TextField ref="txName" 
value={self.state.name} 
hintText="Nome" 
floatingLabelText="Nome" 
className="full-width"
onChange={(event) => {this.setState(name: event.target.value);}}
/>

React文档中的更多信息:https://facebook.github.io/react/docs/forms.html

答案 1 :(得分:0)

我通过以下链接解决此问题:

<TextField value={self.state.code} hintText="Código" floatingLabelText="Código" className="full-width" disabled={true}/><br/>
<TextField valueLink={this.linkState('name')} hintText="Nome" floatingLabelText="Nome" className="full-width"/><br/>
<TextField valueLink={this.linkState('observation')} hintText="Observação" floatingLabelText="Observação" multiLine={true} className="full-width"/><br/>
<SelectField valueLink={this.linkState('genere')} hintText="Sexo" floatingLabelText="Sexo" menuItems={[
    {payload: 0, text: ''},
    {payload: 1, text: 'Masculino'},
    {payload: 2, text: 'Feminina'}
]}/><br/>
<Checkbox checkedLink={this.linkState('active')} label="Ativo?"/><br/>