Create matching custom element for V4 text area

This commit is contained in:
Michael Hadley 2019-01-16 12:49:03 -08:00
parent 2c4e54756e
commit ccb52d032c
2 changed files with 58 additions and 0 deletions

57
lib/TextArea/V4.js Normal file
View File

@ -0,0 +1,57 @@
CustomElement = require('../CustomElement')
CustomElement.create({
tagName: 'nri-textarea-v4',
initialize: function() {
this._autoresize = false
},
onConnect: function() {
this._textarea = this.querySelector('textarea')
this._updateListener()
},
observedAttributes: ['data-autoresize'],
onAttributeChange: function(name, previous, next) {
if (name === 'data-autoresize') {
this._autoresize = next !== null
if (!this._textarea) return
this._updateListener()
}
},
methods: {
_updateListener: function() {
if (this._autoresize) {
this._textarea.addEventListener('input', this._resize)
this._resize()
} else {
this._textarea.removeEventListener('input', this._resize)
}
},
_resize: function() {
var minHeight = null
if (this._textarea.style.minHeight) {
minHeight = parseInt(this._textarea.style.minHeight, 10)
} else {
minHeight = parseInt(window.getComputedStyle(this._textarea).minHeight, 10)
}
if (minHeight === 0) {
minHeight = parseInt(window.getComputedStyle(this._textarea).height, 10)
}
this._textarea.style.overflowY = 'hidden'
this._textarea.style.minHeight = minHeight + 'px'
this._textarea.style.transition = 'none'
if (this._textarea.scrollHeight > minHeight) {
this._textarea.style.height = minHeight + 'px'
this._textarea.style.height = this._textarea.scrollHeight + 'px'
} else {
this._textarea.style.height = minHeight + 'px'
}
}
}
})

View File

@ -1 +1,2 @@
require('./TextArea/V3')
require('./TextArea/V4')