diff options
Diffstat (limited to 'packages/forms/src/toggle/index.tsx')
-rw-r--r-- | packages/forms/src/toggle/index.tsx | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/packages/forms/src/toggle/index.tsx b/packages/forms/src/toggle/index.tsx new file mode 100644 index 000000000..759694dc8 --- /dev/null +++ b/packages/forms/src/toggle/index.tsx | |||
@@ -0,0 +1,117 @@ | |||
1 | import { Theme } from '@meetfranz/theme'; | ||
2 | import classnames from 'classnames'; | ||
3 | import CSS from 'csstype'; | ||
4 | import { observer } from 'mobx-react'; | ||
5 | import React, { Component, createRef } from 'react'; | ||
6 | import injectStyle from 'react-jss'; | ||
7 | |||
8 | import { IFormField, IWithStyle, Omit } from '../typings/generic'; | ||
9 | |||
10 | import Error from '../error'; | ||
11 | import Label from '../label'; | ||
12 | import Wrapper from '../wrapper'; | ||
13 | |||
14 | interface IProps extends React.InputHTMLAttributes<HTMLInputElement>, IFormField, IWithStyle { | ||
15 | // field: any; | ||
16 | } | ||
17 | |||
18 | const styles = (theme: Theme) => ({ | ||
19 | toggle: { | ||
20 | background: theme.toggleBackground, | ||
21 | borderRadius: theme.borderRadius, | ||
22 | height: theme.toggleHeight, | ||
23 | position: 'relative' as CSS.PositionProperty, | ||
24 | width: theme.toggleWidth, | ||
25 | }, | ||
26 | button: { | ||
27 | background: theme.toggleButton, | ||
28 | borderRadius: '100%', | ||
29 | boxShadow: '0 1px 4px rgba(0, 0, 0, .3)', | ||
30 | width: theme.toggleHeight - 2, | ||
31 | height: theme.toggleHeight - 2, | ||
32 | left: 1, | ||
33 | top: 1, | ||
34 | position: 'absolute' as CSS.PositionProperty, | ||
35 | transition: 'all .5s', | ||
36 | }, | ||
37 | buttonActive: { | ||
38 | background: theme.toggleButtonActive, | ||
39 | left: (theme.toggleWidth - theme.toggleHeight) + 1, | ||
40 | }, | ||
41 | input: { | ||
42 | visibility: 'hidden' as any, | ||
43 | }, | ||
44 | disabled: { | ||
45 | opacity: 0.5, | ||
46 | }, | ||
47 | toggleLabel: { | ||
48 | display: 'flex', | ||
49 | |||
50 | '& > span': { | ||
51 | order: 1, | ||
52 | marginLeft: 15, | ||
53 | marginTop: 2, | ||
54 | }, | ||
55 | }, | ||
56 | }); | ||
57 | |||
58 | @observer | ||
59 | class ToggleComponent extends Component<IProps> { | ||
60 | public static defaultProps = { | ||
61 | onChange: () => {}, | ||
62 | showLabel: true, | ||
63 | disabled: false, | ||
64 | error: '', | ||
65 | }; | ||
66 | |||
67 | render() { | ||
68 | const { | ||
69 | classes, | ||
70 | disabled, | ||
71 | error, | ||
72 | id, | ||
73 | label, | ||
74 | showLabel, | ||
75 | checked, | ||
76 | value, | ||
77 | onChange, | ||
78 | } = this.props; | ||
79 | |||
80 | console.log('props', this.props); | ||
81 | |||
82 | return ( | ||
83 | <Wrapper> | ||
84 | <Label | ||
85 | title={label} | ||
86 | showLabel={showLabel} | ||
87 | htmlFor={id} | ||
88 | className={classes.toggleLabel} | ||
89 | > | ||
90 | <div className={classnames({ | ||
91 | [`${classes.toggle}`]: true, | ||
92 | [`${classes.disabled}`]: disabled, | ||
93 | })}> | ||
94 | <div className={classnames({ | ||
95 | [`${classes.button}`]: true, | ||
96 | [`${classes.buttonActive}`]: checked, | ||
97 | })} /> | ||
98 | <input | ||
99 | className={classes.input} | ||
100 | id={id || name} | ||
101 | type="checkbox" | ||
102 | checked={checked} | ||
103 | value={value} | ||
104 | onChange={onChange} | ||
105 | disabled={disabled} | ||
106 | /> | ||
107 | </div> | ||
108 | </Label> | ||
109 | {error && ( | ||
110 | <Error message={error} /> | ||
111 | )} | ||
112 | </Wrapper> | ||
113 | ); | ||
114 | } | ||
115 | } | ||
116 | |||
117 | export const Toggle = injectStyle(styles)(ToggleComponent); | ||