0

I have 7 buttons radio types. Each button has a different style and it has to look different when checked=true. I want them to change their style when checked. This is my component responsible for creating a one button radio look

class RadioButton extends Component {
  constructor() {
    super();
    this.state = {
      checked: false,
    };

    this.handleChange = this.handleChange.bind(this);
  }

  handleChange() {
    const { checked } = this.state;
    this.setState({
      checked: !checked,
    });
  }

  render() {
    const {
      value,
      name,
      backgroundColor,
      backgroundColorChecked,
      borderColor,
      height,
      width,
      ...otherProps
    } = this.props;
    const { checked } = this.state;
    return (
      <InputGroup>
        <Input
          name={name}
          value={value}
          checked={checked}
          onChange={this.handleChange}
          style={{
            backgroundColor: checked ? `${backgroundColorChecked}` : `${backgroundColor}`,
            borderColor: `${borderColor}`,
            height: `${height}`,
            width: `${width}`,
          }}
          {...otherProps}
        />
      </InputGroup>
    );
  }
}

And this is my component responsible for creating a few button radio

 <FinalField name="rate">
          {({ input }) => (
            <FormGroup>
              <RadioButton
                value="capable"
                type="radio"
                backgroundColorChecked="#6baf8f"
                backgroundColor="#f5fffa"
                borderColor="#6baf8f"
                height="1.8125rem"
                width="1.8125rem"
                {...input}
              />
            </FormGroup>
          )}
        </FinalField>

In the console I gets two errors You must pass 'type="radio"' prop to your Field(rate) component. Without it we don't know how to unpack your 'value' prop - "undefined". and Failed prop type: The prop 'style' is marked as required in 'RadioButton', but its value is 'undefined'.

I do not know why these errors appear and how to fix them.Thanks for all the tips.

1 Answer 1

1

You must pass 'type="radio"' prop to your Field(rate) component.

This problem means that you need to add 'type="radio"' to input element in class RadioButton.

<Input
          type="radio"
          name={name}
          value={value}
          checked={checked}
          onChange={this.handleChange}
          style={{
            backgroundColor: checked ? `${backgroundColorChecked}` : `${backgroundColor}`,
            borderColor: `${borderColor}`,
            height: `${height}`,
            width: `${width}`,
          }}
          {...otherProps}
        />

Failed prop type:

I assume that you have defined propType in class RadioButton like this:

RadioButton.protoTypes = {
    style: ProtoTypes.object.isRequired
}

However,the style property isn't passed to the class RadioButton.

Sign up to request clarification or add additional context in comments.

7 Comments

no, that RadioButton.propTypes = { value: PropTypes.string.isRequired, name: PropTypes.string.isRequired, style: PropTypes.shape({ backgroundColor: PropTypes.string.isRequired, borderColor: PropTypes.string.isRequired, height: PropTypes.string.isRequired, width: PropTypes.string.isRequired, }).isRequired, };
you have defined style in RadioButton.propTypes.But it's not passed in <RadioButton/>
one problem solved - prop type: The prop 'style' is marked as required in 'RadioButton', but its value is 'undefined'
Where are are getting your Input element?The original input Element should be input not Input.
i have component QuestionsList.js and gets the data and transfers it as props QuestionsListItem.js and the radio button is transferred to this component
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.