nifty-wallet/ui/app/components/editable-label.js

52 lines
1.3 KiB
JavaScript
Raw Normal View History

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 13:18:32 -07:00
function EditableLabel () {
Component.call(this)
}
2016-06-21 13:18:32 -07:00
EditableLabel.prototype.render = function () {
2016-12-16 10:04:57 -08:00
const props = this.props
const state = this.state
if (state && state.isEditingLabel) {
return h('div.editable-label', [
h('input.sizing-input', {
defaultValue: props.textValue,
maxLength: '20',
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()
}
}
2016-06-21 13:18:32 -07:00
EditableLabel.prototype.saveText = function () {
2016-12-16 10:04:57 -08:00
var container = findDOMNode(this)
var text = container.querySelector('.editable-label input').value
var truncatedText = text.substring(0, 20)
2016-06-28 11:34:30 -07:00
this.props.saveText(truncatedText)
this.setState({ isEditingLabel: false, textLabel: truncatedText })
}