Skip to content

Commit 0105eed

Browse files
committed
fix(toggle): use react hooks
1 parent 8b2b09f commit 0105eed

File tree

4 files changed

+50
-67
lines changed

4 files changed

+50
-67
lines changed

src/Toggle.tsx

Lines changed: 45 additions & 60 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 IToggleProps {
@@ -16,70 +16,55 @@ export interface IToggleProps {
1616
onChange?: (checked: boolean) => void;
1717
}
1818

19-
export interface IToggleState {
20-
checked?: boolean;
21-
}
19+
export const BasicToggle = (props: IToggleProps) => {
20+
const { id, className, width, height, disabled, onChange = noop } = props;
2221

23-
export class BasicToggle extends React.Component<IToggleProps, IToggleState> {
24-
constructor(props: IToggleProps) {
25-
super(props);
26-
this.state = { checked: this.props.checked };
27-
}
22+
const [checked, setValue] = React.useState(props.checked || false);
23+
const isChecked = props.hasOwnProperty('checked') ? props.checked : checked;
2824

29-
private onChange = (event: React.ChangeEvent<HTMLInputElement>) => {
30-
this.setState({ checked: event.target.checked });
31-
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-
40-
const { id, className, width, height, disabled, checked: propsChecked } = this.props;
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-
id={id}
48-
checked={checked || false}
49-
disabled={disabled}
50-
onChange={this.onChange}
51-
position="absolute"
52-
css={{ clip: 'rect(1px, 1px, 1px, 1px)' }}
53-
/>
54-
<Flex
55-
as="span"
56-
display="block"
57-
m="none"
58-
p="none"
59-
radius="100px"
60-
bg={checked ? 'toggle.checked.bg' : 'toggle.bg'}
61-
cursor={disabled ? 'not-allowed' : 'pointer'}
62-
width={width || '40px'}
63-
height={height || '20px'}
64-
opacity={disabled ? 0.6 : 1}
65-
items="center"
30+
return (
31+
<Box as="label" display="inline-block" id={id} className={className}>
32+
<input
33+
type="checkbox"
34+
id={id}
35+
checked={isChecked || false}
36+
disabled={disabled}
37+
onChange={handleChange}
38+
style={{ position: 'absolute', clip: 'rect(1px, 1px, 1px, 1px)' }}
39+
/>
40+
<Flex
41+
as="span"
42+
display="block"
43+
m="none"
44+
p="none"
45+
radius="100px"
46+
bg={isChecked ? 'toggle.checked.bg' : 'toggle.bg'}
47+
cursor={disabled ? 'not-allowed' : 'pointer'}
48+
width={width || '40px'}
49+
height={height || '20px'}
50+
opacity={disabled ? 0.6 : 1}
51+
items="center"
52+
css={{
53+
fontSize: '14px',
54+
transition: 'background-color .15s ease-in-out',
55+
}}
56+
>
57+
<Icon
58+
icon="circle"
59+
fg={isChecked ? 'toggle.checked.fg' : 'toggle.fg'}
6660
css={{
67-
fontSize: '14px',
68-
transition: 'background-color .15s ease-in-out',
61+
paddingLeft: isChecked ? '22px' : '4px',
62+
transition: 'padding .15s ease-in-out, color .25s ease-in-out',
6963
}}
70-
>
71-
<Icon
72-
icon="circle"
73-
fg={checked ? 'toggle.checked.fg' : 'toggle.fg'}
74-
css={{
75-
paddingLeft: checked ? '22px' : '4px',
76-
transition: 'padding .15s ease-in-out, color .25s ease-in-out',
77-
}}
78-
/>
79-
</Flex>
80-
</Box>
81-
);
82-
}
83-
}
64+
/>
65+
</Flex>
66+
</Box>
67+
);
68+
};
8469

8570
export const Toggle = styled<IToggleProps>(BasicToggle as any)``;

stories/Checkbox.tsx

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

stories/Input.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ storiesOf('Input', module)
4242
.addDecorator(withKnobs)
4343
.add('uncontrolled', () => <Input {...inputKnobs()} {...textKnobs()} {...boxKnobs()} />)
4444
.add('autosize', () => <Input {...autosizeInputKnobs()} {...textKnobs()} {...boxKnobs()} autosize />)
45+
.add('controlled set', () => <Input {...inputKnobs()} {...textKnobs()} {...boxKnobs()} value="not editable" />)
4546
.addDecorator(StateDecorator(store))
4647
.add('controlled store', () => (
4748
<Input
@@ -51,5 +52,4 @@ storiesOf('Input', module)
5152
value={store.get('value')}
5253
onChange={(value: any) => store.set({ value })}
5354
/>
54-
))
55-
.add('controlled set', () => <Input {...inputKnobs()} {...textKnobs()} {...boxKnobs()} value="someText" />);
55+
));

stories/Textarea.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ storiesOf('Textarea', module)
5757
.addDecorator(withKnobs)
5858
.add('uncontrolled', () => <Textarea {...textareaKnobs()} {...textKnobs()} {...boxKnobs()} />)
5959
.add('autosize', () => <Textarea {...textareaAutosizeKnobs()} {...textKnobs()} {...boxKnobs()} autosize />)
60+
.add('controlled set', () => <Textarea {...textareaKnobs()} {...textKnobs()} {...boxKnobs()} value="not-editable" />)
6061
.addDecorator(StateDecorator(store))
6162
.add('controlled', () => (
6263
<Textarea
@@ -66,7 +67,4 @@ storiesOf('Textarea', module)
6667
value={store.get('value')}
6768
onChange={(value: any) => store.set({ value })}
6869
/>
69-
))
70-
.add('controlled set', () => (
71-
<Textarea {...textareaKnobs()} {...textKnobs()} {...boxKnobs()} value="Should not be update-able" />
7270
));

0 commit comments

Comments
 (0)