Skip to main content

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

PropertyTypeDefaultRequiredDescription
actionReactNode-NoContents displayed within the ActionLabel.
checkedBoolean-NoSets the native checkbox checked state.
classNameString-NoAllows extending the class names of the component.
disabledBoolean-NoSets the native checkbox disabled state.
errorBoolean-NoVisually displayed as error. Adds necessary accessibility tags.
idString-YesNative HTML id attribute. Also affects label-id and message-id for accessibility reasons.
labelReactNode-NoContents displayed within the PrimaryLabel.
labelHiddenBoolean-NoVisually hide the label. (label is still required for accessibility)
labelPlacement'left' | 'right'-NoThe placement of the label.
labelPropsGenericComponentPropsWithoutAs-NoProps passed to the label component.
labelIdString`${id}-label`NoThe unique HTML id attribute. It is used for associating the label and the radio components for accessibility.
messageReactNode-NoContents displayed beneath the label.
onChangeChangeEventHandler-NoNative change event, validates on event.target.checked.
optionalLabelString-NoContents displayed within the OptionalLabel.
requiredBoolean-NoSets native input required attribute.
valueString-NoNative input value.
inputPropsReact.HTMLProps-NoObject 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.