-
Notifications
You must be signed in to change notification settings - Fork 0
/
v-default.js
76 lines (63 loc) · 1.48 KB
/
v-default.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
const vDefault = {
mounted(el, binding, vnode, prevVnode) {
let vModel = vnode.dirs[0];
let value = vModel.value;
let defaultValue = binding.value ?? el.value;
if (value === null || value === undefined) { // No value set
setValue(el, defaultValue);
}
},
updated(el, binding, vnode, prevVnode) {
if (binding.oldValue !== binding.value) { // Default value changed
let vModel = vnode.dirs[0];
if (vModel.value === binding.oldValue) { // v-model value is still the old default value
setValue(el, binding.value);
}
}
},
}
// Get the value of a form element
export function getValue(formElement) {
if (formElement.type === "checkbox") {
return formElement.checked;
}
else if (formElement.type === "radio") {
if (formElement.checked) {
return formElement.value;
}
else {
return null;
}
}
return formElement.value;
}
export function setValue(formElement, value) {
if (formElement.type === "checkbox") {
formElement.checked = value;
}
else if (formElement.type === "radio") {
if (formElement.value === value) {
formElement.checked = true;
}
}
else {
formElement.value = value;
}
let event;
if (formElement.matches("select")) {
event = new Event("change", { bubbles: true });
}
else {
event = new InputEvent("input", { bubbles: true });
}
formElement.dispatchEvent(event);
}
export default vDefault;
export const meta = {
type: "directive",
name: "default",
default: vDefault
}
if (globalThis.VApp) {
VApp.registerHelper(meta);
}