Switch: React
Switches toggle the state of a single setting on or off.
Installation
Before you can start using the @mediahuis/chameleon-react
component, you’ll need to install it, for more information check: Getting started for developers.
With that done, you’re now ready to implement the component in your application.
import { Switch } from '@mediahuis/chameleon-react';
export default function Example() {
return <Switch label="Example" id="switch-1" />;
}
Properties
Property | Type | Default | Required | Description |
---|---|---|---|---|
action | ReactNode | - | No | Contents displayed within the ActionLabel. |
checked | Boolean | - | No | Sets the native checkbox checked state. |
className | String | - | No | Allows extending the class names of the component. |
disabled | Boolean | - | No | Sets the native checkbox disabled state. |
error | Boolean | - | No | Visually displayed as error. Adds necessary accessibility tags. |
id | String | - | Yes | Native HTML id attribute. Also affects label-id and message-id for accessibility reasons. |
label | ReactNode | - | No | Contents displayed within the PrimaryLabel. |
labelHidden | Boolean | - | No | Visually hide the label. (label is still required for accessibility) |
labelPlacement | 'left' | 'right' | - | No | The placement of the label. |
labelProps | GenericComponentPropsWithoutAs | - | No | Props passed to the label component. |
labelId | String | `${id}-label` | No | The unique HTML id attribute. It is used for associating the label and the radio components for accessibility. |
message | ReactNode | - | No | Contents displayed beneath the label. |
onChange | ChangeEventHandler | - | No | Native change event, validates on event.target.checked. |
optionalLabel | String | - | No | Contents displayed within the OptionalLabel. |
required | Boolean | - | No | Sets native input required attribute. |
value | String | - | No | Native input value. |
inputProps | React.HTMLProps | - | No | Object containing additional props passed to the input component. |
required
Chameleon form inputs are required by default, with the required
attribute automatically set. To mark a field as optional, pass the optionalLabel
prop to remove the required
attribute.