aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/ui/SearchInput.js
blob: bca412cef8b582534bd9f3bb3ceba22933cb0426 (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
118
119
120
121
122
123
124
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { observer } from 'mobx-react';
import classnames from 'classnames';
import uuidv1 from 'uuid/v1';
import { debounce } from 'lodash';

@observer
export default class SearchInput extends Component {
  static propTypes = {
    value: PropTypes.string,
    defaultValue: PropTypes.string,
    className: PropTypes.string,
    onChange: PropTypes.func,
    onReset: PropTypes.func,
    name: PropTypes.string,
    throttle: PropTypes.bool,
    throttleDelay: PropTypes.number,
  };

  static defaultProps = {
    value: '',
    defaultValue: '',
    className: '',
    name: uuidv1(),
    throttle: false,
    throttleDelay: 250,
    onChange: () => null,
    onReset: () => null,
  }

  constructor(props) {
    super(props);

    this.state = {
      value: props.value || props.defaultValue,
    };

    this.throttledOnChange = debounce(this.throttledOnChange, this.props.throttleDelay);
  }

  onChange(e) {
    const { throttle, onChange } = this.props;
    const { value } = e.target;
    this.setState({ value });

    if (throttle) {
      e.persist();
      this.throttledOnChange(value);
    } else {
      onChange(value);
    }
  }

  onClick() {
    const { defaultValue } = this.props;
    const { value } = this.state;

    if (value === defaultValue) {
      this.setState({ value: '' });
    }

    this.input.focus();
  }

  onBlur() {
    const { defaultValue } = this.props;
    const { value } = this.state;

    if (value === '') {
      this.setState({ value: defaultValue });
    }
  }

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

    onChange(e);
  }

  reset() {
    const { defaultValue, onReset } = this.props;
    this.setState({ value: defaultValue });

    onReset();
  }

  input = null;

  render() {
    const { className, name, defaultValue } = this.props;
    const { value } = this.state;

    return (
      <div
        className={classnames([
          className,
          'search-input',
        ])}
      >
        <label
          htmlFor={name}
          className="mdi mdi-magnify"
          onClick={() => this.onClick()}
        />
        <input
          name={name}
          type="text"
          value={value}
          onChange={e => this.onChange(e)}
          onClick={() => this.onClick()}
          onBlur={() => this.onBlur()}
          ref={(ref) => { this.input = ref; }}
        />
        {value !== defaultValue && value.length > 0 && (
          <span
            className="mdi mdi-close-circle-outline"
            onClick={() => this.reset()}
          />
        )}
      </div>
    );
  }
}