widgets: textarea proto

This commit is contained in:
jean-emmanuel 2021-09-27 11:25:11 +02:00
parent 51260d98e6
commit 1587165c62
4 changed files with 152 additions and 1 deletions

View file

@ -0,0 +1,113 @@
var Widget = require('../common/widget'),
{deepCopy} = require('../../utils'),
html = require('nanohtml')
class TextArea extends Widget {
static description() {
return 'Text area (multi line input).'
}
static defaults() {
return super.defaults().extend({
style: {
// _separator_input_style: 'Textarea style',
},
class_specific: {
},
})
}
constructor(options) {
super({...options, html: html`
<inner>
<textarea></textarea>
</inner>
`})
this.value = ''
this.stringValue = ''
this.focused = false
this.validation = null
this.input = DOM.get(this.widget, 'textarea')[0]
if (this.getProp('align') === 'left') this.widget.classList.add('left')
if (this.getProp('align') === 'right') this.widget.classList.add('right')
if (this.getProp('interaction')) {
this.input.setAttribute('tabindex', 0)
this.input.addEventListener('focus', this.focus.bind(this))
this.input.addEventListener('blur', (e)=>{
this.blur(true)
})
this.input.addEventListener('keydown', (e)=>{
if (e.keyCode === 13 && !e.shiftKey) {
// enter
e.preventDefault()
this.blur()
}
else if (e.keyCode === 27) {
// esc
this.blur(false)
this.input.value = this.stringValue
}
})
}
}
focus() {
if (this.focused) return
this.focused = true
}
blur(change=true) {
if (!this.focused) return
this.focused = false
this.input.blur()
if (change) this.inputChange()
}
inputChange() {
this.setValue(this.input.value, {sync:true, send:true})
}
setValue(v, options={} ) {
this.value = String(v)
if (this.value === '' || this.value === null) this.value = this.getProp('default')
if (this.input.value !== this.value) this.input.value = this.value
if (options.send && !options.fromSync) this.sendValue()
if (options.sync) this.changed(options)
}
}
TextArea.dynamicProps = TextArea.prototype.constructor.dynamicProps
.filter(x=>x !== 'interaction')
module.exports = TextArea

View file

@ -6,6 +6,7 @@ module.exports.widgets = {
dropdown: require('./basics/dropdown'),
menu: require('./basics/menu'),
input: require('./basics/input'),
textarea: require('./basics/textarea'),
file: require('./basics/file'),
// containers
@ -54,7 +55,7 @@ module.exports.widgets = {
}
module.exports.categories = {
'Basics':['button', 'switch', 'dropdown', 'menu', 'input', 'file'],
'Basics':['button', 'switch', 'dropdown', 'menu', 'input', 'textarea', 'file'],
'Containers':['panel', 'modal', 'clone', 'fragment', 'matrix', 'keyboard', 'patchbay'],
'Frames':['frame', 'svg', 'html', 'image'],
'Graphs':['plot','eq','visualizer'],