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)
|
2016-06-21 22:18:32 +02:00
|
|
|
function EditableLabel () {
|
2016-05-21 01:18:54 +02:00
|
|
|
Component.call(this)
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
EditableLabel.prototype.render = function () {
|
2016-05-21 01:18:54 +02:00
|
|
|
const props = this.props
|
2016-06-21 22:56:04 +02:00
|
|
|
const state = this.state
|
2016-05-21 01:18:54 +02:00
|
|
|
|
|
|
|
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,
|
2016-06-28 22:10:29 +02:00
|
|
|
maxLength: '20',
|
2016-06-21 22:18:32 +02:00
|
|
|
onKeyPress: (event) => {
|
2016-05-21 01:18:54 +02:00
|
|
|
this.saveIfEnter(event)
|
|
|
|
},
|
|
|
|
}),
|
2016-06-06 16:09:21 +02:00
|
|
|
h('button.editable-button', {
|
2016-06-21 22:18:32 +02:00
|
|
|
onClick: () => this.saveText(),
|
|
|
|
}, 'Save'),
|
2016-05-21 01:18:54 +02:00
|
|
|
])
|
|
|
|
} else {
|
2016-06-10 05:58:33 +02:00
|
|
|
return h('div.name-label', {
|
2016-06-21 22:18:32 +02:00
|
|
|
onClick: (event) => {
|
2016-05-21 01:18:54 +02:00
|
|
|
this.setState({ isEditingLabel: true })
|
|
|
|
},
|
|
|
|
}, this.props.children)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
EditableLabel.prototype.saveIfEnter = function (event) {
|
2016-05-21 01:18:54 +02:00
|
|
|
if (event.key === 'Enter') {
|
|
|
|
this.saveText()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
EditableLabel.prototype.saveText = function () {
|
2016-05-21 01:18:54 +02:00
|
|
|
var container = findDOMNode(this)
|
|
|
|
var text = container.querySelector('.editable-label input').value
|
2016-06-28 22:10:57 +02:00
|
|
|
var truncatedText = text.substring(0, 20)
|
2016-06-28 20:34:30 +02:00
|
|
|
this.props.saveText(truncatedText)
|
|
|
|
this.setState({ isEditingLabel: false, textLabel: truncatedText })
|
2016-05-21 01:18:54 +02:00
|
|
|
}
|