Dojo's Checkbox widget provides either a wrapped, styleable checkbox widget or an on/off toggle. Both the normal and toggle Checkbox modes use the native <input type="checkbox"> as a base.
- Creates either a normal checkbox or toggle switch
- Correctly handles a11y attributes
- Wraps the input in a visible or invisible but accessible
<label>if thelabelproperty is added
Checkbox ensures that the proper attributes (ARIA or otherwise) are set along with classes when properties such as disabled, readOnly, invalid, etc. are used.
When using the toggle mode, only one of onLabel and offLabel will be read, based on the current state. E.g. if checked with a label of "Sample Checkbox", the full screen reader-accessible label would read "Sample Checkbox On".
If the label property is not used, we recommend creating a separate label and pointing it at the input's widgetId property.
// Normal usage
w(Checkbox, {
label: 'Sign up for updates',
name: 'update-checkbox'
checked: this.state.checked,
value: 'updates',
onChange: (event: TypedTargetEvent<HTMLInputElement>) => {
this.setState({ checked: event.target.checked });
},
});
// Toggle mode with description
w(Checkbox, {
checked: this.state.musicChecked,
describedBy: 'instructions',
label: 'Play music',
mode: Mode.toggle,
name: 'music',
onLabel: 'On',
offLabel: 'Off',
onChange: (event: TypedTargetEvent<HTMLInputElement>) => {
this.setState({ musicChecked: event.target.checked });
}
}),
v('p', {
id: 'instructions'
}, [ 'Turns background music on or off' ]);The following CSS classes are available on the Checkbox widget for use with custom themes:
root: Applied to either the wrapping<label>, or a<div>in the same position in the node hierarchychecked: Applied to the same level asrootifproperties.checkedis truedisabled: Applied to the same level asrootifproperties.disabledis truereadonly: Applied to the same level asrootifproperties.readOnlyis truerequired: Applied to the same level asrootifproperties.requiredis trueinvalid: Applied to the same level asrootifproperties.invalidis truevalid: Applied to the same level asrootifproperties.invalidis set false (i.e. not only undefined)toggle: Applied to the same level asrootifproperties.modeisMode.toggleinputWrapper: Applied to the immediate parent of the<input>input: Applied to the<input>elementonLabel: Applied to the node used for the "on" text in toggle modeoffLabel: Applied to the node used for the "off" text in toggle mode