aboutsummaryrefslogtreecommitdiff
path: root/src/settings/components/ui/Input.tsx
blob: 13a246bd5f62989d8fedd53387ef05a04f527af1 (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
import React from 'react';
import PropTypes from 'prop-types';
import './Input.scss';

class Input extends React.Component {

  renderText(props) {
    let inputClassName = props.error ? 'input-error' : '';
    return <div className='settings-ui-input'>
      <label htmlFor={props.id}>{ props.label }</label>
      <input type='text' className={inputClassName} {...props} />
    </div>;
  }

  renderRadio(props) {
    let inputClassName = props.error ? 'input-error' : '';
    return <div className='settings-ui-input'>
      <label>
        <input type='radio' className={inputClassName} {...props} />
        { props.label }
      </label>
    </div>;
  }

  renderTextArea(props) {
    let inputClassName = props.error ? 'input-error' : '';
    return <div className='settings-ui-input'>
      <label
        htmlFor={props.id}
      >{ props.label }</label>
      <textarea className={inputClassName} {...props} />
      <p className='settings-ui-input-error'>{ this.props.error }</p>
    </div>;
  }

  render() {
    let { type } = this.props;

    switch (this.props.type) {
    case 'text':
      return this.renderText(this.props);
    case 'radio':
      return this.renderRadio(this.props);
    case 'textarea':
      return this.renderTextArea(this.props);
    default:
      console.warn(`Unsupported input type ${type}`);
    }
    return null;
  }
}

Input.propTypes = {
  type: PropTypes.string,
  error: PropTypes.string,
  label: PropTypes.string,
  value: PropTypes.string,
};

export default Input;