Skip to content

Commit

Permalink
Fix <sl-checkbox-group> value not reflecting initial checked state (
Browse files Browse the repository at this point in the history
  • Loading branch information
jpzwarte authored Jul 17, 2024
1 parent 047889e commit 5051056
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/dull-lemons-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sl-design-system/checkbox': patch
---

Fix group value not reflecting initial checked state
22 changes: 18 additions & 4 deletions packages/components/checkbox/src/checkbox-group.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ describe('sl-checkbox-group', () => {
`);
});

it('should render correctly', () => {
expect(el).shadowDom.to.equalSnapshot();
});

it('should not be disabled', () => {
expect(el.disabled).to.not.be.true;
expect(el).not.to.have.attribute('disabled');
Expand Down Expand Up @@ -259,6 +255,24 @@ describe('sl-checkbox-group', () => {
});
});

describe('implicit value', () => {
let el: CheckboxGroup;

beforeEach(async () => {
el = await fixture(html`
<sl-checkbox-group>
<sl-checkbox checked value="0">Option 1</sl-checkbox>
<sl-checkbox checked value="1">Option 2</sl-checkbox>
<sl-checkbox value="2">Option 3</sl-checkbox>
</sl-checkbox-group>
`);
});

it('should have a value of the checked boxes', () => {
expect(el.value).to.deep.equal(['0', '1', null]);
});
});

describe('without values', () => {
let el: CheckboxGroup;

Expand Down
12 changes: 12 additions & 0 deletions packages/components/checkbox/src/checkbox-group.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ export const Value: Story = {
}
};

export const ImplicitValue: Story = {
args: {
slot: () => html`
<sl-checkbox-group>
<sl-checkbox checked value="0">Option 1</sl-checkbox>
<sl-checkbox checked value="1">Option 2</sl-checkbox>
<sl-checkbox value="2">Option 3</sl-checkbox>
</sl-checkbox-group>
`
}
};

export const WithoutValues: Story = {
args: {
boxes: () => html`
Expand Down
15 changes: 9 additions & 6 deletions packages/components/checkbox/src/checkbox-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export class CheckboxGroup<T = unknown> extends FormControlMixin(LitElement) {
override render(): TemplateResult {
return html`
<slot
@slotchange=${this.#onSlotchange}
@slotchange=${this.#onSlotChange}
@sl-blur=${this.#stopEvent}
@sl-change=${this.#stopEvent}
@sl-focus=${this.#stopEvent}
Expand Down Expand Up @@ -182,17 +182,20 @@ export class CheckboxGroup<T = unknown> extends FormControlMixin(LitElement) {
event.stopPropagation();
}

#onSlotchange(): void {
#onSlotChange(): void {
this.#rovingTabindexController.clearElementCache();

this.#observer.disconnect();

this.boxes?.forEach((box, index) => {
box.name = this.name;
if (box.value !== undefined) {
box.checked = this.value?.includes(box.value) ?? false;
} else {
box.formValue = this.value?.at(index) ?? null;

if (this.value !== undefined) {
if (box.value !== undefined) {
box.checked = this.value?.includes(box.value) ?? false;
} else {
box.formValue = this.value?.at(index) ?? null;
}
}

if (typeof this.disabled === 'boolean') {
Expand Down

0 comments on commit 5051056

Please sign in to comment.