Skip to content

Commit 7934c21

Browse files
fix(button-group): prevent selected event from emitting twice on click (#14455)
1 parent a79006e commit 7934c21

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

projects/igniteui-angular/src/lib/buttonGroup/buttonGroup.component.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -535,9 +535,8 @@ export class IgxButtonGroupComponent extends DisplayDensityBase implements After
535535
if (updatedButtons.length > 0) {
536536
updatedButtons.forEach((button) => {
537537
const index = this.buttons.map((b) => b.nativeElement).indexOf(button);
538-
const args: IButtonGroupEventArgs = { owner: this, button: this.buttons[index], index };
539538

540-
this.updateButtonSelectionState(index, args);
539+
this.updateButtonSelectionState(index);
541540
});
542541
}
543542

@@ -562,13 +561,11 @@ export class IgxButtonGroupComponent extends DisplayDensityBase implements After
562561
return updated;
563562
}
564563

565-
private updateButtonSelectionState(index: number, args: IButtonGroupEventArgs) {
564+
private updateButtonSelectionState(index: number) {
566565
if (this.buttons[index].selected) {
567566
this.updateSelected(index);
568-
this.selected.emit(args);
569567
} else {
570568
this.updateDeselected(index);
571-
this.deselected.emit(args);
572569
}
573570
}
574571
}

projects/igniteui-angular/src/lib/buttonGroup/buttongroup.component.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,32 @@ describe('IgxButtonGroup', () => {
465465
expect(buttonGroup.buttons[0].nativeElement.classList.contains('igx-button-group__item--selected')).toBe(false);
466466
});
467467

468+
it('should emit selected event only once per selection', async() => {
469+
const fixture = TestBed.createComponent(InitButtonGroupComponent);
470+
fixture.detectChanges();
471+
await wait();
472+
473+
const buttonGroup = fixture.componentInstance.buttonGroup;
474+
475+
spyOn(buttonGroup.selected, 'emit').and.callThrough();
476+
477+
buttonGroup.selectButton(0);
478+
await wait();
479+
fixture.detectChanges();
480+
481+
const buttons = fixture.nativeElement.querySelectorAll('button');
482+
buttons[1].click();
483+
await wait();
484+
fixture.detectChanges();
485+
486+
expect(buttonGroup.selected.emit).toHaveBeenCalledTimes(1);
487+
488+
buttons[0].click();
489+
await wait();
490+
fixture.detectChanges();
491+
492+
expect(buttonGroup.selected.emit).toHaveBeenCalledTimes(2);
493+
});
468494
});
469495

470496
@Component({

0 commit comments

Comments
 (0)