aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/components/editable-label.js
blob: 41c129d4e2f2c48f96f2bf173d8c942ccd298a65 (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
const Component = require('react').Component
const h = require('react-hyperscript')
const inherits = require('util').inherits
const findDOMNode = require('react-dom').findDOMNode

module.exports = EditableLabel


inherits(EditableLabel, Component)
function EditableLabel() {
  Component.call(this)
}

EditableLabel.prototype.render = function() {
  const props = this.props
  let state = this.state

  if (state && state.isEditingLabel) {

    return h('div.editable-label', [
      h('input.sizing-input', {
        defaultValue: props.textValue,
        onKeyPress:(event) => {
          this.saveIfEnter(event)
        },
      }),
      h('button.editable-button', {
        onClick:() => this.saveText(),
      }, 'Save')
    ])

  } else {
    return h('div.name-label', {
      onClick:(event) => {
        this.setState({ isEditingLabel: true })
      },
    }, this.props.children)
  }
}

EditableLabel.prototype.saveIfEnter = function(event) {
  if (event.key === 'Enter') {
    this.saveText()
  }
}

EditableLabel.prototype.saveText = function() {
  var container = findDOMNode(this)
  var text = container.querySelector('.editable-label input').value
  this.props.saveText(text)
  this.setState({ isEditingLabel: false, textLabel: text })
}