@workday/canvas-kit-docs
Version:
Documentation components of Canvas Kit components
84 lines (53 loc) • 2.31 kB
text/mdx
import {
ExampleCodeBlock,
SymbolDoc,
Specifications,
InformationHighlight,
} from '@workday/canvas-kit-docs';
import Caution from './examples/Caution';
import Basic from './examples/Basic';
import Disabled from './examples/Disabled';
import Error from './examples/Error';
import LabelPosition from './examples/LabelPosition';
import RefForwarding from './examples/RefForwarding';
Switch is a selection control that is used to switch between two potential states.
[> Workday Design Reference](https://design.workday.com/components/inputs/switch)
```sh
yarn add @workday/canvas-kit-react
```
Switch should be used in tandem with [Form Field](/components/inputs/form-field/) to meet
accessibility standards.
<ExampleCodeBlock code={Basic} />
Set the `disabled` prop of the Switch to prevent users from interacting with it.
<ExampleCodeBlock code={Disabled} />
Switch supports [ref forwarding](https://reactjs.org/docs/forwarding-refs.html). It will forward
`ref` to its underlying `<input type="checkbox">` element.
<ExampleCodeBlock code={RefForwarding} />
Set the `orientation` prop of the Form Field to designate the position of the label relative to the
input component. By default, the orientation will be set to `vertical`.
<ExampleCodeBlock code={LabelPosition} />
Set the `error` prop of the wrapping Form Field to `"caution"` or
`"error"` to set the Switch to the Caution or Error state, respectively. You will
also need to set the `hintId` and `hintText` props on the Form Field to meet accessibility
standards.
The `error` prop may be applied directly to the Switch with a value of `Switch.ErrorType.Caution` or
`Switch.ErrorType.Error` if Form Field is not being used.
<ExampleCodeBlock code={Caution} />
<ExampleCodeBlock code={Error} />
Switch supports custom styling via the `cs` prop. For more information, check our
["How To Customize Styles"](https://workday.github.io/canvas-kit/?path=/docs/styling-guides-customizing-styles--docs).
<SymbolDoc name="Switch" fileName="/react/" />
<Specifications file="Switch.spec.ts" name="Switch" />