-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathfile.js
40 lines (39 loc) · 1006 Bytes
/
file.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
import {html} from "htl";
import {maybeWidth} from "./css.js";
import {maybeLabel} from "./label.js";
import {createText} from "./text.js";
export function file({
label,
required,
accept,
capture,
multiple,
disabled,
width,
value, // eslint-disable-line no-unused-vars
submit, // eslint-disable-line no-unused-vars
transform = (file) => file,
...options
} = {}) {
const input = html`<input
type=file
name=file
disabled=${disabled}
required=${required}
accept=${accept}
capture=${capture}
multiple=${multiple}
>`;
const form = html`<form class=__ns__ style=${maybeWidth(width)}>
${maybeLabel(label, input)}<div class=__ns__-input>
${input}
</div>
</form>`;
return createText(form, input, undefined, options, {
get: (input) => multiple ? Array.from(input.files, (file) => transform(file))
: input.files.length ? transform(input.files[0])
: null,
set: () => {}, // ignored
same: () => false // ignored
});
}