InputSwitch

InputSwitch is used to select a boolean value.


import { InputSwitch } from 'primereact/inputswitch';
         

InputSwitch is used as a controlled input with checked and onChange properties.


<InputSwitch checked={checked} onChange={(e) => setChecked(e.value)} />
         

Enabling checked property displays the component as active initially.


<InputSwitch checked={checked} onChange={(e) => setChecked(e.value)} />
         

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


<InputSwitch className="p-invalid" />
         

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


<InputSwitch disabled />
         

Screen Reader

InputSwitch component uses a hidden native checkbox element with switch role internally that is only visible to screen readers. Value to describe the component can either be provided via label tag combined with inputId prop or using aria-labelledby, aria-label props.


<label htmlFor="switch1">Remember Me</label>
<InputSwitch inputId="switch1" />

<span id="switch2">Remember Me</span>
<InputSwitch aria-labelledby="switch2" />

<InputSwitch aria-label="Remember Me" />
     

Keyboard Support

KeyFunction
tabMoves focus to the switch.
spaceToggles the checked state.