Skip to content

Commit

Permalink
add radio input
Browse files Browse the repository at this point in the history
  • Loading branch information
Beth Shook committed Mar 14, 2024
1 parent b118503 commit 262838f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/components/forms/controlled/inputTypes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@ export const TextArea = (props: MakeControlled<typeof Uncontrolled.TextArea>) =>
/>;
};

export const Radio = (props: MakeControlled<typeof Uncontrolled.Radio>) => {
const {data, namespace, setInput} = useFormHelpers();

const onChangeValue = (value: boolean | undefined) => {
props.onChangeValue?.(value);
setInput.field(props.name)(value);
};

const checked = data[props.name];
useEmptyDisabledValue(props, checked, onChangeValue);

return <Uncontrolled.Radio
{...props}
name={namespace + '.' + props.name}
checked={!!checked}
onChangeValue={onChangeValue}
/>;
};

export const Checkbox = (props: MakeControlled<typeof Uncontrolled.Checkbox>) => {
const {data, namespace, setInput} = useFormHelpers();

Expand Down
31 changes: 31 additions & 0 deletions src/components/forms/uncontrolled/inputTypes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,37 @@ export const Select = ({
</FormInputWrapper>;
};

/*
* radio element
*/
type RadioProps = React.ComponentPropsWithoutRef<'input'> & InputProps & {
onChangeValue?: (value: boolean | undefined) => void;
wrapperProps?: React.ComponentPropsWithoutRef<'label'>;
};
const RadioLine = styled.div`
flex-direction: row;
display: flex;
align-items: center;
`;
export const Radio = ({
label,
help,
wrapperProps,
onChangeValue,
...props
}: RadioProps) => {
return <FormInputWrapper {...wrapperProps}>
<RadioLine>
<input type="radio" {...props} onChange={e => {
onChangeValue?.(!!e.target.checked);
props.onChange?.(e);
}}/>
<FormLabelText><RequiredIndicator show={props.required} />{label}</FormLabelText>
</RadioLine>
<HelpText value={help} />
</FormInputWrapper>;
};

/*
* checkbox element
*/
Expand Down

0 comments on commit 262838f

Please sign in to comment.