discourse/app/assets/javascripts/wizard/addon/components/fields/checkboxes.js
David Taylor e4c373194d
DEV: Refactor Wizard components (#24770)
This commit refactors the Wizard component code in preparation for moving it to the 'static' directory for Embroider route-splitting. It also includes a number of general improvements and simplifications.

Extracted from https://github.com/discourse/discourse/pull/23678

Co-authored-by: Godfrey Chan <godfreykfc@gmail.com>
2023-12-07 16:33:38 +00:00

34 lines
819 B
JavaScript

import Component from "@ember/component";
import { action, set } from "@ember/object";
export default Component.extend({
init(...args) {
this._super(...args);
this.set("field.value", this.field.value || []);
for (let choice of this.field.choices) {
if (this.field.value.includes(choice.id)) {
set(choice, "checked", true);
}
}
},
@action
changed(checkbox) {
let newFieldValue = this.field.value;
const checkboxValue = checkbox.parentElement
.getAttribute("value")
.toLowerCase();
if (checkbox.checked) {
newFieldValue.push(checkboxValue);
} else {
const index = newFieldValue.indexOf(checkboxValue);
if (index > -1) {
newFieldValue.splice(index, 1);
}
}
this.set("field.value", newFieldValue);
},
});