forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 3
/
CheckboxWithLabel.tsx
42 lines (36 loc) 路 985 Bytes
/
CheckboxWithLabel.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
import * as React from 'react';
interface CheckboxWithLabelProps {
labelRef: React.LegacyRef<HTMLLabelElement>;
inputRef: React.LegacyRef<HTMLInputElement>;
labelOff: string;
labelOn: string;
}
interface CheckboxWithLabelState {
isChecked: boolean;
}
class CheckboxWithLabel extends React.Component<
CheckboxWithLabelProps,
CheckboxWithLabelState
> {
constructor(props: CheckboxWithLabelProps) {
super(props);
this.state = {isChecked: false};
}
render() {
return (
<label ref={this.props.labelRef}>
<input
ref={this.props.inputRef}
type="checkbox"
checked={this.state.isChecked}
onChange={() =>
this.setState(current => ({isChecked: !current.isChecked}))
}
/>
{this.state.isChecked ? this.props.labelOn : this.props.labelOff}
</label>
);
}
}
export default CheckboxWithLabel;