From 4b8a4302ff92f4f894184678d055c043f471ec77 Mon Sep 17 00:00:00 2001 From: Kate <43079840+katebatura@users.noreply.github.com> Date: Thu, 16 Sep 2021 14:31:10 +0300 Subject: [PATCH] fix(button group): prevent unpress in single mode button group (#2860) --- .../components/button-group/button-group.component.ts | 6 +++++- .../components/button-group/button-toggle.directive.ts | 8 +++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/framework/theme/components/button-group/button-group.component.ts b/src/framework/theme/components/button-group/button-group.component.ts index f40ea8ecbc..db12ebafb6 100644 --- a/src/framework/theme/components/button-group/button-group.component.ts +++ b/src/framework/theme/components/button-group/button-group.component.ts @@ -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'; /** * `` visually groups buttons together and allow to control buttons properties and the state as a diff --git a/src/framework/theme/components/button-group/button-toggle.directive.ts b/src/framework/theme/components/button-group/button-toggle.directive.ts index 7854156acc..5ce453448d 100644 --- a/src/framework/theme/components/button-group/button-toggle.directive.ts +++ b/src/framework/theme/components/button-group/button-toggle.directive.ts @@ -13,6 +13,7 @@ import { HostListener, Input, NgZone, + Optional, Output, Renderer2, } from '@angular/core'; @@ -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; @@ -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( @@ -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); }