Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(button-group): clear selection via selectedItems #1168

Merged
merged 4 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/components/button-group/button-group.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,24 @@ describe('Button Group', () => {
expect(buttonGroup.selectedItems.length).to.equal(0);
});

it('should clear the selection when passing an empty array to `selectedItems` property', async () => {
buttonGroup = await createButtonGroupComponent(`
<igc-button-group selection="multiple">
<igc-toggle-button value="left">Left</igc-toggle-button>
<igc-toggle-button value="center">Center</igc-toggle-button>
<igc-toggle-button value="right" selected>Right</igc-toggle-button>
<igc-toggle-button value="top" selected>Top</igc-toggle-button>
</igc-button-group>`);

expect(buttonGroup.selection).to.equal('multiple');
expect(buttonGroup.selectedItems.length).to.equal(2);

buttonGroup.selectedItems = [];
await elementUpdated(buttonGroup);

expect(buttonGroup.selectedItems.length).to.equal(0);
});

it('initial selection through child selection attribute has higher priority', async () => {
// single mode
buttonGroup = await createButtonGroupComponent(`
Expand Down
7 changes: 6 additions & 1 deletion src/components/button-group/button-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export default class IgcButtonGroupComponent extends EventEmitterMixin<
}

public set selectedItems(values: string[]) {
if (values && values.length) {
if (values) {
this._selectedItems = new Set(values);
this.setSelection(this._selectedItems);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (values) {
this._selectedItems = new Set(values);
this.setSelection(this._selectedItems);
}
this._selectedItems = new Set(Array.isArray(values) ? values : []);
this.setSelection(this._selectedItems);

This would also work when assigning falsy values, i.e. null/undefined.
You can add a test case for that as well.

Expand Down Expand Up @@ -205,6 +205,11 @@ export default class IgcButtonGroupComponent extends EventEmitterMixin<
}

private setSelection(values: Set<string>) {
if (!values.size) {
this.toggleButtons.forEach((b) => (b.selected = false));
return;
}

for (const button of this.toggleButtons) {
if (values.has(button.value)) {
button.selected = true;
Expand Down
Loading