更新动态表单数据-JavaScript / React

时间:2018-11-26 04:55:40

标签: javascript arrays reactjs forms

我有两个输入字段。我还将Fabric UI用作此应用程序的UI框架。我需要能够在提交时更新视图。我正在将道具设置为组件中的状态。设置好之后,然后将这些值传递给视图。不确定如何检索和更新所述值。

{
        isOpened: false,
        label: "Home Address",
        labelId: 2,
        values:
            [
                {field: "homeAddress1", value: "2343 Main Street"},
                {field: "homeAddress2", value: "New York, NY"}
            ]
    }

我正在提交时更新每个对象中的值。

class Info extends Component {
  constructor (props) {
    super(props)
    this.state = {
      isOpened: this.props.info.isOpened,
      information: this.props.info
    }
    this.toggleEdit = this.toggleEdit.bind(this)
    this.handleChange = this.handleChange.bind(this)
    this.handleSubmit = this.handleSubmit.bind(this)
  }

  toggleEdit () {
    const { isOpened } = this.state
    this.setState({
      isOpened: !isOpened
    })
  }

  handleSubmit(event) {
    console.log('event', this.refs.prefName.value)
    event.preventDefault();

  }

  handleChange(event) {
      console.log('logging state', this.state)
      this.state.information.values.forEach(item => {
        // const { value } = item.value  
        // this.setState({value: event.target.value})
      })
    //   this.setState({value: event.target.value})
    console.log('event on change', event.target.value)
  }

  render () {
    const { isOpened } = this.state
    const { information } = this.state

    console.log(this.state, 'logging state')
    return (
      <div className="profile--info-wrapper">
        <h2 className="profile--sub-header--light">
          {information.label}
          {information.editField && (
            <Icon
              iconName="Edit"
              className={
                'ms-IconExample ' +
                (!isOpened ? 'pencil-icon' : 'pencil-icon-edit')
              }
              onClick={this.toggleEdit}
            />
          )}
        </h2>
        <form className="profile--form">
          {information.values.map(item => {
            return !isOpened ? (
              <FieldWrapper item={item} key={item.field} />
            ) : item.field === 'emergencyContactNumber' ||
            item.field === 'phoneNumber' ? (
                <MaskedTextField
                  label={item.fieldName}
                  mask="999-999-9999"
                  value={item.value}
                  key={item.field}
                  ref={item.field}
                />
              ) : (
                <TextField
                  label={item.fieldName}
                  value={item.value}
                  key={item.field}
                  onChange={this.handleChange}
                  ref={item.field}
                />
              )
          })}
          {isOpened && (
            <div className="ms-Grid-row button--wrapper">
              <PrimaryButton
                data-automation-id="test"
                text="Save"
                onClick={this.handleSubmit}
                allowDisabledFocus={true}
              />
              <DefaultButton
                data-automation-id="test"
                text="Cancel"
                onClick={this.toggleEdit}
              />
            </div>
          )}
        </form>
      </div>
    )
  }
}

0 个答案:

没有答案