1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-27 12:56:01 +01:00
metamask-extension/ui/app/components/editable-label.js

57 lines
1.6 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) => {
2017-07-25 02:04:13 +02:00
const nameAttribute = event.target.getAttribute('name')
// checks for class to handle smaller CTA above the account name
const classAttribute = event.target.getAttribute('class')
if (nameAttribute === 'edit' || classAttribute === 'edit-text') {
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 })
}