-
Notifications
You must be signed in to change notification settings - Fork 34
/
textarea.js
55 lines (54 loc) · 1.36 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
import {html} from "htl";
import {maybeWidth} from "./css.js";
import {bubbles} from "./event.js";
import {maybeLabel} from "./label.js";
import {createText, onoff, truefalse} from "./text.js";
export function textarea({
value = "",
label,
placeholder,
spellcheck,
autocomplete,
autocapitalize,
rows = 3,
minlength,
maxlength,
required = minlength > 0,
readonly,
disabled,
monospace = false,
resize = rows < 12,
width,
...options
} = {}) {
const input = html`<textarea
name=text
readonly=${readonly}
disabled=${disabled}
required=${required}
rows=${rows}
minlength=${minlength}
maxlength=${maxlength}
spellcheck=${truefalse(spellcheck)}
autocomplete=${onoff(autocomplete)}
autocapitalize=${onoff(autocapitalize)}
placeholder=${placeholder}
onkeydown=${onkeydown}
style=${{
width,
fontFamily: monospace ? "var(--monospace, monospace)" : null,
resize: resize ? null : "none"
}}
>`;
const form = html`<form class="__ns__ __ns__-textarea" style=${maybeWidth(width)}>
${maybeLabel(label, input)}<div>
${input}
</div>
</form>`;
function onkeydown(event) {
if (options.submit && event.key === "Enter" && (event.metaKey || event.ctrlKey)) {
return form.dispatchEvent(new Event("submit", bubbles));
}
}
return createText(form, input, value, options);
}