Skip to content

Commit 5937f2c

Browse files
committed
fix(checkbox): use react hooks
1 parent 3ab7007 commit 5937f2c

File tree

2 files changed

+42
-57
lines changed

2 files changed

+42
-57
lines changed

src/Checkbox.tsx

Lines changed: 40 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import noop = require('lodash/noop');
12
import * as React from 'react';
23

34
import { Box } from './Box';
45
import { Flex } from './Flex';
56
import { Icon } from './Icon';
6-
import { Input } from './Input';
77
import { styled } from './utils';
88

99
export interface ICheckboxProps {
@@ -16,63 +16,48 @@ export interface ICheckboxProps {
1616
onChange?: (checked: boolean) => void;
1717
}
1818

19-
export interface ICheckboxState {
20-
checked?: boolean;
21-
}
22-
23-
export class BasicCheckbox extends React.Component<ICheckboxProps, ICheckboxState> {
24-
constructor(props: ICheckboxProps) {
25-
super(props);
26-
this.state = { checked: this.props.checked };
27-
}
19+
export const BasicCheckbox = (props: ICheckboxProps) => {
20+
const { id, className, width, height, disabled, onChange = noop } = props;
2821

29-
private onChange = (event: React.ChangeEvent<HTMLInputElement>) => {
30-
this.setState({ checked: event.target.checked });
22+
const [checked, setValue] = React.useState(props.checked || false);
23+
const isChecked = props.hasOwnProperty('checked') ? props.checked : checked;
3124

32-
if (this.props.onChange) {
33-
this.props.onChange(event.target.checked);
34-
}
25+
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
26+
setValue(event.target.checked);
27+
onChange(event.target.checked);
3528
};
3629

37-
public render() {
38-
const { checked: stateChecked } = this.state;
39-
const { id, className, width, height, disabled, checked: propsChecked } = this.props;
40-
41-
const checked = this.props.hasOwnProperty('checked') ? propsChecked : stateChecked;
42-
43-
return (
44-
<Box as="label" display="inline-block" id={id} className={className}>
45-
<Input
46-
type="checkbox"
47-
checked={checked || false}
48-
disabled={disabled}
49-
onChange={this.onChange}
50-
position="absolute"
51-
css={{ clip: 'rect(1px, 1px, 1px, 1px)' }}
52-
/>
53-
<Flex
54-
as="span"
55-
display="block"
56-
m="none"
57-
p="none"
58-
radius="md"
59-
items="center"
60-
justify="center"
61-
bg={checked ? 'toggle.checked.bg' : 'toggle.bg'}
62-
cursor={disabled ? 'not-allowed' : 'pointer'}
63-
width={width || '20px'}
64-
height={height || '20px'}
65-
opacity={disabled ? 0.6 : 1}
66-
css={{
67-
fontSize: '14px',
68-
transition: 'background-color .15s ease-in-out',
69-
}}
70-
>
71-
{checked && <Icon icon="check" fg="toggle.checked.fg" />}
72-
</Flex>
73-
</Box>
74-
);
75-
}
76-
}
30+
return (
31+
<Box as="label" display="inline-block" id={id} className={className}>
32+
<input
33+
type="checkbox"
34+
checked={isChecked || false}
35+
disabled={disabled}
36+
onChange={handleChange}
37+
style={{ position: 'absolute', clip: 'rect(1px, 1px, 1px, 1px)' }}
38+
/>
39+
<Flex
40+
as="span"
41+
display="block"
42+
m="none"
43+
p="none"
44+
radius="md"
45+
items="center"
46+
justify="center"
47+
bg={isChecked ? 'toggle.checked.bg' : 'toggle.bg'}
48+
cursor={disabled ? 'not-allowed' : 'pointer'}
49+
width={width || '20px'}
50+
height={height || '20px'}
51+
opacity={disabled ? 0.6 : 1}
52+
css={{
53+
fontSize: '14px',
54+
transition: 'background-color .15s ease-in-out',
55+
}}
56+
>
57+
{isChecked && <Icon icon="check" fg="toggle.checked.fg" />}
58+
</Flex>
59+
</Box>
60+
);
61+
};
7762

7863
export const Checkbox = styled<ICheckboxProps>(BasicCheckbox as any)``;

stories/Checkbox.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export const checkboxKnobs = (tabName = 'Checkbox') => {
2020
storiesOf('Checkbox', module)
2121
.addDecorator(withKnobs)
2222
.add('uncontrolled', () => <Checkbox id="1" {...checkboxKnobs()} />)
23-
.add('controlled checked', () => <Checkbox {...checkboxKnobs()} checked={true} />)
2423
.addDecorator(StateDecorator(store))
2524
.add('controlled', () => (
2625
<Checkbox
@@ -31,4 +30,5 @@ storiesOf('Checkbox', module)
3130
store.set({ checked });
3231
}}
3332
/>
34-
));
33+
))
34+
.add('controlled set', () => <Checkbox {...checkboxKnobs()} checked={true} />);

0 commit comments

Comments
 (0)