根据React中的切换值设置文本字段的禁用/启用值

时间:2018-08-16 14:56:40

标签: reactjs sharepoint office-fabric

我正在通过Office Fabric创建一个面板:

  <div className={ styles.column }>
    <DefaultButton 
      onClick={this._showJobCreatePanel.bind(this)} text="Open Panel" 
    />
    <Panel
      isOpen={this.state.shouldShowJobCreatePanel}
      type={PanelType.smallFixedFar}
      onDismiss={this._hideJobCreatePanel.bind(this)}
      headerText="Create new Delivery Job"
      closeButtonAriaLabel="Cancel"
      onRenderFooterContent={this._onRenderFooterContent.bind(this)}
    >
      <ChoiceGroup
        options={trucksGroupOptions}
        required={true}
        label="Trucks"
        onChange={this._onChangeTruckChoice.bind(this)}
        style={divStyle} 
      ></ChoiceGroup>
      <Toggle
        defaultChecked={false}
        label="Out of Service"
        onText="Yes"
        offText="No"
        onChanged={newValue => {this._jobToCreate.OutOfService = newValue; this.setState({shouldDisableFields: newValue});}}
      ></Toggle>
      <TextField 
        disabled={this.state.shouldDisableFields} 
        required={!this.state.shouldDisableFields} 
        placeholder="Enter Customer" 
        label="Customer" 
        onChanged = {newValue => {this._jobToCreate.Title = newValue;}}
      ></TextField>
      <div style = {divStyle}></div>
    </Panel>
  </div>      

如果切换设置为true,我想禁用文本字段。我拥有的代码有效,我切换为true,并且禁用了文本字段。

我遇到的问题是,当我致电setState时,它正在重新绘制面板,而我失去了ChoiceGroup的用户输入。

我认为我可以更改IChoiceGroupOption[]组的值:

  <Toggle
    defaultChecked={false}
    label="Out of Service"
    onText="Yes"
    offText="No"
    onChanged={newValue => {
      for (let truckInOptions of trucksGroupOptions) {
        if (truckInOptions.key === this._jobToCreate.Truck) {
          truckInOptions.checked = newValue;
        }
      }
      console.log("truksGroupOptions: ", trucksGroupOptions);
      this._jobToCreate.OutOfService = newValue; 
      this.setState({shouldDisableFields: newValue});
    }}
  ></Toggle>

Checked更改为true,但是ChoiceGroup仍在清除选择。

如何根据切换值启用/禁用文本字段?

0 个答案:

没有答案