|
1 | | - |
2 | 1 | export default { |
3 | 2 | methods: { |
4 | 3 | /** |
5 | 4 | * Prepare `data` configuration for the Vue Screen Component |
6 | 5 | * |
7 | 6 | */ |
8 | 7 | defaultValues(screen, definition) { |
9 | | - this.variables.forEach(({name, config}) => { |
| 8 | + this.variables.forEach((v) => { |
| 9 | + const { name, config } = v; |
| 10 | + const { component } = v.element; |
10 | 11 | if (this.isComputedVariable(name, definition)) return; |
| 12 | + const isCheckbox = component === "FormCheckbox"; |
11 | 13 | if (config.defaultValue) { |
12 | | - if (config.defaultValue.mode === 'basic') { |
13 | | - this.setupDefaultValue(screen, name, `this.mustache(${JSON.stringify(config.defaultValue.value)})`); |
14 | | - } else if (config.defaultValue.mode === 'js') { |
15 | | - this.setupDefaultValue(screen, name, `(function() {${config.defaultValue.value}}).bind(this.getDataReference())()`); |
| 14 | + if (config.defaultValue.mode === "basic") { |
| 15 | + this.setupDefaultValue( |
| 16 | + screen, |
| 17 | + name, |
| 18 | + `this.mustache(${JSON.stringify(config.defaultValue.value)})`, |
| 19 | + isCheckbox |
| 20 | + ); |
| 21 | + } else if (config.defaultValue.mode === "js") { |
| 22 | + this.setupDefaultValue( |
| 23 | + screen, |
| 24 | + name, |
| 25 | + `(function() {${config.defaultValue.value}}).bind(this.getDataReference())()`, |
| 26 | + isCheckbox |
| 27 | + ); |
16 | 28 | } |
17 | 29 | } |
18 | | - if ('initiallyChecked' in config) { |
19 | | - this.setupDefaultValue(screen, name, config.initiallyChecked ? 'true' : 'false'); |
| 30 | + if ("initiallyChecked" in config) { |
| 31 | + this.setupDefaultValue( |
| 32 | + screen, |
| 33 | + name, |
| 34 | + config.initiallyChecked ? "true" : "false", |
| 35 | + isCheckbox |
| 36 | + ); |
20 | 37 | } |
21 | 38 | // Update vdata |
22 | 39 | this.addMounted(screen, ` |
23 | 40 | this.setValue(${JSON.stringify(name)}, this.getValue(${JSON.stringify(name)}), this.vdata, this); |
24 | 41 | `); |
25 | 42 | }); |
26 | 43 | }, |
27 | | - setupDefaultValue(screen, name, value) { |
| 44 | + setupDefaultValue(screen, name, value, isCheckbox = false) { |
28 | 45 | const safeDotName = this.safeDotName(name); |
29 | 46 | const defaultComputedName = `default_${safeDotName}__`; |
30 | | - this.addData(screen, `${name}_was_filled__`, `this.getValue(${JSON.stringify(name)}, this.vdata) !== undefined || this.getValue(${JSON.stringify(name)}, data) !== undefined`); |
| 47 | + // For checkboxes, use explicit undefined checks to preserve false values |
| 48 | + // For other components, use falsy checks to maintain existing behavior |
| 49 | + const wasFilledCheck = isCheckbox |
| 50 | + ? `this.getValue(${JSON.stringify( |
| 51 | + name |
| 52 | + )}, this.vdata) !== undefined || this.getValue(${JSON.stringify( |
| 53 | + name |
| 54 | + )}, data) !== undefined` |
| 55 | + : `!!this.getValue(${JSON.stringify( |
| 56 | + name |
| 57 | + )}, this.vdata) || !!this.getValue(${JSON.stringify(name)}, data)`; |
| 58 | + const mountCheck = isCheckbox |
| 59 | + ? `this.${safeDotName} === undefined` |
| 60 | + : `!this.${safeDotName}`; |
| 61 | + this.addData(screen, `${name}_was_filled__`, wasFilledCheck); |
31 | 62 | this.addMounted( |
32 | 63 | screen, |
33 | | - `if (this.${safeDotName} === undefined) { |
| 64 | + `if (${mountCheck}) { |
34 | 65 | this.tryFormField(${JSON.stringify(name)}, () => { |
35 | 66 | this.${safeDotName} = ${value}; |
36 | 67 | this.setValue(${JSON.stringify(name)}, ${value}, this.vdata, this);}); |
37 | 68 | }` |
38 | 69 | ); |
39 | 70 | screen.computed[defaultComputedName] = { |
40 | | - get: new Function(`return this.tryFormField(${JSON.stringify(name)}, () => ${value});`), |
41 | | - set() {}, |
| 71 | + get: new Function( |
| 72 | + `return this.tryFormField(${JSON.stringify(name)}, () => ${value});` |
| 73 | + ), |
| 74 | + set() {} |
42 | 75 | }; |
43 | | - this.addWatch(screen, defaultComputedName, `!this.${safeDotName}_was_filled__ && this.setValue(${JSON.stringify(name)}, this.${defaultComputedName}, this.vdata, this);`); |
44 | | - }, |
| 76 | + this.addWatch( |
| 77 | + screen, |
| 78 | + defaultComputedName, |
| 79 | + `!this.${safeDotName}_was_filled__ && this.setValue(${JSON.stringify( |
| 80 | + name |
| 81 | + )}, this.${defaultComputedName}, this.vdata, this);` |
| 82 | + ); |
| 83 | + } |
45 | 84 | }, |
46 | 85 | mounted() { |
47 | 86 | this.extensions.push({ |
48 | 87 | onbuild({ screen, definition }) { |
49 | 88 | this.defaultValues(screen, definition); |
50 | 89 | }, |
51 | 90 | onloadproperties({ properties, element, definition }) { |
52 | | - const name = element.config.name; |
| 91 | + const { name } = element.config; |
53 | 92 | if (this.isComputedVariable(name, definition)) return; |
54 | 93 | if (element.config.defaultValue || element.config.initiallyChecked) { |
55 | 94 | const safeDotName = this.safeDotName(name); |
56 | | - const event = `${safeDotName}_was_filled__ |= !!$event; !${safeDotName}_was_filled__ && (vdata.${this.dot2bracket(name)} = default_${safeDotName}__)`; |
57 | | - this.addEvent(properties, 'input', event); |
| 95 | + const event = `${safeDotName}_was_filled__ |= !!$event; !${safeDotName}_was_filled__ && (vdata.${this.dot2bracket( |
| 96 | + name |
| 97 | + )} = default_${safeDotName}__)`; |
| 98 | + this.addEvent(properties, "input", event); |
58 | 99 | } |
59 | | - }, |
| 100 | + } |
60 | 101 | }); |
61 | | - }, |
| 102 | + } |
62 | 103 | }; |
0 commit comments