【发布时间】:2018-12-13 22:37:02
【问题描述】:
我有 7 个按钮单选类型。每个按钮都有不同的样式,并且在checked=true 时必须看起来不同。我希望他们在检查时改变他们的风格。
这是我负责创建单按钮单选外观的组件
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>
);
}
}
这是我负责创建几个按钮单选的组件
<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>
在控制台中我收到两个错误You must pass 'type="radio"' prop to your Field(rate) component.
Without it we don't know how to unpack your 'value' prop - "undefined".
和Failed prop type: The prop 'style' is marked as required in 'RadioButton', but its value is 'undefined'。
我不知道为什么会出现这些错误以及如何修复它们。感谢所有提示。
【问题讨论】:
标签: reactjs radio-button