aboutsummaryrefslogtreecommitdiff
path: root/src/settings/components/form/PropertiesForm.jsx
blob: 9143a7a9da0a6d6e2ecac72a3879f95200a3e885 (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
import './PropertiesForm.scss';
import React from 'react';
import PropTypes from 'prop-types';

class PropertiesForm extends React.Component {

  render() {
    let types = this.props.types;
    let value = this.props.value;
    if (!value) {
      value = {};
    }

    return <div className='form-properties-form'>
      {
        Object.keys(types).map((name) => {
          let type = types[name];
          let inputType = null;
          if (type === 'string') {
            inputType = 'text';
          } else if (type === 'number') {
            inputType = 'number';
          } else if (type === 'boolean') {
            inputType = 'checkbox';
          }
          return <div key={name} className='form-properties-form-row'>
            <label>
              <span className='column-name'>{name}</span>
              <input type={inputType} name={name}
                className='column-input'
                value={value[name] ? value[name] : ''}
                onChange={this.bindValue.bind(this)}
                checked={value[name]}
              />
            </label>
          </div>;
        })
      }
    </div>;
  }

  bindValue(e) {
    if (!this.props.onChange) {
      return;
    }

    let name = e.target.name;
    let next = { ...this.props.value };
    if (e.target.type.toLowerCase() === 'checkbox') {
      next[name] = e.target.checked;
    } else if (e.target.type.toLowerCase() === 'number') {
      next[name] = Number(e.target.value);
    } else {
      next[name] = e.target.value;
    }

    this.props.onChange(next);
  }
}

PropertiesForm.propTypes = {
  value: PropTypes.objectOf(PropTypes.any),
  onChange: PropTypes.func,
};

export default PropertiesForm;