|
| 1 | +import {Layout} from '@react-spectrum/docs'; |
| 2 | +export default Layout; |
| 3 | + |
| 4 | +import docs from 'docs:@react-spectrum/checkbox'; |
| 5 | +import packageData from '../package.json'; |
| 6 | +import {HeaderInfo, PropTable} from '@react-spectrum/docs'; |
| 7 | + |
| 8 | +```jsx import |
| 9 | + import {Checkbox} from '@react-spectrum/checkbox'; |
| 10 | + ``` |
| 11 | + |
| 12 | +# Checkbox |
| 13 | + |
| 14 | +<p>{docs.exports.Checkbox.description}</p> |
| 15 | + |
| 16 | +<HeaderInfo |
| 17 | + packageData={packageData} |
| 18 | + componentNames={['Checkbox']} |
| 19 | + sourceData={[ |
| 20 | + {type: 'Spectrum', url: 'https://spectrum.adobe.com/page/checkbox/'} |
| 21 | + ]} /> |
| 22 | + |
| 23 | +## Example |
| 24 | + |
| 25 | +```tsx example |
| 26 | +<Checkbox>Checkbox Label</Checkbox> |
| 27 | +``` |
| 28 | + |
| 29 | +## Content |
| 30 | + |
| 31 | +Checkboxes accept children, which are rendered as the label. |
| 32 | + |
| 33 | +### Accessibility |
| 34 | + |
| 35 | +In certain cases a visible label isn't needed. For example, a checkbox used to select a table row. |
| 36 | +If a visible label isn't specified, an `aria-label` must be provided to the Checkbox for accessibility. |
| 37 | +If the field is labeled by a separate element, an `aria-labelledby` prop must be provided using the id of the labeling element instead. |
| 38 | + |
| 39 | +### Internationalization |
| 40 | + |
| 41 | +To internationalize a Checkbox, a localized label should be passed to the `children` or `aria-label` prop. |
| 42 | + |
| 43 | +## Value |
| 44 | + |
| 45 | +Checkboxes are not selected by default. The `defaultSelected` prop can be used to set the default state (uncontrolled). |
| 46 | +Alternatively, the `isSelected` prop can be used to make the selected state controlled. |
| 47 | + |
| 48 | +```tsx example |
| 49 | +<Checkbox defaultSelected>Uncontrolled</Checkbox> |
| 50 | +<Checkbox isSelected>Controlled</Checkbox> |
| 51 | +``` |
| 52 | + |
| 53 | +### Indeterminate |
| 54 | +[View guidelines](https://spectrum.adobe.com/page/checkbox/#Mixed-value) |
| 55 | + |
| 56 | +A Checkbox can be in an indeterminate state, controlled using the `isIndeterminate` prop. |
| 57 | +This overrides the selection state, whether controlled or uncontrolled. |
| 58 | +The Checkbox will remain in an indeterminate state until the `isIndeterminate` prop is set to false, regardless of user interaction. |
| 59 | + |
| 60 | +```tsx example |
| 61 | +<Checkbox isIndeterminate>Checkbox Label</Checkbox> |
| 62 | +``` |
| 63 | + |
| 64 | +## Events |
| 65 | + |
| 66 | +Checkboxes accept a user defined `onChange` prop, triggered whenever the Checkbox is clicked. |
| 67 | +The example below uses `onChange` to alert the user to changes in the checkbox's state. |
| 68 | + |
| 69 | +```tsx example |
| 70 | +function Example() { |
| 71 | + let [selected, setSelection] = React.useState('false'); |
| 72 | + |
| 73 | + let toggle = (value) => { |
| 74 | + value ? setSelection('true') : setSelection('false'); |
| 75 | + } |
| 76 | + |
| 77 | + return ( |
| 78 | + <div> |
| 79 | + <div> The Checkbox is checked: {selected} </div> |
| 80 | + <Checkbox onChange={toggle}> |
| 81 | + Checkbox Label |
| 82 | + </Checkbox> |
| 83 | + </div> |
| 84 | + ); |
| 85 | + } |
| 86 | +``` |
| 87 | + |
| 88 | +## Validation |
| 89 | + |
| 90 | +Checkboxes can display a validation state to communicate to the user if the current value is invalid. |
| 91 | +Implement your own validation logic in your app and pass `"invalid"` to the Checkbox via the `validationState` prop. |
| 92 | + |
| 93 | +```tsx example |
| 94 | +<Checkbox validationState="invalid">Checkbox Label</Checkbox> |
| 95 | +``` |
| 96 | + |
| 97 | +## Props |
| 98 | + |
| 99 | +<PropTable component={docs.exports.Checkbox} links={docs.links} /> |
| 100 | + |
| 101 | +## Visual Options |
| 102 | + |
| 103 | +### isDisabled |
| 104 | +[View guidelines](https://spectrum.adobe.com/page/checkbox/#Disabled) |
| 105 | + |
| 106 | +```tsx example |
| 107 | +<Checkbox isDisabled>Checkbox Label</Checkbox> |
| 108 | +``` |
| 109 | + |
| 110 | +### isEmphasized |
| 111 | +[View guidelines](https://spectrum.adobe.com/page/checkbox/#Emphasized-or-not?) |
| 112 | + |
| 113 | +```tsx example |
| 114 | +<Checkbox isEmphasized>Checkbox Label</Checkbox> |
| 115 | +``` |
0 commit comments