aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/ui
diff options
context:
space:
mode:
authorLibravatar muhamedsalih-tw <104364298+muhamedsalih-tw@users.noreply.github.com>2022-11-07 06:27:57 +0530
committerLibravatar GitHub <noreply@github.com>2022-11-07 00:57:57 +0000
commitdd238ae7949e72e3b90235f56e14686cc5231f34 (patch)
tree4f63ef38367c9aacfe39028a68a74705bb8954fa /src/components/ui
parentTransform tray & menu files to typescript (#740) (diff)
downloadferdium-app-dd238ae7949e72e3b90235f56e14686cc5231f34.tar.gz
ferdium-app-dd238ae7949e72e3b90235f56e14686cc5231f34.tar.zst
ferdium-app-dd238ae7949e72e3b90235f56e14686cc5231f34.zip
Remove duplicated Toggle.js component (#741)
Diffstat (limited to 'src/components/ui')
-rw-r--r--src/components/ui/Toggle.js72
-rw-r--r--src/components/ui/toggle/index.tsx56
2 files changed, 24 insertions, 104 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/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);