Checkboxes are used for multiple choices, not for mutually exclusive choices.
import { Checkbox } from "@wfp/react"
<Checkbox id="check-1" labelText="Label text" />;
<InputGroup labelText="Input group" helperText="Describing the input group" > <Checkbox id="check-1" labelText="Option 1" /> <Checkbox id="check-2" labelText="Option 2" /> <Checkbox id="check-3" labelText="Option 3" /> </InputGroup>;
<InputGroup labelText="Input group" helperText="Describing the input group" vertical > <Checkbox id="check-1" labelText="Option 1" /> <Checkbox id="check-2" labelText="Option 2" /> <Checkbox id="check-3" labelText="Option 3" /> </InputGroup>;
const FormExample = () => { const [defaultValues, setDefaultValues] = useState( {} ); const { control, register, handleSubmit, watch, reset, } = useForm({ defaultValues }); const [data, setData] = useState({}); const setDefaultValuesFunc = (e) => { console.log(e.target.value); try { const values = JSON.parse(e.target.value); setDefaultValues(values); } catch (e) { console.log(e); } }; const resetInputs = () => { reset(defaultValues); }; const currentValues = watch(); return ( <> <form onSubmit={handleSubmit((data) => setData(data) )} > <InputGroup labelText="Input group" helperText="Describing the input group" > <Checkbox id="1" name="check-1" labelText="Option 1" {...register("check-1")} /> <Checkbox id="2" name="check-2" labelText="Option 2" {...register("check-2")} /> </InputGroup> <Button type="submit">Submit</Button>{" "} <Button onClick={resetInputs} kind="secondary"> Reset here </Button> <div className="debug"> <h4>Submitted form data</h4> <pre>{JSON.stringify(data, null, 2)}</pre> <h4>Current values</h4> <pre> {JSON.stringify(currentValues, null, 2)} </pre> <TextInput labelText="Default values (editable)" defaultValue={JSON.stringify( defaultValues )} onChange={setDefaultValuesFunc} /> </div> </form> </> ); }; render(<FormExample />);
The checkbox control allows for three states: selected, unselected, and indeterminate. The indeterminate state comes into play when the checkbox contains a sublist of selections, some of which are selected, and some unselected.
When the checkbox is in an invalid state, the user will be notified by an error message.
<Checkbox id="check-invalid" labelText="Accept terms and conditions" invalid invalidText="Please accept the terms and conditions" />;
Users should be able to select the checkbox by clicking on the box directly or by clicking on its label.
The default view of a set of checkboxes is having no option selected.
Checkbox input(1): It indicates the checkbox state. By default it is set to unselected.
Checkbox label(2): Describes the information you want to select or unselect.