Skip to content

Commit

Permalink
fix (buttons) radio buttons no change on every click (#2132)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemvfrolov authored and valorkin committed Jun 27, 2017
1 parent 325b510 commit 31ee792
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/buttons/button-radio.directive.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import {
Directive, ElementRef, HostBinding, forwardRef, HostListener, Input, OnInit, ChangeDetectorRef
Directive,
ElementRef,
HostBinding,
forwardRef,
HostListener,
Input,
OnInit,
ChangeDetectorRef
} from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';

Expand Down Expand Up @@ -41,12 +48,17 @@ export class ButtonRadioDirective implements ControlValueAccessor, OnInit {

if (this.uncheckable && this.btnRadio === this.value) {
this.value = undefined;
} else {
this.value = this.btnRadio;
this.onTouched();
this.onChange(this.value);
return;
}

this.onTouched();
this.onChange(this.value);
if (this.btnRadio !== this.value) {
this.value = this.btnRadio;
this.onTouched();
this.onChange(this.value);
return;
}
}

public constructor(el: ElementRef, private cdr: ChangeDetectorRef) {
Expand Down
15 changes: 15 additions & 0 deletions src/spec/button.directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,21 @@ describe('Directive: Buttons', () => {
expect(btn.children[3].classList).not.toContain('active');
}));

it('should not toggle when click in active button without uncheckable', () => {
fixture = createComponent(html, 'OnPush');
context = fixture.componentInstance;
element = fixture.nativeElement;

let btn = element.querySelector('.btn-group.radio');
expect(context.radioModel).toEqual('Middle');

(btn.children[1] as HTMLElement).click();
fixture.detectChanges();
expect(btn.children[0].classList).not.toContain('active');
expect(btn.children[1].classList).toContain('active');
expect(btn.children[2].classList).not.toContain('active');
});

it('should unset active class via click', () => {
fixture = createComponent(html);
context = fixture.componentInstance;
Expand Down

0 comments on commit 31ee792

Please sign in to comment.