<Box display="grid" sx={{gap: 3}}><CheckboxGroup><CheckboxGroup.Label>Choices</CheckboxGroup.Label><FormControl><Checkbox value="one" /><FormControl.Label>Choice one</FormControl.Label></FormControl><FormControl><Checkbox value="two" /><FormControl.Label>Choice two</FormControl.Label></FormControl><FormControl><Checkbox value="three" /><FormControl.Label>Choice three</FormControl.Label></FormControl></CheckboxGroup></Box>
const WithOnChangeHandlers = () => {const [selectedCheckboxValues, setSelectedCheckboxValues] = React.useState(['one', 'two'])const [lastSelectedCheckboxValue, setLastSelectedCheckboxValue] = React.useState()const handleCheckboxGroupChange = (selectedValues, e) => {setSelectedCheckboxValues(selectedValues)setLastSelectedCheckboxValue(e.currentTarget.value)}const handleChoiceOneChange = e => {alert('Choice one has its own handler')}return (<Box display="grid" sx={{gap: 1}}><CheckboxGroup onChange={handleCheckboxGroupChange}><CheckboxGroup.Label>Choices</CheckboxGroup.Label><FormControl><Checkbox value="one" defaultChecked onChange={handleChoiceOneChange} /><FormControl.Label>Choice one</FormControl.Label></FormControl><FormControl><Checkbox value="two" defaultChecked /><FormControl.Label>Choice two</FormControl.Label></FormControl><FormControl><Checkbox value="three" /><FormControl.Label>Choice three</FormControl.Label></FormControl></CheckboxGroup>{Boolean(selectedCheckboxValues.length) && (<div>The selected checkbox values are {selectedCheckboxValues.join(', ')}</div>)}{Boolean(lastSelectedCheckboxValue) && <div>The last affected checkbox value is {lastSelectedCheckboxValue}</div>}</Box>)}render(<WithOnChangeHandlers />)
<CheckboxGroup disabled><CheckboxGroup.Label>Choices</CheckboxGroup.Label><FormControl><Checkbox value="one" /><FormControl.Label>Choice one</FormControl.Label></FormControl><FormControl><Checkbox value="two" /><FormControl.Label>Choice two</FormControl.Label></FormControl><FormControl><Checkbox value="three" /><FormControl.Label>Choice three</FormControl.Label></FormControl></CheckboxGroup>
<CheckboxGroup required><CheckboxGroup.Label>Choices</CheckboxGroup.Label><FormControl><Checkbox value="one" /><FormControl.Label>Choice one</FormControl.Label></FormControl><FormControl><Checkbox value="two" /><FormControl.Label>Choice two</FormControl.Label></FormControl><FormControl><Checkbox value="three" /><FormControl.Label>Choice three</FormControl.Label></FormControl></CheckboxGroup>
<CheckboxGroup><CheckboxGroup.Label>Choices</CheckboxGroup.Label><FormControl><Checkbox value="one" /><FormControl.Label>Choice one</FormControl.Label></FormControl><FormControl><Checkbox value="two" /><FormControl.Label>Choice two</FormControl.Label></FormControl><FormControl><Checkbox value="three" /><FormControl.Label>Choice three</FormControl.Label></FormControl><CheckboxGroup.Validation variant="error">Your choices are wrong</CheckboxGroup.Validation></CheckboxGroup>
<CheckboxGroup><CheckboxGroup.Label>Choices</CheckboxGroup.Label><CheckboxGroup.Caption>You can pick any or all of these choices</CheckboxGroup.Caption><FormControl><Checkbox value="one" /><FormControl.Label>Choice one</FormControl.Label></FormControl><FormControl><Checkbox value="two" /><FormControl.Label>Choice two</FormControl.Label></FormControl><FormControl><Checkbox value="three" /><FormControl.Label>Choice three</FormControl.Label></FormControl></CheckboxGroup>
<CheckboxGroup><CheckboxGroup.Label visuallyHidden>Choices</CheckboxGroup.Label><FormControl><Checkbox value="one" /><FormControl.Label>Choice one</FormControl.Label></FormControl><FormControl><Checkbox value="two" /><FormControl.Label>Choice two</FormControl.Label></FormControl><FormControl><Checkbox value="three" /><FormControl.Label>Choice three</FormControl.Label></FormControl></CheckboxGroup>
<><Boxid="choiceHeading"borderBottomWidth="1px"borderBottomStyle="solid"borderBottomColor="border.default"pb={2}mb={3}fontSize={3}>Choices</Box><CheckboxGroup aria-labelledby="choiceHeading"><FormControl><Checkbox value="one" /><FormControl.Label>Choice one</FormControl.Label></FormControl><FormControl><Checkbox value="two" /><FormControl.Label>Choice two</FormControl.Label></FormControl><FormControl><Checkbox value="three" /><FormControl.Label>Choice three</FormControl.Label></FormControl></CheckboxGroup></>
Name | Type | Default | Description |
---|---|---|---|
aria-labelledby | string | Used when associating the input group with a label other than CheckboxGroup.Label | |
children Required | CheckboxGroup.Label | CheckboxGroup.Caption | CheckboxGroup.Validation | FormControl | ||
disabled | boolean | false | Whether the input group allows user input |
id | string | a generated string | The unique identifier for this input group. Used to associate the label, validation text, and caption text. You may want a custom ID to make it easier to select elements in integration tests. |
onChange | (selected: string[], e?: ChangeEvent<HTMLInputElement>) => void | An onChange handler that gets called when the selection changes | |
required | boolean | false | If true, the user must make a selection before the owning form can be submitted |
sx | SystemStyleObject | Style overrides to apply to the component. See also overriding styles. |
A title for the set of choices. If a CheckboxGroup.Label
is not passed as a child, you must pass the external title's ID to the aria-describedby
prop on CheckboxGroup
Name | Type | Default | Description |
---|---|---|---|
visuallyHidden | boolean | false | If true, the fieldset legend will be visually hidden |
sx | SystemStyleObject | Style overrides to apply to the component. See also overriding styles. |
Name | Type | Default | Description |
---|---|---|---|
children | React.ReactNode | The caption content | |
sx | SystemStyleObject | Style overrides to apply to the component. See also overriding styles. |
If the user's selection has been flagged during validation, CheckboxGroup.Validation
may be used to render contextual validation information to help the user complete their task
Name | Type | Default | Description |
---|---|---|---|
children | React.ReactNode | The validation message | |
variant Required | 'error' | 'success' | 'warning' | Changes the visual style to match the validation status | |
sx | SystemStyleObject | Style overrides to apply to the component. See also overriding styles. |