Checkbox
Allow users to select one or more items from a list
#Examples
#Base checkbox
This interactive input control indicates the current state. By default, it is unchecked.
const [checked, setChecked] = useState(false);
return <BaseCheckbox checked={checked} onChange={(e) => setChecked(e.target.checked)} />;#Checkbox
Provide a label indicating to users what can be selected or deselected.
const [checked, setChecked] = useState(false);
return (
<Checkbox value="value" checked={checked} onChange={(e) => setChecked(e.target.checked)}>
Label
</Checkbox>
);#Checkbox group
When using CheckboxGroup, provide a group label to indicate the category of the options and explain what can be selected.
Default state:
by default, checkboxes are unchecked, meaning no options have been selected. Use checked state when the item is selected.Indeterminate state:
use the indeterminate state when you have a list of selections, where some items are are selected, and some are unselected.Disabled state:
when the user expects a predefined set of options, a disabled option is useful to indicate that normally that option would be available, but now it is not available due to a specific condition. If possible, provide hint text or a visual cue to checkbox is disabled to avoid confusion for the user.Label with dynamic text:
be aware of how the labels will resize and how any visuals surrounding them will be affected.Label with icon or tooltip:
help the user understand the context of the checkbox.
const [selected, setSelected] = useState<string[]>([]);
return (
<CheckboxGroup onChange={setSelected} value={selected}>
<CheckboxGroup.Checkbox value="option-1">Regular option</CheckboxGroup.Checkbox>
<CheckboxGroup.Checkbox value="option-2" indeterminate>
Indeterminate option
</CheckboxGroup.Checkbox>
<CheckboxGroup.Checkbox value="option-3" disabled>
Disabled option
</CheckboxGroup.Checkbox>
<CheckboxGroup.Checkbox value="option-4">
<Icon>
<IconApprove />
</Icon>
<InlineText>Option with left icon</InlineText>
</CheckboxGroup.Checkbox>
<CheckboxGroup.Checkbox value="option-5">
{(props: { checked: boolean; disabled: boolean }) => (
<>
<InlineText
emphasis={props.checked ? "medium" : "normal"}
lineHeight="multi-line"
tone={props.disabled ? "subtle" : undefined}
>
Option with right icon and dynamic text style
</InlineText>
<Icon>
<IconApprove />
</Icon>
</>
)}
</CheckboxGroup.Checkbox>
<Tooltip variant={{ type: "interactive" }} content="Extra info on the option 5">
<CheckboxGroup.Checkbox value="option-6">Option with tooltip</CheckboxGroup.Checkbox>
</Tooltip>
</CheckboxGroup>
);#Horizontal checkbox group
Make a horizontal group by using the direction prop on the CheckboxGroup component.
const [selected, setSelected] = useState<string[]>([]);
return (
<CheckboxGroup onChange={setSelected} value={selected} direction="horizontal">
<CheckboxGroup.Checkbox value="option-1">Regular option</CheckboxGroup.Checkbox>
<CheckboxGroup.Checkbox value="option-2" indeterminate>
Indeterminate option
</CheckboxGroup.Checkbox>
<CheckboxGroup.Checkbox value="option-3" disabled>
Disabled option
</CheckboxGroup.Checkbox>
<CheckboxGroup.Checkbox value="option-4">
<Icon>
<IconApprove />
</Icon>
<InlineText>Option with left icon</InlineText>
</CheckboxGroup.Checkbox>
<Tooltip variant={{ type: "interactive" }} content="Extra info on the option 5">
<CheckboxGroup.Checkbox value="option-6">Option with tooltip</CheckboxGroup.Checkbox>
</Tooltip>
</CheckboxGroup>
);#Properties
#Base checkbox
| Property | Description | Defined | Value |
|---|---|---|---|
checkedRequired | booleanDetermines if the checkbox appears checked or not | ||
onChangeRequired | functionEvent for when the state of the checkbox changes | ||
idOptional | stringId applied to the form control | ||
onBlurOptional | functionCallback for onBlur event | ||
aria-describedbyOptional | stringID of an an element that describes what the form control is for | ||
aria-labelOptional | stringLabel of the form control | ||
aria-labelledbyOptional | stringID of an an element that labels this form control | ||
valueOptional | stringValue of the form control | ||
nameOptional | stringName applied to the form control | ||
invalidOptional | booleanIs the form control invalid | ||
disabledOptional | booleanDisables the checkbox, prevent it from changing state | ||
indeterminateOptional | booleanShows the checkbox as neither checked nor unchecked | ||
data-observe-keyOptional | stringUnique string, used by external script e.g. for event tracking | ||
classNameOptional | stringCustom className that's applied to the outermost element (only intended for special cases) | ||
styleOptional | objectStyle object to apply custom inline styles (only intended for special cases) | ||
tabIndexOptional | numberTab index of the outermost HTML element of the component | ||
onKeyDownOptional | functionCallback for onKeyDown event | ||
onMouseDownOptional | functionCallback for onMouseDown event | ||
onMouseEnterOptional | functionCallback for onMouseEnter event | ||
onMouseLeaveOptional | functionCallback for onMouseLeave event | ||
onFocusOptional | functionCallback for onFocus event |
#Checkbox
| Property | Description | Defined | Value |
|---|---|---|---|
onChangeRequired | functionEvent for when the state of the checkbox changes | ||
checkedRequired | booleanDetermines if the checkbox appears checked or not | ||
valueRequired | stringValue of the form control | ||
idOptional | stringId applied to the form control | ||
disabledOptional | booleanDisables the checkbox, prevent it from changing state | ||
classNameOptional | stringCustom className that's applied to the outermost element (only intended for special cases) | ||
onKeyDownOptional | functionCallback for onKeyDown event | ||
onBlurOptional | functionCallback for onBlur event | ||
onMouseDownOptional | functionCallback for onMouseDown event | ||
aria-describedbyOptional | stringID of an an element that describes what the form control is for | ||
aria-labelOptional | stringLabel of the form control | ||
aria-labelledbyOptional | stringID of an an element that labels this form control | ||
data-observe-keyOptional | stringUnique string, used by external script e.g. for event tracking | ||
styleOptional | objectStyle object to apply custom inline styles (only intended for special cases) | ||
nameOptional | stringName applied to the form control | ||
tabIndexOptional | numberTab index of the outermost HTML element of the component | ||
onMouseEnterOptional | functionCallback for onMouseEnter event | ||
onMouseLeaveOptional | functionCallback for onMouseLeave event | ||
onFocusOptional | functionCallback for onFocus event | ||
invalidOptional | booleanIs the form control invalid | ||
indeterminateOptional | booleanShows the checkbox as neither checked nor unchecked | ||
childrenOptional | element |
#Checkbox group
| Property | Description | Defined | Value |
|---|---|---|---|
valueRequired | string[]Value of the form control | ||
onChangeRequired | functionCallback for onChange event | ||
nameOptional | stringName applied to the form control | ||
idOptional | stringId applied to the form control | ||
invalidOptional | booleanIs the form control invalid | ||
onBlurOptional | functionCallback for onBlur event | ||
aria-labelOptional | stringLabel of the form control | ||
aria-describedbyOptional | stringID of an an element that describes what the form control is for | ||
aria-labelledbyOptional | stringID of an an element that labels this form control | ||
childrenOptional | element | ||
directionOptional | "horizontal" | "vertical" | ||
classNameOptional | stringCustom className that's applied to the outermost element (only intended for special cases) | ||
styleOptional | objectStyle object to apply custom inline styles (only intended for special cases) |
#Guidelines
#Best practices
#Do not use when
#Accessibility
Explore detailed guidelines for this component: Accessibility Specifications
#Writing
#Notable changes
#Migrating from FancyLab 16.x to FancyLab 17.x
The CheckboxGroup component on FancyLab 16.x or prior takes an array of control objects as input/property, which is terse and works well with typechecking, but it plays poorly with composition. As an example, adding a tooltip to a checkbox label is not possible using this structure as well as changing the icon position to after or before the label.
So, in FancyLab 17.x, the CheckboxGroup component was refactored to allow composing other components with them. Now, instead of having a controls object, subcomponents are used to create the controls.
Please find below an example that illustrates the usage before and after these changes. Note that the controls property has been replaced by children of type CheckboxGroup.Checkbox.
#Before
<CheckboxGroup
onChange={setSelected}
value={selected}
controls={[
{ label: "White Wine", value: "white-wine" },
{ label: "Red Wine", value: "red-wine" },
]}
/>
#After
<CheckboxGroup onChange={setSelected} value={selected}>
<CheckboxGroup.Checkbox value="white-wine">White Wine</CheckboxGroup.Checkbox>
<CheckboxGroup.Checkbox value="red-wine">Red Wine</CheckboxGroup.Checkbox>
</CheckboxGroup>