import {SymbolDoc, Specifications} from '@workday/canvas-kit-docs';

import {Switch} from '@workday/canvas-kit-react/switch';

import Alert from './examples/Alert';
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';


# Canvas Kit Switch

Switch is a selection control that is used to switch between two potential states.

[> Workday Design Reference](https://design.workday.com/components/inputs/switch)

## Installation

```sh
yarn add @workday/canvas-kit-react
```

## Usage

### Basic Example

Switch should be used in tandem with [Form Field](/components/inputs/form-field/) to meet
accessibility standards.

<ExampleCodeBlock code={Basic} />

### Disabled

Set the `disabled` prop of the Switch to prevent users from interacting with it.

<ExampleCodeBlock code={Disabled} />

### Ref Forwarding

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} />

### Label Position

Set the `labelPosition` prop of the wrapping Form Field to designate the position of the label
relative to the Switch. `labelPosition` accepts the following values:

- `FormField.LabelPosition.Top` (Default)
- `FormField.LabelPosition.Left`

<ExampleCodeBlock code={LabelPosition} />

### Error States

Set the `error` prop of the wrapping Form Field to `FormField.ErrorType.Alert` or
`FormField.ErrorType.Error` to set the Switch to the Alert 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.Alert` or
`Switch.ErrorType.Error` if Form Field is not being used.

#### Alert

<ExampleCodeBlock code={Alert} />

#### Error

<ExampleCodeBlock code={Error} />

## Component API

<SymbolDoc name="Switch" fileName="/react/" />

## Specifications

<Specifications file="Switch.spec.ts" name="Switch" />