MultiStateCheckbox

MultiStateCheckbox is used to select a state from given options.


import { MultiStateCheckbox } from 'primereact/multistatecheckbox';
         

MultiStateCheckbox is used as a controlled input with value and onChange properties along with the option collection. The optionValue field refers to the value of each option, if ommitted the object itself becomes the value of an option. The icon to display is retrieved from the optionIcon property that defaults to icon property name.

public

<MultiStateCheckbox value={value} onChange={(e) => setValue(e.value)} options={options} optionValue="value" />
         

Invalid state style is added using the p-invalid class to indicate a failed validation.


<MultiStateCheckbox className="p-invalid" />
         

When disabled is present, the element cannot be edited and focused.


<MultiStateCheckbox disabled />
         

Compatibility with popular React form libraries.

Formik is a popular library for handling forms in React.

 

<label htmlFor="item" className={classNames('mr-2', { 'p-error': formik.errors.item })}>Level</label>
<MultiStateCheckbox id="item" name="item" value={formik.values.item} onChange={(e) => { formik.setFieldValue('item', e.value) }} options={options} optionValue="value" className={classNames({ 'p-invalid': formik.errors.item })} />
{getFormErrorMessage('item')}
<Button label="Submit" type="submit" icon="pi pi-check" />
         

React Hook Form is another popular React library to handle forms.

 

<Controller name="value"  control={form.control} rules={{ required: 'Level is required.'}}
    render={({ field, fieldState }) => (
        <>
            <label htmlFor={field.name} className={classNames({ 'p-error': errors.name })}>Level*</label>
            <MultiStateCheckbox id={field.name} value={field.value} onChange={field.onChange} ref={field.ref} options={options} optionValue="value" className={classNames({ 'p-invalid': fieldState.error })} />
            {getFormErrorMessage(field.name)}
        </>
    )}
/>
         

Screen Reader

MultiStateCheckbox component uses an element with checkbox role. Value to describe the component can either be provided with aria-labelledby or aria-label props. Component adds an element witharia-live attribute that is only visible to screen readers to read the value displayed. Values to read are defined with the optionLabel property that defaults to optionValue if not defined. Unchecked state label on the other hand is retrieved from nullLabel key of the aria property from the locale API. This is an example of a custom accessibility implementation as there is no one to one mapping between the component design and the WCAG specification.


<span id="chkbox1">Access Type</span>
<MultiStateCheckbox aria-labelledby="chkbox1" />

<TriStateCheckbox aria-label="Access Type" />
     

Keyboard Support

KeyFunction
tabMoves focus to the checkbox.
spaceToggles between the values.