aboutsummaryrefslogtreecommitdiffstats
path: root/packages/forms/src/toggle/index.tsx
blob: 6487f1d07ce960dbb07da5cbd53f061ff19326aa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { Theme } from '@meetfranz/theme';
import classnames from 'classnames';
import CSS from 'csstype';
import React, { Component, createRef } from 'react';
import injectStyle from 'react-jss';

import { IFormField, IWithStyle, Omit } from '../typings/generic';

import { Error } from '../error';
import { Label } from '../label';
import { Wrapper } from '../wrapper';

interface IProps extends React.InputHTMLAttributes<HTMLInputElement>, IFormField, IWithStyle {
  className?: string;
}

const styles = (theme: Theme) => ({
  toggle: {
    background: theme.toggleBackground,
    borderRadius: theme.borderRadius,
    height: theme.toggleHeight,
    position: 'relative' as CSS.PositionProperty,
    width: theme.toggleWidth,
  },
  button: {
    background: theme.toggleButton,
    borderRadius: '100%',
    boxShadow: '0 1px 4px rgba(0, 0, 0, .3)',
    width: theme.toggleHeight - 2,
    height: theme.toggleHeight - 2,
    left: 1,
    top: 1,
    position: 'absolute' as CSS.PositionProperty,
    transition: 'all .5s',
  },
  buttonActive: {
    background: theme.toggleButtonActive,
    left: (theme.toggleWidth - theme.toggleHeight) + 1,
  },
  input: {
    visibility: 'hidden' as any,
  },
  disabled: {
    opacity: theme.inputDisabledOpacity,
  },
  toggleLabel: {
    display: 'flex',

    '& > span': {
      order: 1,
      marginLeft: 15,
      marginTop: 2,
    },
  },
});

class ToggleComponent extends Component<IProps> {
  public static defaultProps = {
    onChange: () => {},
    showLabel: true,
    disabled: false,
    error: '',
  };

  render() {
    const {
      classes,
      className,
      disabled,
      error,
      id,
      label,
      showLabel,
      checked,
      value,
      onChange,
    } = this.props;

    return (
      <Wrapper
        className={className}
        identifier="franz-toggle"
      >
        <Label
          title={label}
          showLabel={showLabel}
          htmlFor={id}
          className={classes.toggleLabel}
        >
          <div className={classnames({
            [`${classes.toggle}`]: true,
            [`${classes.disabled}`]: disabled,
          })}>
            <div className={classnames({
              [`${classes.button}`]: true,
              [`${classes.buttonActive}`]: checked,
            })} />
            <input
              className={classes.input}
              id={id || name}
              type="checkbox"
              checked={checked}
              value={value}
              onChange={onChange}
              disabled={disabled}
            />
          </div>
        </Label>
        {error && (
          <Error message={error} />
        )}
      </Wrapper>
    );
  }
}

export const Toggle = injectStyle(styles)(ToggleComponent);