Skip to content

Commit

Permalink
fix(checkbox-group): fix multiple selection in checkbox group
Browse files Browse the repository at this point in the history
  • Loading branch information
anehx committed Sep 20, 2022
1 parent 1722144 commit 6bb902b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
11 changes: 3 additions & 8 deletions addon/components/validated-input/types/checkbox-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@ export default class CheckboxGroupComponent extends Component {
onUpdate(key, event) {
event.preventDefault();

const value = this.value || [];
const index = value.indexOf(key);
const value = new Set(this.args.value || []);

if (index > -1) {
value.splice(index, 1);
} else {
value.push(key);
}
value.delete(key) || value.add(key);

this.args.update(value);
this.args.update([...value]);
this.args.setDirty();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render } from "@ember/test-helpers";
import { click, render } from "@ember/test-helpers";
import { setupRenderingTest } from "ember-qunit";
import hbs from "htmlbars-inline-precompile";
import { module } from "qunit";
Expand Down Expand Up @@ -30,6 +30,29 @@ module(
assert.dom("input[type=checkbox]").exists({ count: 2 });
});

testDefault("it can select multiple values", async function (assert) {
this.options = [
{ key: 1, label: 1 },
{ key: 2, label: 2 },
];
this.value = [];

await render(hbs`
<ValidatedInput::Types::CheckboxGroup
@options={{this.options}}
@value={{this.value}}
@update={{fn (mut this.value)}}
@setDirty={{fn (mut this.dirty) true}}
/>
`);

await click('input[value="1"]');
await click('input[value="2"]');

assert.deepEqual(this.value, [1, 2]);
assert.true(this.dirty);
});

testUikit("it renders", async function (assert) {
this.set("options", [
{
Expand Down

0 comments on commit 6bb902b

Please sign in to comment.