Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f093ed0

Browse files
putting everything in state
1 parent d770086 commit f093ed0

File tree

5 files changed

+23
-14
lines changed

5 files changed

+23
-14
lines changed

‎.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lib/*

‎.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
package.json
2+
lib/*

‎.prettierrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
printWidth: 100
22
singleQuote: true
33
tabWidth: 4
4-
trailingComma: all
4+
trailingComma: es5
55
semi: true
66
arrowParens: always

‎lib/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/CheckboxGroup.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,42 @@ type CheckboxGroupStateT = {
1818

1919
export default class CheckboxGroup extends React.Component<
2020
CheckboxGroupPropsT,
21-
CheckboxGroupStateT,
21+
CheckboxGroupStateT
2222
> {
2323
static getDerivedStateFromProps(nextProps: CheckboxGroupPropsT) {
2424
if (!nextProps.values) {
25-
return [];
25+
return {
26+
values: [],
27+
name: nextProps.name,
28+
};
2629
}
2730

2831
if (Array.isArray(nextProps.values)) {
2932
return {
3033
values: nextProps.values.map((value) => value.toString()),
34+
name: nextProps.name,
3135
};
3236
}
3337

3438
if (typeof nextProps.values === 'string') {
3539
return {
3640
values: [nextProps.values],
41+
name: nextProps.name,
3742
};
3843
}
3944
}
4045

46+
constructor(props, state) {
47+
super(props, state);
48+
this.state = {
49+
...this.state,
50+
onChange: this.onChange,
51+
};
52+
}
53+
4154
state = {
4255
values: [],
56+
name: this.props.name,
4357
};
4458

4559
removeValue = (value: string, originalEvent: SyntheticEvent<HTMLInputElement>) => {
@@ -59,7 +73,7 @@ export default class CheckboxGroup extends React.Component<
5973
if (typeof this.props.onChange === 'function') {
6074
this.props.onChange(this.state.values, originalEvent);
6175
}
62-
},
76+
}
6377
);
6478
};
6579

@@ -77,14 +91,13 @@ export default class CheckboxGroup extends React.Component<
7791
if (typeof this.props.onChange === 'function') {
7892
this.props.onChange(this.state.values, originalEvent);
7993
}
80-
},
94+
}
8195
);
8296
};
8397

8498
onChange = (e: SyntheticEvent<HTMLInputElement>) => {
8599
e.persist();
86100
const { currentTarget: { value, checked } } = e;
87-
88101
if (checked) {
89102
this.addValue(value, e);
90103
} else {
@@ -94,13 +107,7 @@ export default class CheckboxGroup extends React.Component<
94107

95108
render() {
96109
return (
97-
<CheckboxContext.Provider
98-
value={{
99-
name: this.props.name,
100-
values: this.state.values,
101-
onChange: this.onChange,
102-
}}
103-
>
110+
<CheckboxContext.Provider value={this.state}>
104111
{this.props.children}
105112
</CheckboxContext.Provider>
106113
);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /