Skip to content
Snippets Groups Projects
StyledSwitch.tsx 1.99 KiB
Newer Older
  • Learn to ignore specific revisions
  • Yoan VALLET's avatar
    Yoan VALLET committed
    import { withStyles } from '@material-ui/core/styles'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import Switch, { SwitchProps } from '@material-ui/core/Switch'
    
    import { FluidType } from 'enums'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import React from 'react'
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    
    const SwitchBase = withStyles({
      root: {
        paddingLeft: 0,
        width: 52,
        overflow: 'initial',
      },
      switchBase: {
        color: 'var(--greyBright)',
        width: 'auto',
        left: -8,
        '&$checked': {
          color: 'var(--greyBright)',
          '& + $track': {
            opacity: 1,
            backgroundColor: 'var(--multiColor)',
          },
        },
      },
      checked: {},
      track: {
        opacity: 1,
        backgroundColor: 'var(--greyDark)',
        marginTop: 'initial',
        marginLeft: 'initial',
        left: 0,
      },
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    })(Switch) as React.FC<StyledSwitchProps>
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    
    const SwitchElec = withStyles({
      switchBase: {
        '&$checked': {
          '& + $track': {
            backgroundColor: 'var(--elecColor)',
          },
        },
      },
      checked: {},
      track: {},
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    })(SwitchBase) as React.FC<StyledSwitchProps>
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    
    const SwitchWater = withStyles({
      switchBase: {
        '&$checked': {
          '& + $track': {
            backgroundColor: 'var(--waterColor)',
          },
        },
      },
      checked: {},
      track: {},
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    })(SwitchBase) as React.FC<StyledSwitchProps>
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    
    const SwitchGas = withStyles({
      switchBase: {
        '&$checked': {
          '& + $track': {
            backgroundColor: 'var(--gasColor)',
          },
        },
      },
      checked: {},
      track: {},
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    })(SwitchBase) as React.FC<StyledSwitchProps>
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    
    interface StyledSwitchProps extends SwitchProps {
      fluidType?: FluidType
    }
    
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    const StyledSwitch = ({ fluidType, ...props }: StyledSwitchProps) => {
    
      if (fluidType === undefined) {
    
    Yoan VALLET's avatar
    Yoan VALLET committed
        return <SwitchBase {...props} />
      }
    
      switch (fluidType) {
        case FluidType.ELECTRICITY:
          return <SwitchElec {...props} />
        case FluidType.WATER:
          return <SwitchWater {...props} />
        case FluidType.GAS:
          return <SwitchGas {...props} />
        default:
          return <SwitchBase {...props} />
      }
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    }
    
    export default StyledSwitch