Skip to content

Commit 829e298

Browse files
committed
Add checks for checkbox component
1 parent 735ab9c commit 829e298

File tree

2 files changed

+67
-22
lines changed

2 files changed

+67
-22
lines changed

src/mixins/extensions/DataManager.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ export default {
88
const { component } = v.element;
99
const dataFormat = v.config.dataFormat || null;
1010
const safeDotName = this.safeDotName(v.name);
11+
// For checkboxes, use explicit undefined checks to preserve false values
12+
const isCheckbox = component === "FormCheckbox";
13+
const vdataCheck = isCheckbox ? "vdataVal !== undefined" : "vdataVal";
14+
const dataCheck = isCheckbox ? "dataVal !== undefined" : "dataVal";
1115
this.addData(
1216
screen,
1317
safeDotName,
@@ -16,9 +20,9 @@ export default {
1620
const vdataVal = this.getValue(${JSON.stringify(
1721
v.name
1822
)}, this.vdata);
19-
if (vdataVal !== undefined) return vdataVal;
23+
if (${vdataCheck}) return vdataVal;
2024
const dataVal = this.getValue(${JSON.stringify(v.name)}, data);
21-
if (dataVal !== undefined) return dataVal;
25+
if (${dataCheck}) return dataVal;
2226
return this.initialValue('${component}', '${dataFormat}', ${JSON.stringify(
2327
v.config
2428
)});
Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,103 @@
1-
21
export default {
32
methods: {
43
/**
54
* Prepare `data` configuration for the Vue Screen Component
65
*
76
*/
87
defaultValues(screen, definition) {
9-
this.variables.forEach(({name, config}) => {
8+
this.variables.forEach((v) => {
9+
const { name, config } = v;
10+
const { component } = v.element;
1011
if (this.isComputedVariable(name, definition)) return;
12+
const isCheckbox = component === "FormCheckbox";
1113
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+
);
1628
}
1729
}
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+
);
2037
}
2138
// Update vdata
2239
this.addMounted(screen, `
2340
this.setValue(${JSON.stringify(name)}, this.getValue(${JSON.stringify(name)}), this.vdata, this);
2441
`);
2542
});
2643
},
27-
setupDefaultValue(screen, name, value) {
44+
setupDefaultValue(screen, name, value, isCheckbox = false) {
2845
const safeDotName = this.safeDotName(name);
2946
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);
3162
this.addMounted(
3263
screen,
33-
`if (this.${safeDotName} === undefined) {
64+
`if (${mountCheck}) {
3465
this.tryFormField(${JSON.stringify(name)}, () => {
3566
this.${safeDotName} = ${value};
3667
this.setValue(${JSON.stringify(name)}, ${value}, this.vdata, this);});
3768
}`
3869
);
3970
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() {}
4275
};
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+
}
4584
},
4685
mounted() {
4786
this.extensions.push({
4887
onbuild({ screen, definition }) {
4988
this.defaultValues(screen, definition);
5089
},
5190
onloadproperties({ properties, element, definition }) {
52-
const name = element.config.name;
91+
const { name } = element.config;
5392
if (this.isComputedVariable(name, definition)) return;
5493
if (element.config.defaultValue || element.config.initiallyChecked) {
5594
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);
5899
}
59-
},
100+
}
60101
});
61-
},
102+
}
62103
};

0 commit comments

Comments
 (0)