Skip to content

Commit 091831e

Browse files
Jonas Metzeneranehx
Jonas Metzener
authored andcommitted
fix(form): fix wrong updating of the answer value
1 parent 3881d3c commit 091831e

File tree

3 files changed

+11
-14
lines changed

3 files changed

+11
-14
lines changed

addon/components/cf-field.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ export default Component.extend({
3333
"config:environment"
3434
);
3535

36-
this.set("field.answer.value", value);
37-
3836
/* istanbul ignore next */
3937
if (environment !== "test") {
4038
yield timeout(500);
4139
}
4240

41+
this.set("field.answer.value", value);
42+
4343
yield this.field.validate.perform();
4444

4545
try {
+8-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import Component from "@ember/component";
2-
import { get } from "@ember/object";
32
import layout from "../../../templates/components/cf-field/input/checkbox";
43
import { computed } from "@ember/object";
54

@@ -11,7 +10,6 @@ import { computed } from "@ember/object";
1110
*/
1211
export default Component.extend({
1312
layout,
14-
tagName: "",
1513

1614
choices: computed(
1715
"field.question.{multipleChoiceOptions,dynamicMultipleChoiceOptions}.edges",
@@ -25,18 +23,17 @@ export default Component.extend({
2523

2624
actions: {
2725
/**
28-
* Toggle the checked state of an option and trigger saving the field.
26+
* Update the value of the field with the slugs of the currently checked
27+
* boxes.
2928
*
30-
* @method toggle
31-
* @param {String} slug The slug of the changed option
32-
* @param {Boolean} checked Whether the options checkbox is checked or not
29+
* @method update
3330
*/
34-
toggle(slug, checked) {
35-
const value = get(this, "field.answer.value") || [];
31+
update() {
32+
const checkedBoxes = [
33+
...this.element.querySelectorAll("input[type=checkbox]:checked")
34+
];
3635

37-
this.onSave([
38-
...new Set([...value, slug].filter(v => v !== slug || checked))
39-
]);
36+
this.onSave([...new Set(checkedBoxes.map(el => el.value))]);
4037
}
4138
}
4239
});

addon/templates/components/cf-field/input/checkbox.hbs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
value={{option.slug}}
1010
checked={{contains option.slug field.answer.value}}
1111
disabled={{disabled}}
12-
onchange={{action (action "toggle" option.slug) value="target.checked"}}
12+
onchange={{action "update"}}
1313
>
1414
{{option.label}}
1515
</label>

0 commit comments

Comments
 (0)