ComponentsNewUpdated
This component is new!
This component is updated!
Examples
A simple checkbox
<Checkbox> Jeg liker tog </Checkbox>
A group of checkboxes, with default checked
<CheckboxGroup defaultValue={["train", "ship"]} gap={3}> <Checkbox value="train">Train</Checkbox> <Checkbox value="bus">Bus</Checkbox> <Checkbox value="ship">Ship</Checkbox> </CheckboxGroup>
Different states and vertical layout
<CheckboxGroup direction="column"> <Checkbox disabled>Disabled</Checkbox> <Checkbox checked disabled> Disabled </Checkbox> <Checkbox readOnly>Readonly</Checkbox> <Checkbox invalid>Invalid</Checkbox> </CheckboxGroup>
A controlled checkbox
() => { const [checked, setChecked] = React.useState(false) return ( <Checkbox checked={checked} onCheckedChange={(e) => setChecked(!!e.checked)} > Check me </Checkbox> ) }
With the CheckboxGroup and Fieldset components
<Fieldset> <CheckboxGroup defaultValue={['train', 'bus']} direction="column"> <FieldsetLegend> Choose transportation </FieldsetLegend> <FieldsetContent display="flex" flexDirection="column" gap={1}> <Checkbox value="train">Train</Checkbox> <Checkbox value="boat">Boat</Checkbox> <Checkbox value="walking">Walking</Checkbox> <Checkbox value="bus">Bus</Checkbox> <Checkbox value="flying">Flying</Checkbox> </FieldsetContent> </CheckboxGroup> </Fieldset>
Guidelines
There are some things that are recommended when using checkboxes:
- Use checkboxes when users can select multiple options.
- If there are many options, consider using a select menu or combobox instead of checkboxes.
- Arrange checkboxes vertically rather than horizontally to improve readability and scanning.
- Order options alphabetically when all options are equally relevant.
- Avoid disabled checkboxes. Consider removing the option instead, or give an explanation to why the option is disabled.
Code
<Checkbox />
import { Checkbox } from "@vygruppen/spor-react";
A checkbox
Props
Name | Type | Required? | Description |
|---|---|---|---|
checked | boolean | ||
defaultChecked | boolean | ||
disabled | boolean | ||
invalid | boolean | ||
onCheckedChange | (details: CheckedChangeDetails) => void | Callback for when the checkbox changes | |
value | string | The returned value | |
form | string | The id of the form that the checkbox belongs to. | |
id | string | The unique identifier of the machine. | |
ids | Partial<{ root: string hiddenInput: string control: string label: string }> | The ids of the elements in the checkbox. Useful for composition. | |
name | string | The name of the input field in a checkbox. Useful for form submission. | |
readOnly | boolean | ||
required | boolean |
<CheckboxGroup />
import { CheckboxGroup } from "@vygruppen/spor-react";
Groups Checkbox components
Props
Name | Type | Required? | Description |
|---|---|---|---|
defaultValue | string[] | Array of initially checked values | |
onCheckedChange | function | Callback for when the checkbox selection changes | |
disabled | boolean | ||
direction | "row" | "column" | "row-reverse" | "column-reverse" | Default "row" | |
gap | string | number | Space between checkboxes. Default is 1 |