summaryrefslogtreecommitdiff
path: root/app/client/common/numberInput.component.js
blob: c3ad24cbf15a7fc6ec0be87b770d00b51f4e2cf9 (plain)
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
import { h, Component } from 'preact'

class NumberInput extends Component {
  constructor(props){
    super(props)
    this.state = { value: null, changed: false }
    this.handleInput = this.handleInput.bind(this)
    this.handleKeydown = this.handleKeydown.bind(this)
  }
  handleInput(e){
    this.setState({
      value: e.target.value,
      changed: true,
    })
    this.props.onInput && this.props.onInput(e.target.value, e.target.name)
  }
  handleKeydown(e){
    if (e.keyCode === 13) {
      this.setState({
        value: e.target.value,
        changed: false,
      })
      this.props.onSave && this.props.onSave(e.target.value, e.target.name)
    }
  }
  render() {
    return (
      <div className='numberInput param'>
        <label>
          <span>{this.props.title}</span>
          <input
            type={'number'}
            name={this.props.name || 'number'}
            value={this.state.changed ? this.state.value : this.props.value}
            onInput={this.handleInput}
            onKeydown={this.handleKeydown}
            placeholder={this.props.placeholder}
            autofocus={this.props.autofocus}
            min={this.props.min}
            max={this.props.max}
            step={this.props.step || this.props.type === 'int' ? 1 : 0.01}
          />
        </label>
      </div>
    )
  }
}

export default NumberInput