/
textarea.js
118 lines (75 loc) · 2.52 KB
/
textarea.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
var Widget = require('../common/widget'),
html = require('nanohtml')
class TextArea extends Widget {
static description() {
return 'Text area (multi line input). Tip: hit shift + enter for new lines.'
}
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.on('focus', this.onFocus.bind(this), {element: this.input})
this.on('blur', (e)=>{
this.blur(true)
}, {element: this.input})
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.value
}
})
}
}
onFocus() {
if (this.focused) return
this.focused = true
}
focus() {
this.input.focus()
}
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 = v === '' || v === null ? this.getProp('default') : String(v)
if (this.value === '') 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