You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+35-28Lines changed: 35 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# ⚛ React Checkbox Context
2
2
3
-
This package was heavily inspired by [react-checkbox-group](https://github.com/ziad-saab/react-checkbox-group) after it stopped working the way I used it. `<Checkbox />` elements suddenly had to be direct children of `<CheckboxGroup>` (which was impossible for my use case) or the `CheckboxGroup` explicitly needed to have a `checkboxDepth` prop (which was not flexible enough for me). So I decided to write my own `<CheckboxGroup>` component based on React's new [Context API](https://reactjs.org/docs/context.html).
3
+
This package was heavily inspired by [react-checkbox-group](https://github.com/ziad-saab/react-checkbox-group) after it stopped working the way I used it. `<Checkbox />` elements suddenly had to be direct children of `<CheckboxGroup>` (which was impossible for my use case) or the `CheckboxGroup` explicitly needed to have a `checkboxDepth` prop (which was not flexible enough for me). So I decided to write my own `<CheckboxGroup>` component based on React's (then) new [Context API](https://reactjs.org/docs/context.html).
4
4
5
5
Big thank you to [Ziad Saab](https://github.com/ziad-saab) for the inspiration!
6
6
@@ -9,6 +9,7 @@ Big thank you to [Ziad Saab](https://github.com/ziad-saab) for the inspiration!
9
9
```
10
10
npm install react-checkbox-context
11
11
```
12
+
12
13
or
13
14
14
15
```
@@ -21,54 +22,60 @@ What does `react-checkbox-context` do and how does it do that? Let me borrow the
21
22
22
23
This is your average checkbox group:
23
24
24
-
```js
25
+
```jsx
25
26
<form>
26
-
<input onChange={this.handleFruitChange} type="checkbox" name="fruit" value="apple"/> Apple
Since this component uses React's Context API, `<Checkbox>` elements can by anywhere inside of a `<CheckboxGroup>` and **do not** have to be a direct descendant! So this is easily possible **without** having to specify any `checkboxDepth` props or the like:
**Attention:** When migrating from `react-checkbox-group` please note that the prop name to pass the values to a `CheckboxGroup` is named `values` instead of `value` since it's an array and as such sounds more logical to me to be plural.
68
+
**Attention:** When migrating from `react-checkbox-group` please note that the prop name to pass the values to a `CheckboxGroup` is named `values` instead of `value`.
|`onChange`|`(originalEvent: SyntheticEvent<HTMLInputElement>, values: Array<string>) => void`| Will be called on every time a checkbox changes its state. |
70
-
|`name`|`string`| Name for all checkboxes within one `<CheckboxGroup>`|
71
-
|`values`|`Array<string>`| Values of the `<Checkbox>` elements marked as `checked`|
|`onChange`|`(event: ChangeEvent, values: string[]) => void`| Will be called on every time a checkbox changes its state. |
77
+
|`name`|`string`| Name for all checkboxes within one `<CheckboxGroup>`|
78
+
|`values`|`string[]`| Values of the `<Checkbox>` elements marked as `checked`|
72
79
73
80
Status of checkboxes (checked/unchecked) can be controlled from outside by passing new values to `<CheckboxGroup>` (e.g. `<CheckboxGroup values={this.state.checkedItems} />`).
74
81
@@ -78,7 +85,7 @@ The `Checkbox` component passes all of its props the the underlying `<input type
78
85
79
86
## Todo
80
87
81
-
* Add more tests, especially with Enzyme to be able to check if `onChange` events are fired correctly.
88
+
- Add more tests, more specifically a test if `onChange` events are fired correctly.
0 commit comments