UNPKG

metadata-based-explorer1

Version:
66 lines (52 loc) 1.55 kB
// @flow import * as React from 'react'; import RadioButton from './RadioButton'; type Props = { children: React.Node, className: string, name?: string, onChange?: Function, value?: string, }; type State = { value?: string, }; class RadioGroup extends React.Component<Props, State> { static defaultProps = { className: '', }; constructor(props: Props) { super(props); this.state = { value: props.value, }; } // @TODO: think about adding componentDidUpdate or gDSFP // to update the internal state value based on new props value onChangeHandler = (event: SyntheticEvent<>) => { const { target } = event; const { onChange } = this.props; if (target instanceof HTMLInputElement) { this.setState({ value: target.value, }); } if (onChange) { onChange(event); } }; render() { const { children, className, name } = this.props; const { value: stateValue } = this.state; return ( <div className={`radio-group ${className}`} onChange={this.onChangeHandler}> {React.Children.map(children, radio => { const { value, ...rest } = radio.props; return <RadioButton isSelected={value === stateValue} name={name} value={value} {...rest} />; })} </div> ); } } export type RadioGroupProps = Props; export default RadioGroup;