-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathinput.js
33 lines (30 loc) · 973 Bytes
/
input.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
import {observe} from "./observe.js";
export function input(input) {
return observe(function(change) {
var event = eventof(input), value = valueof(input);
function inputted() { change(valueof(input)); }
input.addEventListener(event, inputted);
if (value !== undefined) change(value);
return function() { input.removeEventListener(event, inputted); };
});
}
function valueof(input) {
switch (input.type) {
case "range":
case "number": return input.valueAsNumber;
case "date": return input.valueAsDate;
case "checkbox": return input.checked;
case "file": return input.multiple ? input.files : input.files[0];
case "select-multiple": return Array.from(input.selectedOptions, o => o.value);
default: return input.value;
}
}
function eventof(input) {
switch (input.type) {
case "button":
case "submit":
case "checkbox": return "click";
case "file": return "change";
default: return "input";
}
}