aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/ui
diff options
context:
space:
mode:
authorLibravatar Muhamed <unknown>2022-11-07 01:11:48 +0530
committerLibravatar Vijay Aravamudhan <vraravam@users.noreply.github.com>2022-11-08 17:25:27 +0530
commita1f5e31bfce97b4bbe492e24bf7d57ec2803c31a (patch)
tree881417b8917e8212fcf94547b92bba2701592745 /src/components/ui
parent6.2.1-nightly.42 [skip ci] (diff)
downloadferdium-app-a1f5e31bfce97b4bbe492e24bf7d57ec2803c31a.tar.gz
ferdium-app-a1f5e31bfce97b4bbe492e24bf7d57ec2803c31a.tar.zst
ferdium-app-a1f5e31bfce97b4bbe492e24bf7d57ec2803c31a.zip
refactor: remove toggle component's duplicate
Diffstat (limited to 'src/components/ui')
-rw-r--r--src/components/ui/Toggle.js72
-rw-r--r--src/components/ui/button/index.tsx4
-rw-r--r--src/components/ui/toggle/index.tsx56
3 files changed, 26 insertions, 106 deletions
diff --git a/src/components/ui/Toggle.js b/src/components/ui/Toggle.js
deleted file mode 100644
index c1d86a7f6..000000000
--- a/src/components/ui/Toggle.js
+++ /dev/null
@@ -1,72 +0,0 @@
1import { Component } from 'react';
2import PropTypes from 'prop-types';
3import { observer } from 'mobx-react';
4import classnames from 'classnames';
5import { Field } from 'mobx-react-form';
6
7// Can this file be merged into the './toggle/index.tsx' file?
8class Toggle extends Component {
9 static propTypes = {
10 field: PropTypes.instanceOf(Field).isRequired,
11 className: PropTypes.string,
12 showLabel: PropTypes.bool,
13 disabled: PropTypes.bool,
14 };
15
16 static defaultProps = {
17 className: '',
18 showLabel: true,
19 disabled: false,
20 };
21
22 onChange(e) {
23 const { field } = this.props;
24
25 field.onChange(e);
26 }
27
28 render() {
29 const { field, className, showLabel, disabled } = this.props;
30
31 if (field.value === '' && field.default !== '') {
32 field.value = field.default;
33 }
34
35 return (
36 <div
37 className={classnames([
38 'franz-form__field',
39 'franz-form__toggle-wrapper',
40 'franz-form__toggle-disabled',
41 className,
42 ])}
43 >
44 <label
45 htmlFor={field.id}
46 className={classnames({
47 'franz-form__toggle': true,
48 'is-active': field.value,
49 })}
50 >
51 <div className="franz-form__toggle-button" />
52 <input
53 type="checkbox"
54 id={field.id}
55 name={field.name}
56 value={field.name}
57 checked={field.value}
58 onChange={e => (!disabled ? this.onChange(e) : null)}
59 />
60 </label>
61 {field.error && <div className={field.error}>{field.error}</div>}
62 {field.label && showLabel && (
63 <label className="franz-form__label" htmlFor={field.id}>
64 {field.label}
65 </label>
66 )}
67 </div>
68 );
69 }
70}
71
72export default observer(Toggle);
diff --git a/src/components/ui/button/index.tsx b/src/components/ui/button/index.tsx
index c1e647bc0..dc984bf95 100644
--- a/src/components/ui/button/index.tsx
+++ b/src/components/ui/button/index.tsx
@@ -2,7 +2,7 @@ import Icon from '@mdi/react';
2import classnames from 'classnames'; 2import classnames from 'classnames';
3import { Property } from 'csstype'; 3import { Property } from 'csstype';
4import { noop } from 'lodash'; 4import { noop } from 'lodash';
5import { Component, MouseEvent } from 'react'; 5import { Component, MouseEventHandler } from 'react';
6import withStyles, { WithStylesProps } from 'react-jss'; 6import withStyles, { WithStylesProps } from 'react-jss';
7import Loader from 'react-loader'; 7import Loader from 'react-loader';
8import { Theme } from '../../../themes'; 8import { Theme } from '../../../themes';
@@ -136,7 +136,7 @@ interface IProps extends IFormField, WithStylesProps<typeof styles> {
136 disabled?: boolean; 136 disabled?: boolean;
137 id?: string; 137 id?: string;
138 type?: 'button' | 'reset' | 'submit' | undefined; 138 type?: 'button' | 'reset' | 'submit' | undefined;
139 onClick: (event: MouseEvent<HTMLButtonElement | HTMLAnchorElement>) => void; 139 onClick?: MouseEventHandler<HTMLInputElement>;
140 buttonType?: ButtonType; 140 buttonType?: ButtonType;
141 loaded?: boolean; 141 loaded?: boolean;
142 busy?: boolean; 142 busy?: boolean;
diff --git a/src/components/ui/toggle/index.tsx b/src/components/ui/toggle/index.tsx
index d478cbba5..fee8adbc7 100644
--- a/src/components/ui/toggle/index.tsx
+++ b/src/components/ui/toggle/index.tsx
@@ -1,27 +1,26 @@
1import classnames from 'classnames'; 1import classnames from 'classnames';
2import { Property } from 'csstype'; 2import { Property } from 'csstype';
3import { noop } from 'lodash';
3import { Component, InputHTMLAttributes } from 'react'; 4import { Component, InputHTMLAttributes } from 'react';
4import injectStyle, { WithStylesProps } from 'react-jss'; 5import withStyles, { WithStylesProps } from 'react-jss';
5
6import { Theme } from '../../../themes'; 6import { Theme } from '../../../themes';
7import { IFormField } from '../typings/generic';
8
9import Error from '../error'; 7import Error from '../error';
10import Label from '../label'; 8import Label from '../label';
9import { IFormField } from '../typings/generic';
11import Wrapper from '../wrapper'; 10import Wrapper from '../wrapper';
12 11
13interface IProps 12interface IProps
14 extends InputHTMLAttributes<HTMLInputElement>, 13 extends Omit<InputHTMLAttributes<HTMLInputElement>, 'value'>,
15 IFormField, 14 IFormField,
16 WithStylesProps<typeof styles> { 15 WithStylesProps<typeof styles> {
17 className?: string; 16 className?: string;
17 value: boolean | undefined; // due to type capability between InputHTMLAttributes and mobx-react-form
18} 18}
19 19
20let buttonTransition: string = 'none'; 20const buttonTransition: string =
21 21 window && window.matchMedia('(prefers-reduced-motion: no-preference)')
22if (window && window.matchMedia('(prefers-reduced-motion: no-preference)')) { 22 ? 'all .5s'
23 buttonTransition = 'all .5s'; 23 : 'none';
24}
25 24
26const styles = (theme: Theme) => ({ 25const styles = (theme: Theme) => ({
27 toggle: { 26 toggle: {
@@ -64,25 +63,18 @@ const styles = (theme: Theme) => ({
64}); 63});
65 64
66class ToggleComponent extends Component<IProps> { 65class ToggleComponent extends Component<IProps> {
67 public static defaultProps = {
68 onChange: () => {},
69 showLabel: true,
70 disabled: false,
71 error: '',
72 };
73
74 render() { 66 render() {
75 const { 67 const {
76 classes, 68 classes,
77 className, 69 className,
78 disabled, 70 id = '',
79 error, 71 name = '',
80 id, 72 label = '',
81 label, 73 error = '',
82 showLabel, 74 value = false,
83 checked, 75 showLabel = true,
84 value, 76 disabled = false,
85 onChange, 77 onChange = noop,
86 } = this.props; 78 } = this.props;
87 79
88 return ( 80 return (
@@ -102,24 +94,24 @@ class ToggleComponent extends Component<IProps> {
102 <div 94 <div
103 className={classnames({ 95 className={classnames({
104 [`${classes.button}`]: true, 96 [`${classes.button}`]: true,
105 [`${classes.buttonActive}`]: checked, 97 [`${classes.buttonActive}`]: value,
106 })} 98 })}
107 /> 99 />
108 <input 100 <input
109 className={classes.input}
110 id={id}
111 type="checkbox" 101 type="checkbox"
112 checked={checked} 102 id={id}
113 value={value} 103 name={name}
104 checked={value as boolean | undefined}
105 className={classes.input}
114 onChange={onChange} 106 onChange={onChange}
115 disabled={disabled} 107 disabled={disabled}
116 /> 108 />
117 </div> 109 </div>
118 </Label> 110 </Label>
119 {error && <Error message={error} />} 111 {error ? <Error message={error as string} /> : null}
120 </Wrapper> 112 </Wrapper>
121 ); 113 );
122 } 114 }
123} 115}
124 116
125export default injectStyle(styles, { injectTheme: true })(ToggleComponent); 117export default withStyles(styles, { injectTheme: true })(ToggleComponent);