aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/ui/ColorPickerInput.tsx
blob: da1fffb716c2c0d8c1811c64db9933ada760a4ca (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
import {
  ChangeEvent,
  ChangeEventHandler,
  Component,
  createRef,
  RefObject,
} from 'react';
import { observer } from 'mobx-react';
import classnames from 'classnames';
import { SliderPicker } from 'react-color';
import { noop } from 'lodash';
import { Field } from '../../@types/mobx-form.types';

interface IProps {
  field: Field;
  className?: string;
  focus?: boolean;
  onChange: ChangeEventHandler<HTMLInputElement>;
}

// TODO - [TS DEBT] check if field can be spread instead of having it single field attribute in interface
@observer
class ColorPickerInput extends Component<IProps> {
  private inputElement: RefObject<HTMLInputElement> =
    createRef<HTMLInputElement>();

  componentDidMount() {
    const { focus = false } = this.props;
    if (focus) {
      this.focus();
    }
  }

  onChange(e: ChangeEvent<HTMLInputElement>) {
    const { field, onChange = noop } = this.props;

    onChange(e);
    if (field.onChange) {
      field.onChange(e);
    }
  }

  focus() {
    if (this.inputElement && this.inputElement.current) {
      this.inputElement.current.focus();
    }
  }

  handleChangeComplete = (color: { hex: string }) => {
    const { field } = this.props;
    field.value = color.hex;
  };

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

    let { type } = field;
    type = 'text';

    return (
      <div
        className={classnames({
          'franz-form__field': true,
          'has-error': field.error,
          [`${className}`]: className,
        })}
      >
        <SliderPicker
          color={field.value}
          onChangeComplete={this.handleChangeComplete}
          id={field.id}
          type={type}
          className="franz-form__input"
          name={field.name}
          value={field.value}
          placeholder={field.placeholder}
          onBlur={field.onBlur}
          onFocus={field.onFocus}
          ref={this.inputElement}
          disabled={field.disabled}
        />
        <div className="franz-form__input-wrapper franz-form__input-wrapper__color-picker">
          <input
            id={field.id}
            type={type}
            className="franz-form__input"
            name={field.name}
            value={field.value}
            placeholder={field.placeholder}
            onChange={e => this.onChange(e)}
            onBlur={field.onBlur}
            onFocus={field.onFocus}
            ref={this.inputElement}
            disabled={field.disabled}
          />
        </div>
      </div>
    );
  }
}

export default ColorPickerInput;