Skip to content

Commit 3acf7d1

Browse files
authored
Merge branch '15.1.x' into bpachilova/fix-13392-15.1.x
2 parents 73218cf + da6abf0 commit 3acf7d1

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ All notable changes for each version of this project will be documented in this
3838
</igx-card-actions>
3939
</igx-card>
4040
```
41+
- `IgxButtonGroup`:
42+
- The `selected` and `deselected` events are now cancellable.
4143

4244
### General
4345
- `IgxPivotGrid`

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

+12-7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { IgxRippleModule } from '../directives/ripple/ripple.directive';
2323
import { IgxIconModule } from '../icon/public_api';
2424
import { takeUntil } from 'rxjs/operators';
2525
import { DisplayDensityBase, DisplayDensityToken, IDisplayDensityOptions } from '../core/density';
26-
import { IBaseEventArgs } from '../core/utils';
26+
import { CancelableEventArgs, IBaseEventArgs } from '../core/utils';
2727
import { mkenum } from '../core/utils';
2828

2929
/**
@@ -442,26 +442,31 @@ export class IgxButtonGroupComponent extends DisplayDensityBase implements After
442442
*/
443443
public _clickHandler(index: number) {
444444
const button = this.buttons[index];
445+
const args: IButtonGroupEventArgs = { cancel: false, button, index };
445446

446447
if (!this.multiSelection) {
447448
this.buttons.forEach((b, i) => {
448449
if (i !== index && this.selectedIndexes.indexOf(i) !== -1) {
449-
this.deselected.emit({ button: b, index: i });
450+
this.deselected.emit({ cancel: false, button: b, index: i });
450451
}
451452
});
452453
}
453454

454455
if (this.selectedIndexes.indexOf(index) === -1) {
455-
this.selectButton(index);
456-
this.selected.emit({ button, index });
456+
this.selected.emit(args);
457+
if(!args.cancel){
458+
this.selectButton(index);
459+
}
457460
} else {
458-
this.deselectButton(index);
459-
this.deselected.emit({ button, index });
461+
this.deselected.emit(args);
462+
if(!args.cancel){
463+
this.deselectButton(index);
464+
}
460465
}
461466
}
462467
}
463468

464-
export interface IButtonGroupEventArgs extends IBaseEventArgs {
469+
export interface IButtonGroupEventArgs extends IBaseEventArgs, CancelableEventArgs {
465470
button: IgxButtonDirective;
466471
index: number;
467472
}

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

+41
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,47 @@ describe('IgxButtonGroup', () => {
146146
expect(btnGroupInstance.deselected.emit).toHaveBeenCalled();
147147
});
148148

149+
it('should not select the button on click if event is canceled ', () => {
150+
const fixture = TestBed.createComponent(ButtonGroupWithSelectedButtonComponent);
151+
fixture.detectChanges();
152+
153+
const btnGroupInstance = fixture.componentInstance.buttonGroup;
154+
fixture.detectChanges();
155+
156+
btnGroupInstance.buttons[1].select();
157+
fixture.detectChanges();
158+
159+
btnGroupInstance.selected.subscribe((e) => {
160+
e.cancel = true;
161+
});
162+
fixture.detectChanges();
163+
164+
const button = fixture.debugElement.nativeElement.querySelector('button');
165+
button.click();
166+
fixture.detectChanges();
167+
168+
expect(btnGroupInstance.buttons[0].selected).toBe(false);
169+
});
170+
171+
it('should not deselect the button on click if event is canceled ', () => {
172+
const fixture = TestBed.createComponent(ButtonGroupWithSelectedButtonComponent);
173+
fixture.detectChanges();
174+
175+
const btnGroupInstance = fixture.componentInstance.buttonGroup;
176+
fixture.detectChanges();
177+
178+
btnGroupInstance.deselected.subscribe((e) => {
179+
e.cancel = true;
180+
});
181+
fixture.detectChanges();
182+
183+
const button = fixture.debugElement.nativeElement.querySelector('button');
184+
button.click();
185+
fixture.detectChanges();
186+
187+
expect(btnGroupInstance.buttons[0].selected).toBe(true);
188+
});
189+
149190
it('Button Group single selection', () => {
150191
const fixture = TestBed.createComponent(InitButtonGroupComponent);
151192
fixture.detectChanges();

0 commit comments

Comments
 (0)