1
+ import noop = require( 'lodash/noop' ) ;
1
2
import * as React from 'react' ;
2
3
3
4
import { Box } from './Box' ;
4
5
import { Flex } from './Flex' ;
5
6
import { Icon } from './Icon' ;
6
- import { Input } from './Input' ;
7
7
import { styled } from './utils' ;
8
8
9
9
export interface IToggleProps {
@@ -16,70 +16,55 @@ export interface IToggleProps {
16
16
onChange ?: ( checked : boolean ) => void ;
17
17
}
18
18
19
- export interface IToggleState {
20
- checked ?: boolean ;
21
- }
19
+ export const BasicToggle = ( props : IToggleProps ) => {
20
+ const { id, className, width, height, disabled, onChange = noop } = props ;
22
21
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 ;
28
24
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 ) ;
35
28
} ;
36
29
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' }
66
60
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' ,
69
63
} }
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
+ } ;
84
69
85
70
export const Toggle = styled < IToggleProps > ( BasicToggle as any ) `` ;
0 commit comments