将自定义单选按钮组件放在Material UI中的单选组中

dwj*_*ton 5 reactjs material-ui

我想要一个单选按钮列表,其中一个选项是自由格式的“其他”文本框,该框允许用户输入自己的文本。

这里有一个我想做的所有工作的沙箱:

https://codesandbox.io/s/r4oo5q8q5o

handleChange = event => {
    this.setState({
      value: event.target.value
    });
  };

  selectItem = item => {
    this.setState({
      selectedItem: item
    });
  };

  handleOtherChange = event => {
    this.setState({
      otherText: event.target.value
    });

    this.selectItem(
      //Todo put in right format
      this.state.otherText
    );
  };
  focusOther = () => {
    this.setState({
      value: "Other"
    });

    this.selectItem(this.state.otherText);
  };

  render() {
    const { classes, items } = this.props;
    const { value } = this.state;

    return (
      <div className={classes.root}>
        <Typography>
          {" "}
          Selected item is: {JSON.stringify(this.state.selectedItem)}
        </Typography>

        <FormControl component="fieldset" fullWidth>
          <RadioGroup value={this.state.value} onChange={this.handleChange}>
            {items.map(v => (
              <FormControlLabel
                value={v.name}
                control={<Radio />}
                label={v.name}
                key={v.name}
                onChange={() => this.selectItem(v)}
              />
            ))}

            <FormControlLabel
              value="Other"
              control={<Radio />}
              label={
                <TextField
                  placeholder="other"
                  onChange={this.handleOtherChange}
                  onFocus={this.focusOther}
                />
              }
              onChange={() => this.selectItem(this.state.otherText)}
            />
          </RadioGroup>
        </FormControl>
      </div>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

现在,我要做的就是将“其他”文本框设置为自己的组件。

这是我的尝试:

https://codesandbox.io/s/ryomnpw1o

export default class OtherRadioButton extends React.Component {
  constructor() {
    super();

    this.state = {
      text: null
    };
  }

  handleTextChange = event => {
    this.setState({
      text: event.target.value
    });
    this.props.onChange(this.state.text);
  };
  focusOther = () => {
    this.props.onFocus(this.props.value);
    this.props.onChange(this.state.text);
  };

  render() {
    return (
      <FormControlLabel
        value={this.props.value}
        control={<Radio />}
        label={
          <TextField
            placeholder="other"
            onChange={this.handleTextChange}
            onFocus={this.focusOther}
          />
        }
        onChange={this.focusOther}
      />
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

用于:

   <OtherRadioButton
      value="Other"
      onFocus={v => this.setState({ value: v})}
      onChange={v => this.selectItem(v)}
    />
Run Code Online (Sandbox Code Playgroud)

如您所见-自由文本的值正在向后传播-但RadioGroup似乎不知道FormGroupLabel的值。

为什么会这样,我将如何解决呢?

Ram*_*aro 6

您可以在此处查看 RadioGroup 源代码。我已经编写了自己的代码来更好地说明如何修复它。见这里:https : //codesandbox.io/s/mz1wn4n33j

RadioGroup 为其 FormControlLabel/RadioButton 子项创建一些道具。通过在不同的组件中创建您自己的自定义单选按钮,这些道具不会传递给 FormControlLabel/RadioButton。

您可以通过将道具传递给自定义 RadioButton 中的 FormControlLabel 来解决这些问题。

<FormControlLabel
    value={this.props.value}       //Pass this
    onChange={this.props.onChange} //Pass this one too
    checked={this.props.checked}   //Also this
    control={<Radio name="gender" />}
    label={
      <TextField
        id="standard-bare"
        defaultValue={this.props.defaultValue}
        margin="normal"
        onChange={this.props.onTextChange}
      />
    }
/>
Run Code Online (Sandbox Code Playgroud)