forked from observablehq/inputs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.js
59 lines (56 loc) · 1.47 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import {html} from "htl";
import {maybeWidth} from "./css.js";
import {maybeLabel} from "./label.js";
import {createText} from "./text.js";
export function fileOf(AbstractFile) {
class LocalFile extends AbstractFile {
constructor(file) {
super(file.name);
Object.defineProperty(this, "_", {value: file});
Object.defineProperty(this, "_url", {writable: true});
}
async url() {
return this._url || (this._url = URL.createObjectURL(this._));
}
async blob() {
return this._;
}
async stream() {
return this._.stream();
}
}
return function file({
label,
required,
accept,
capture,
multiple,
disabled,
width,
value, // eslint-disable-line no-unused-vars
submit, // eslint-disable-line no-unused-vars
...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 => new LocalFile(file))
: input.files.length ? new LocalFile(input.files[0])
: null,
set: () => {}, // ignored
same: () => false // ignored
});
};
}