Skip to content

Commit 1eb9c02

Browse files
Checkbox Documentation (#180)
* js docs rough draft * saving progress * tested against website branch * adhere to wave 1 of review comments * adhere to wave 2 of review comments * more updates * a few more updates * improvements * forgot yarn install * revert
1 parent 1df4b90 commit 1eb9c02

File tree

3 files changed

+127
-2
lines changed

3 files changed

+127
-2
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
```

packages/@react-spectrum/checkbox/src/Checkbox.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ function Checkbox(props: SpectrumCheckboxProps, ref: FocusableRef<HTMLLabelEleme
6969
</label>
7070
);
7171
}
72-
72+
/**
73+
* Checkboxes allow users to select multiple items from a list of individual items,
74+
* or to mark one individual item as selected.
75+
*/
7376
let _Checkbox = forwardRef(Checkbox);
7477
export {_Checkbox as Checkbox};

packages/@react-types/checkbox/src/index.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {ReactNode} from 'react';
33

44
export interface CheckboxBase extends InputBase {
55
children?: ReactNode, // pass in children to render label
6-
76
defaultSelected?: boolean,
87
isSelected?: boolean,
98
onChange?: (isSelected: boolean) => void,
@@ -12,9 +11,17 @@ export interface CheckboxBase extends InputBase {
1211
}
1312

1413
export interface CheckboxProps extends CheckboxBase {
14+
/**
15+
* Indeterminism is presentational, when a checkbox is indeterminate, it overrides the selection state.
16+
* The indeterminate visual representation remains even after subsequent clicks .
17+
*/
1518
isIndeterminate?: boolean
1619
}
1720

1821
export interface SpectrumCheckboxProps extends CheckboxProps, DOMProps, StyleProps {
22+
/**
23+
* By default, checkboxes are not emphasized (gray).
24+
* This prop sets the emphasized style (blue) which provides visual prominence.
25+
*/
1926
isEmphasized?: boolean
2027
}

0 commit comments

Comments
 (0)