Switch

分类:

other

日期:

2022-2-06

标签:

chakra

The Switch component is used as an alternative for the Checkbox component. You can switch between enabled or disabled states.

Switch must always be accompanied by a label, and follows the same keyboard workflow as a Checkbox.

Import#

import { Switch } from '@chakra-ui/react';

Usage#

<FormControl display="flex" alignItems="center">
<FormLabel htmlFor="email-alerts" mb="0">
Enable email alerts?
</FormLabel>
<Switch id="email-alerts" />
</FormControl>

Sizes#

Pass the size prop to change the size of the Switch.

<Stack align="center" direction="row">
<Switch size="sm" />
<Switch size="md" />
<Switch size="lg" />
</Stack>

Switch background color#

You can change the checked background color of the Switch by passing the colorScheme prop.

<Stack direction="row">
<Switch colorScheme="red" />
<Switch colorScheme="teal" size="lg" />
</Stack>

State depending behavior#

States like isDisabled have an impact on the usability of a Switch and on the styles, except for the isInvalid and the isRequired prop.

<FormControl as={SimpleGrid} columns={{ base: 2, lg: 4 }}>
<FormLabel htmlFor="isChecked">isChecked:</FormLabel>
<Switch id="isChecked" isChecked />
<FormLabel htmlFor="isDisabled">isDisabled:</FormLabel>
<Switch id="isDisabled" isDisabled defaultChecked />
<FormLabel htmlFor="isFocusable">isFocusable:</FormLabel>
<Switch id="isFocusable" isFocusable isDisabled />
<FormLabel htmlFor="isInvalid">isInvalid:</FormLabel>
<Switch id="isInvalid" isInvalid />
<FormLabel htmlFor="isReadOnly">isReadOnly:</FormLabel>
<Switch id="isReadOnly" isReadOnly />
<FormLabel htmlFor="isRequired">isRequired:</FormLabel>
<Switch id="isRequired" isRequired />
</FormControl>

Props#

aria-describedby

string

aria-invalid

true

aria-label

Defines the string that labels the checkbox element.

string

aria-labelledby

Refers to the id of the element that labels the checkbox element.

string

colorScheme

"whiteAlpha" | "blackAlpha" | "gray" | "red" | "orange" | "yellow" | "green" | "teal" | "blue" | "cyan" | "purple" | "pink" | "linkedin" | "facebook" | "messenger" | "whatsapp" | "twitter" | "telegram"
"blue"

defaultChecked

If true, the checkbox will be initially checked.

boolean

id

id assigned to input

string

isChecked

If true, the checkbox will be checked. You'll need to pass onChange to update its value (since it is now controlled)

boolean

isDisabled

If true, the checkbox will be disabled

boolean

isFocusable

If true and isDisabled is passed, the checkbox will remain tabbable but not interactive

boolean

isInvalid

If true, the checkbox is marked as invalid. Changes style of unchecked state.

boolean

isReadOnly

If true, the checkbox will be readonly

boolean

isRequired

If true, the checkbox input is marked as required, and required attribute will be added

boolean

name

The name of the input field in a checkbox (Useful for form submission).

string

onBlur

The callback invoked when the checkbox is blurred (loses focus)

((event: FocusEvent<HTMLInputElement, Element>) => void)

onChange

The callback invoked when the checked state of the Checkbox changes.

((event: ChangeEvent<HTMLInputElement>) => void)

onFocus

The callback invoked when the checkbox is focused

((event: FocusEvent<HTMLInputElement, Element>) => void)

size

"sm" | "md" | "lg"
"md"

spacing

The spacing between the switch and its label text

SystemProps["marginLeft"]
0.5rem

tabIndex

number

value

The value to be used in the checkbox input. This is the value that will be returned on form submission.

string | number

variant

Variants Switch

string
footer logo

© 2022 Designed with chakra ui. Powered by contentlayerjs and nextjs etc. All rights reserved

TwitterYouTubeGithub