2016-05-21 01:18:54 +02:00
|
|
|
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', [
|
2016-06-12 19:22:03 +02:00
|
|
|
h('input.sizing-input', {
|
2016-05-21 01:18:54 +02:00
|
|
|
defaultValue: props.textValue,
|
|
|
|
onKeyPress:(event) => {
|
|
|
|
this.saveIfEnter(event)
|
|
|
|
},
|
|
|
|
}),
|
2016-06-06 16:09:21 +02:00
|
|
|
h('button.editable-button', {
|
2016-05-21 01:18:54 +02:00
|
|
|
onClick:() => this.saveText(),
|
|
|
|
}, 'Save')
|
|
|
|
])
|
|
|
|
|
|
|
|
} else {
|
2016-06-10 05:58:33 +02:00
|
|
|
return h('div.name-label', {
|
2016-05-21 01:18:54 +02:00
|
|
|
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 })
|
|
|
|
}
|