diff --git a/assets/js/src/common/form/radio/group.tsx b/assets/js/src/common/form/radio/group.tsx new file mode 100644 index 0000000000..ca5bdf3b5b --- /dev/null +++ b/assets/js/src/common/form/radio/group.tsx @@ -0,0 +1,51 @@ +import React, { InputHTMLAttributes, useState } from 'react'; +import Radio from './radio'; + +type RadioValueType = string | string[] | number; + +type RadioProps = InputHTMLAttributes & { + label: string, +} + +type Props = { + name: string, + options: RadioProps[], + defaultValue?: RadioValueType, + isFullWidth?: boolean, + onChange?: (RadioValueType) => void, +}; + +const RadioGroup = ({ + name, + options, + defaultValue, + isFullWidth, + onChange, +}: Props) => { + const [currentValue, setCurrentValue] = useState(defaultValue); + + const handleChange = (value: RadioValueType) => { + setCurrentValue(value); + onChange(value); + }; + + return ( +
+ {options.map(({ label, value, ...attributes }: RadioProps) => ( + handleChange(value)} + isFullWidth={isFullWidth} + {...attributes} // eslint-disable-line react/jsx-props-no-spreading + > + {label} + + ))} +
+ ); +}; + +export default RadioGroup; diff --git a/assets/js/src/common/form/radio/radio.tsx b/assets/js/src/common/form/radio/radio.tsx new file mode 100644 index 0000000000..9e1d6ae37f --- /dev/null +++ b/assets/js/src/common/form/radio/radio.tsx @@ -0,0 +1,34 @@ +import React, { InputHTMLAttributes } from 'react'; +import classnames from 'classnames'; + +type Props = InputHTMLAttributes & { + children?: React.ReactNode, + isFullWidth?: boolean, + onChange?: (boolean) => void, +}; + +const Radio = ({ + children, + isFullWidth, + onChange, + ...attributes +}: Props) => ( + +); + +export default Radio;