diff options
author | 2019-01-11 21:07:21 +0100 | |
---|---|---|
committer | 2019-01-11 21:07:21 +0100 | |
commit | 0ac2806481852249c6a2d4a032cc2c0324a58ef8 (patch) | |
tree | 03292af10a5a97b824293ff8878d2a5c821d5ed2 /packages/forms | |
parent | expose legacy styles (diff) | |
download | ferdium-app-0ac2806481852249c6a2d4a032cc2c0324a58ef8.tar.gz ferdium-app-0ac2806481852249c6a2d4a032cc2c0324a58ef8.tar.zst ferdium-app-0ac2806481852249c6a2d4a032cc2c0324a58ef8.zip |
Add toggle element to @meetfranz/forms
Diffstat (limited to 'packages/forms')
-rw-r--r-- | packages/forms/src/index.ts | 3 | ||||
-rw-r--r-- | packages/forms/src/input/index.tsx | 10 | ||||
-rw-r--r-- | packages/forms/src/input/styles.ts | 2 | ||||
-rw-r--r-- | packages/forms/src/toggle/index.tsx | 117 | ||||
-rw-r--r-- | packages/forms/src/typings/generic.ts | 7 | ||||
-rw-r--r-- | packages/forms/src/wrapper/index.tsx | 2 |
6 files changed, 131 insertions, 10 deletions
diff --git a/packages/forms/src/index.ts b/packages/forms/src/index.ts index 3f24ecaf7..a506de111 100644 --- a/packages/forms/src/index.ts +++ b/packages/forms/src/index.ts | |||
@@ -1 +1,2 @@ | |||
1 | export { default as Input } from './input'; | 1 | export { Input } from './input'; |
2 | export { Toggle } from './toggle'; | ||
diff --git a/packages/forms/src/input/index.tsx b/packages/forms/src/input/index.tsx index 0e19e3368..d82ee5fe3 100644 --- a/packages/forms/src/input/index.tsx +++ b/packages/forms/src/input/index.tsx | |||
@@ -16,14 +16,12 @@ import scorePasswordFunc from './scorePassword'; | |||
16 | 16 | ||
17 | import styles from './styles'; | 17 | import styles from './styles'; |
18 | 18 | ||
19 | interface IProps extends IFormField, React.InputHTMLAttributes<HTMLInputElement>, IWithStyle { | 19 | interface IProps extends React.InputHTMLAttributes<HTMLInputElement>, IFormField, IWithStyle { |
20 | label: string; | ||
21 | focus?: boolean; | 20 | focus?: boolean; |
22 | prefix?: string; | 21 | prefix?: string; |
23 | suffix?: string; | 22 | suffix?: string; |
24 | scorePassword?: boolean; | 23 | scorePassword?: boolean; |
25 | showPasswordToggle?: boolean; | 24 | showPasswordToggle?: boolean; |
26 | error?: string; | ||
27 | } | 25 | } |
28 | 26 | ||
29 | interface IState { | 27 | interface IState { |
@@ -32,15 +30,15 @@ interface IState { | |||
32 | } | 30 | } |
33 | 31 | ||
34 | @observer | 32 | @observer |
35 | class Input extends Component<IProps, IState> { | 33 | class InputComponent extends Component<IProps, IState> { |
36 | public static defaultProps = { | 34 | public static defaultProps = { |
37 | classes: {}, | ||
38 | focus: false, | 35 | focus: false, |
39 | onChange: () => {}, | 36 | onChange: () => {}, |
40 | scorePassword: false, | 37 | scorePassword: false, |
41 | showLabel: true, | 38 | showLabel: true, |
42 | showPasswordToggle: false, | 39 | showPasswordToggle: false, |
43 | type: 'text', | 40 | type: 'text', |
41 | disabled: false, | ||
44 | }; | 42 | }; |
45 | 43 | ||
46 | state = { | 44 | state = { |
@@ -172,4 +170,4 @@ class Input extends Component<IProps, IState> { | |||
172 | } | 170 | } |
173 | } | 171 | } |
174 | 172 | ||
175 | export default injectSheet(styles)(Input); | 173 | export const Input = injectSheet(styles)(InputComponent); |
diff --git a/packages/forms/src/input/styles.ts b/packages/forms/src/input/styles.ts index 2cdda12ee..2b34e92c3 100644 --- a/packages/forms/src/input/styles.ts +++ b/packages/forms/src/input/styles.ts | |||
@@ -1,5 +1,5 @@ | |||
1 | import { Theme } from '@meetfranz/theme'; | ||
1 | import CSS from 'csstype'; | 2 | import CSS from 'csstype'; |
2 | import { Theme } from '../../../theme/lib'; | ||
3 | 3 | ||
4 | const prefixStyles = (theme: Theme) => ({ | 4 | const prefixStyles = (theme: Theme) => ({ |
5 | background: theme.inputPrefixBackground, | 5 | background: theme.inputPrefixBackground, |
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); | ||
diff --git a/packages/forms/src/typings/generic.ts b/packages/forms/src/typings/generic.ts index e1e497050..bd3ea364b 100644 --- a/packages/forms/src/typings/generic.ts +++ b/packages/forms/src/typings/generic.ts | |||
@@ -1,11 +1,14 @@ | |||
1 | import { Classes } from 'jss'; | 1 | import { Classes } from 'jss'; |
2 | 2 | ||
3 | export interface IFormField { | 3 | export interface IFormField { |
4 | title?: string; | ||
5 | showLabel?: boolean; | 4 | showLabel?: boolean; |
6 | isError?: boolean; | 5 | label?: string; |
6 | error?: string; | ||
7 | } | 7 | } |
8 | 8 | ||
9 | export interface IWithStyle { | 9 | export interface IWithStyle { |
10 | classes: Classes; | 10 | classes: Classes; |
11 | } | 11 | } |
12 | |||
13 | export type Merge<M, N> = Omit<M, Extract<keyof M, keyof N>> & N; | ||
14 | export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>; | ||
diff --git a/packages/forms/src/wrapper/index.tsx b/packages/forms/src/wrapper/index.tsx index c2444b0ea..b3e92da66 100644 --- a/packages/forms/src/wrapper/index.tsx +++ b/packages/forms/src/wrapper/index.tsx | |||
@@ -1,3 +1,4 @@ | |||
1 | import { observer } from 'mobx-react'; | ||
1 | import React, { Component } from 'react'; | 2 | import React, { Component } from 'react'; |
2 | import injectStyle from 'react-jss'; | 3 | import injectStyle from 'react-jss'; |
3 | import { IWithStyle } from '../typings/generic'; | 4 | import { IWithStyle } from '../typings/generic'; |
@@ -8,6 +9,7 @@ interface IProps extends IWithStyle { | |||
8 | children: React.ReactNode; | 9 | children: React.ReactNode; |
9 | } | 10 | } |
10 | 11 | ||
12 | @observer | ||
11 | class Wrapper extends Component<IProps> { | 13 | class Wrapper extends Component<IProps> { |
12 | render() { | 14 | render() { |
13 | const { | 15 | const { |