Skip to content

Commit 05236fb

Browse files
atomicpagesgitbook-bot
authored andcommitted
GitBook: [master] one page modified
1 parent 9d5d6d9 commit 05236fb

File tree

1 file changed

+60
-7
lines changed

1 file changed

+60
-7
lines changed

components/checkbox.md

+60-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
PCR's Checkbox is a simple and small accessible checkbox implementation that's flexible.
44

5-
### Usage
5+
## Usage
66

77
Checkbox receives props as [controlled inputs](https://reactjs.org/docs/forms.html), such as `checked` and `onChange`. Basic controlled usage can be seen below:
88

@@ -27,7 +27,7 @@ function App() {
2727
}
2828
```
2929

30-
#### `useCheckboxState`
30+
### `useCheckboxState`
3131

3232
To make things easier, there's also the `useCheckboxState` hook which takes care of connecting the checkbox and state together.
3333

@@ -50,7 +50,7 @@ function App() {
5050
}
5151
```
5252

53-
### Using icons
53+
### Using `icon`
5454

5555
Using a custom icon instead of the pretty checkbox default is easy. Use the `icon` prop and pass in your own JSX. Supported types are:
5656

@@ -73,39 +73,80 @@ function App() {
7373
}
7474
```
7575

76-
### Specifying shape
76+
### Specifying `shape`
7777

7878
Use the `shape` prop to control the shape of the checkbox. Supported values are:
7979

80-
* `square`
80+
* `square` \(default\)
8181
* `round`
8282
* `curve`
8383

84+
```jsx
85+
import React from 'react'
86+
import { Checkbox, useCheckboxState } from 'pretty-checkbox-react';
87+
88+
function App() {
89+
const checkbox = useCheckboxState();
90+
91+
return (
92+
<Checkbox {...checkbox} shape="curve">Curved Checkbox</Checkbox>
93+
);
94+
}
95+
```
96+
97+
### Using `fill`
98+
99+
Adjust the fill mode of Checkbox via the `fill` prop. Supported values are:
100+
101+
* `fill`
102+
* `thick`
103+
104+
```jsx
105+
import React from 'react'
106+
import { Checkbox, useCheckboxState } from 'pretty-checkbox-react';
107+
108+
function App() {
109+
const checkbox = useCheckboxState();
110+
111+
return (
112+
<Checkbox {...checkbox} fill="thick">Curved Checkbox</Checkbox>
113+
);
114+
}
115+
```
116+
84117
### `indeterminate` state
85118

86-
You can programmatically set checkbox value as `indeterminate`:
119+
You can programmatically set checkbox value as `indeterminate`. This can best be illustrated by an example on the [reakit docs](https://reakit.io/docs/checkbox/#indeterminate-state).
87120

88121
```jsx
89122
import React from 'react'
90123
import classNames from 'classnames';
91124
import { Checkbox, useCheckboxState } from 'pretty-checkbox-react';
92125

93126
function useTreeState({ values }) {
127+
// create a state for the root checkbox
94128
const group = useCheckboxState();
129+
130+
// create a state for each leaf checkbox
95131
const items = useCheckboxState();
96132

97133
React.useEffect(() => {
98134
if (group.state === true) {
135+
// if the group "root" checkbox
136+
// is true, set state where all
137+
// items are selected
99138
items.setState(values);
100139
} else if (group.state === false) {
140+
// when the group "root" checkbox is not selected
141+
// reset all the selected items
101142
items.setState([]);
102143
}
103144
}, [group.state]);
104145

105146
React.useEffect(() => {
106147
if (items.state.length === values.length) {
107148
group.setState(true);
108-
} else if ((items.state as string[]).length) {
149+
} else if (items.state.length) {
109150
group.setState('indeterminate');
110151
} else {
111152
group.setState(false);
@@ -152,5 +193,17 @@ function App() {
152193
}
153194
```
154195

196+
## Props API
197+
198+
### `useCheckboxState`
199+
200+
* `state` - `boolean | any[] | "indeterminate"` - Stores the state of the checkbox. Use an array of multiple checkboxes need to to share the same state value. See the [indeterminate state example](checkbox.md#indeterminate-state) for more info.
201+
202+
### `Checkbox`
155203

204+
| Name | Type | Required | Initial Value | Description |
205+
| :--- | :--- | :--- | :--- | :--- |
206+
| `state` | `boolean | any[] | "indeterminate"` | Yes | \`\` | |
207+
| `onChange` | `Function` | Yes | | Change event to call when the checkbox is clicked or a keyboard event is triggered |
208+
| | | | | |
156209

0 commit comments

Comments
 (0)