The Problem:
What I'm trying to do:
WrapperRadio
component that sets up Controller
and then renders my atomic RadioButton
component. I then use WrapperRadio
within RegistrationForm
and want to be able to see the selected radio button value in the console when I hit the GetValues or Submit button.Examples:
Code Sandbox
<form onSubmit={onSubmit}>
<WrapperRadio
control={control}
name="radioSelection"
label="This is a label"
/>
...
</form>
return (
<div>
<Controller
control={control}
name={name}
render={({ field }) => <RadioButton {...field} label={label} />}
/>
</div>
);
return (
<fieldset className="max-w-sm">
<input
ref={ref}
id={name}
name={name}
type="radio"
aria-label={label}
{...props}
/>
<label htmlFor={name} className="block text-sm font-medium text-gray-700">
{label}
</label>
</fieldset>
);
Use WrapperRadio in RegistrationForm - you should pass in a default value.
<form onSubmit={onSubmit}>
<WrapperRadio
control={control}
name="radioSelection"
label="This is a label"
value={true}
/>
</form>
Using Controller in WrapperRadio
return (
<div>
<Controller
control={control}
name={name}
render={({ field: {onChange, ...props} }) =>
<RadioButton
onChange={e => onChange(e.target.checked)}
{...props} label={label} />}
/>
</div>