From a1f5e31bfce97b4bbe492e24bf7d57ec2803c31a Mon Sep 17 00:00:00 2001 From: Muhamed Date: Mon, 7 Nov 2022 01:11:48 +0530 Subject: refactor: remove toggle component's duplicate --- src/components/ui/toggle/index.tsx | 56 ++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 32 deletions(-) (limited to 'src/components/ui/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 @@ import classnames from 'classnames'; import { Property } from 'csstype'; +import { noop } from 'lodash'; import { Component, InputHTMLAttributes } from 'react'; -import injectStyle, { WithStylesProps } from 'react-jss'; - +import withStyles, { WithStylesProps } from 'react-jss'; import { Theme } from '../../../themes'; -import { IFormField } from '../typings/generic'; - import Error from '../error'; import Label from '../label'; +import { IFormField } from '../typings/generic'; import Wrapper from '../wrapper'; interface IProps - extends InputHTMLAttributes, + extends Omit, 'value'>, IFormField, WithStylesProps { className?: string; + value: boolean | undefined; // due to type capability between InputHTMLAttributes and mobx-react-form } -let buttonTransition: string = 'none'; - -if (window && window.matchMedia('(prefers-reduced-motion: no-preference)')) { - buttonTransition = 'all .5s'; -} +const buttonTransition: string = + window && window.matchMedia('(prefers-reduced-motion: no-preference)') + ? 'all .5s' + : 'none'; const styles = (theme: Theme) => ({ toggle: { @@ -64,25 +63,18 @@ const styles = (theme: Theme) => ({ }); class ToggleComponent extends Component { - public static defaultProps = { - onChange: () => {}, - showLabel: true, - disabled: false, - error: '', - }; - render() { const { classes, className, - disabled, - error, - id, - label, - showLabel, - checked, - value, - onChange, + id = '', + name = '', + label = '', + error = '', + value = false, + showLabel = true, + disabled = false, + onChange = noop, } = this.props; return ( @@ -102,24 +94,24 @@ class ToggleComponent extends Component {
- {error && } + {error ? : null} ); } } -export default injectStyle(styles, { injectTheme: true })(ToggleComponent); +export default withStyles(styles, { injectTheme: true })(ToggleComponent); -- cgit v1.2.3-54-g00ecf