aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/ui/toggle
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/toggle
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/toggle')
-rw-r--r--src/components/ui/toggle/index.tsx56
1 files changed, 24 insertions, 32 deletions
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);