1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-23 18:41:38 +01:00
metamask-extension/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 22:18:32 +02:00
function EditableLabel () {
Component.call(this)
}
2016-06-21 22:18:32 +02:00
EditableLabel.prototype.render = function () {
const props = this.props
2016-06-21 22:56:04 +02:00
const state = this.state
if (state && state.isEditingLabel) {
return h('div.editable-label', [
2016-06-12 19:22:03 +02:00
h('input.sizing-input', {
defaultValue: props.textValue,
maxLength: '20',
2016-06-21 22:18:32 +02:00
onKeyPress: (event) => {
this.saveIfEnter(event)
},
}),
h('button.editable-button', {
2016-06-21 22:18:32 +02:00
onClick: () => this.saveText(),
}, 'Save'),
])
} else {
2016-06-10 05:58:33 +02:00
return h('div.name-label', {
2016-06-21 22:18:32 +02:00
onClick: (event) => {
this.setState({ isEditingLabel: true })
},
}, this.props.children)
}
}
2016-06-21 22:18:32 +02:00
EditableLabel.prototype.saveIfEnter = function (event) {
if (event.key === 'Enter') {
this.saveText()
}
}
2016-06-21 22:18:32 +02:00
EditableLabel.prototype.saveText = function () {
var container = findDOMNode(this)
var text = container.querySelector('.editable-label input').value
var truncatedText = text.substring(0, 20)
2016-06-28 20:34:30 +02:00
this.props.saveText(truncatedText)
this.setState({ isEditingLabel: false, textLabel: truncatedText })
}