更新一对文本字段中的值

时间:2017-03-29 17:19:53

标签: forms reactjs material-ui

我有一个表单组件:

import React from 'react'
import Relay from 'react-relay'

import { browserHistory } from 'react-router'

import MenuItem from 'material-ui/MenuItem'
import TextField from 'material-ui/TextField'

import CreateTransformationSetMutation from '../mutations/CreateTransformationSetMutation'

class CreateTransformationSetDialog extends React.Component {

  componentWillMount() {
    this.props.setOnDialog({
      onSubmit: this.onSubmit,
      title: 'Create and Apply Transformation Set'
    })
  }

  initial_state = {
    targetTableName: '',
    transformations: this.props.viewer.version.columns.edges.map(edge => edge.node).map(column => {
      return {
        targetColumnName: column.name, 
        ruleExpression: '{{' + column.name + '}}'
      }
    })
  }

  state = this.initial_state

  onSubmit = () => {
    const onSuccess = (response) => {
      browserHistory.push('/table')
    }

    const onFailure = () => {}

    Relay.Store.commitUpdate(
      new CreateTransformationSetMutation(
        {
          viewer: this.props.viewer,
          version: this.props.viewer.version,
          targetTableName: this.state.targetTableName,
          transformations: JSON.stringify({transformations: this.state.transformations}),
        }
      ),
      { onSuccess: onSuccess }
    )
  }

  handleTableNameChange = (e) => {
    this.setState({
      targetTableName: e.target.value
    })
  }

  handleTransChange = (e) => {

    ////what should go here////

  }

  render() {
    return (
      <div>
        <TextField
          floatingLabelText="Table Name"
          value={this.state.targetTableName}
          onChange={this.handleTableNameChange} 
        />
        <br />
        {
          this.props.viewer.version.columns.edges.map((edge) => edge.node).map((column, i) =>
            <div key={column.id}>
              <TextField
                name="targetColumnName"
                floatingLabelText="Target Column"
                value={this.state.transformations[i].targetColumnName}
                onChange={this.handleTransChange}
                style={{ margin: 12 }} 
              />
              <TextField
                name="ruleExpression"
                floatingLabelText="Rule to Apply"
                value={this.state.transformations[i].ruleExpression}
                onChange={this.handleTransChange}
                style={{ margin: 12 }}
              />
            </div>
          )
        }

      </div>
    )
  }

}

export default Relay.createContainer(CreateTransformationSetDialog, {
  fragments: {
    viewer: () => Relay.QL`
      fragment on Viewer {
        ${CreateTransformationSetMutation.getFragment('viewer')}
        version(id: $modalEntityId) @include(if: $modalShow) {
          ${CreateTransformationSetMutation.getFragment('version')}
          id
          name
          columns(first: 100) {
            edges {
              node {
                id
                name
              }
            }
          }
        }
      }
    `
  },
  initialVariables: {
    modalEntityId: '',
    modalName: '',
    modalShow: true,
  },
  prepareVariables: ({ modalEntityId, modalName }) => {
    return {
      modalEntityId,
      modalName,
      modalShow: modalName === 'createTransformationSet'
    }
  }
})

每个输入都是一组两个材料-ui TextField,它们具有从this.state.transformations获得的默认值。但是,我正在尝试更新这些字段中的值,我没有运气。

默认的转换状态如下所示:

transformations: [
  {targetColumnName: 'ID', ruleExpression: '{{ID}}'},
  {targetColumnName: 'First Name', ruleExpression: '{{FirstName}}'},
  {targetColumnName: 'Last Name', ruleExpression: '{{LastName}}'}
]

我希望能够更新它以便状态可以改变,例如:

transformations: [
  {targetColumnName: 'ID', ruleExpression: '{{ID}}'},
  {targetColumnName: 'First Name', ruleExpression: '{{FirstName}}'},
  {targetColumnName: 'Percentage', ruleExpression: '{{Percentage/100}}'}
]

表单是使用Relay构建的,但与此问题无关。

非常感谢任何帮助。

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,而是将值放在value属性中,使用defaultValue='some-value'以便可以对其进行变更和更改。

您似乎无法保护XSS(跨站点脚本)

像这样写:

<TextField
  name="targetColumnName"
  floatingLabelText="Target Column"
  defaultValue={this.state.transformations[i].targetColumnName} {/* change from value to defaultValue */}
  onChange={this.handleTransChange}
  style={{ margin: 12 }} 
/>