aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/ui/ToggleRaw.js
blob: 74292a87030be8a1330f5bf031ed071699dc7049 (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
/**
 * "Raw" Toggle - for usage without a MobX Form element
 */
import { Component } from 'react';
import PropTypes from 'prop-types';
import { observer } from 'mobx-react';
import classnames from 'classnames';

@observer
class ToggleRaw extends Component {
  static propTypes = {
    onChange: PropTypes.func.isRequired,
    field: PropTypes.shape({
      value: PropTypes.bool.isRequired,
      id: PropTypes.string,
      name: PropTypes.string,
      label: PropTypes.string,
      error: PropTypes.string,
    }).isRequired,
    className: PropTypes.string,
    showLabel: PropTypes.bool,
    disabled: PropTypes.bool,
  };

  static defaultProps = {
    className: '',
    showLabel: true,
    disabled: false,
  };

  onChange(e) {
    const { onChange } = this.props;

    onChange(e);
  }

  render() {
    const { field, className, showLabel, disabled } = this.props;

    return (
      <div
        className={classnames([
          'franz-form__field',
          'franz-form__toggle-wrapper',
          'franz-form__toggle-disabled',
          className,
        ])}
      >
        <label
          htmlFor={field.id}
          className={classnames({
            'franz-form__toggle': true,
            'is-active': field.value,
          })}
        >
          <div className="franz-form__toggle-button" />
          <input
            type="checkbox"
            id={field.id}
            name={field.name}
            value={field.name}
            checked={field.value}
            onChange={e => (!disabled ? this.onChange(e) : null)}
          />
        </label>
        {field.error && <div className={field.error}>{field.error}</div>}
        {field.label && showLabel && (
          <label className="franz-form__label" htmlFor={field.id}>
            {field.label}
          </label>
        )}
      </div>
    );
  }
}

export default ToggleRaw;