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 all commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
- Button group component now allows resetting the selection state via the `selectedItems` property [#1168](https://github.com/IgniteUI/igniteui-webcomponents/pull/1168)

## [4.8.2] - 2024-04-15
### Fixed
- Textarea - resize handle position for non-suffixed textarea [#1094](https://github.com/IgniteUI/igniteui-webcomponents/issues/1094)
Expand Down
28 changes: 28 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,34 @@ describe('Button Group', () => {
expect(buttonGroup.selectedItems.length).to.equal(0);
});

it('should clear the selection when passing an empty array to `selectedItems` property', () => {
buttons[0].selected = true;

expect(buttonGroup.selectedItems.length).to.equal(1);
expect(buttonGroup.selectedItems).to.have.same.members([
buttons[0].value,
]);

buttonGroup.selectedItems = [];

expect(buttonGroup.selectedItems.length).to.equal(0);
expect(buttons[0].selected).to.be.false;
});

it('should clear the selection when passing falsy values to `selectedItems` property', () => {
buttons[0].selected = true;

expect(buttonGroup.selectedItems.length).to.equal(1);
expect(buttonGroup.selectedItems).to.have.same.members([
buttons[0].value,
]);

buttonGroup.selectedItems = null as unknown as string[];

expect(buttonGroup.selectedItems.length).to.equal(0);
expect(buttons[0].selected).to.be.false;
});

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

public set selectedItems(values: string[]) {
if (values && values.length) {
this._selectedItems = new Set(values);
this.setSelection(this._selectedItems);
}
this._selectedItems = new Set(Array.isArray(values) ? values : []);
this.setSelection(this._selectedItems);
}

@watch('disabled', { waitUntilFirstUpdate: true })
Expand Down Expand Up @@ -205,6 +203,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