Skip to content

Commit

Permalink
fix(button group): prevent unpress in single mode button group (#2860)
Browse files Browse the repository at this point in the history
  • Loading branch information
katebatura authored Sep 16, 2021
1 parent 26e54a2 commit 4b8a430
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ import { NbComponentSize } from '../component-size';
import { NbComponentShape } from '../component-shape';
import { NbComponentOrCustomStatus } from '../component-status';
import { NbButton } from '../button/base-button';
import { NbButtonToggleAppearance, NbButtonToggleChange, NbButtonToggleDirective } from './button-toggle.directive';
import {
NbButtonToggleAppearance,
NbButtonToggleChange,
NbButtonToggleDirective,
} from './button-toggle.directive';

/**
* `<nb-button-group>` visually groups buttons together and allow to control buttons properties and the state as a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
HostListener,
Input,
NgZone,
Optional,
Output,
Renderer2,
} from '@angular/core';
Expand All @@ -21,6 +22,7 @@ import { Observable, Subject } from 'rxjs';
import { NbStatusService } from '../../services/status.service';
import { convertToBoolProperty, NbBooleanInput } from '../helpers';
import { NbButton, NbButtonAppearance } from '../button/base-button';
import { NbButtonGroupComponent } from './button-group.component';

export type NbButtonToggleAppearance = Exclude<NbButtonAppearance, 'hero'>;

Expand Down Expand Up @@ -123,7 +125,10 @@ export class NbButtonToggleDirective extends NbButton {

@HostListener('click')
onClick(): void {
this.pressed = !this.pressed;
// Don't remove the pressed state of the button in single-toggle button-groups
if (this.buttonGroup?.multiple || !this.pressed) {
this.pressed = !this.pressed;
}
}

constructor(
Expand All @@ -132,6 +137,7 @@ export class NbButtonToggleDirective extends NbButton {
protected cd: ChangeDetectorRef,
protected zone: NgZone,
protected statusService: NbStatusService,
@Optional() protected buttonGroup?: NbButtonGroupComponent,
) {
super(renderer, hostElement, cd, zone, statusService);
}
Expand Down

0 comments on commit 4b8a430

Please sign in to comment.