Skip to content

Commit

Permalink
fix(radio): initialiase disabled state according to parent's state
Browse files Browse the repository at this point in the history
Fixes #2635
Closes #2639
  • Loading branch information
Benoit Charbonnier authored and pkozlowski-opensource committed Aug 24, 2018
1 parent c0e7d53 commit 9b19171
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
31 changes: 26 additions & 5 deletions src/buttons/radio.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {TestBed, ComponentFixture, async} from '@angular/core/testing';
import {By} from '@angular/platform-browser';
import {createGenericTestComponent} from '../test/common';

import {Component} from '@angular/core';
import {Validators, FormControl, FormGroup, FormsModule, ReactiveFormsModule, NgModel} from '@angular/forms';
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {FormControl, FormGroup, FormsModule, NgModel, ReactiveFormsModule, Validators} from '@angular/forms';
import {By} from '@angular/platform-browser';

import {createGenericTestComponent} from '../test/common';
import {NgbButtonsModule} from './buttons.module';

const createTestComponent = (html: string) =>
Expand Down Expand Up @@ -324,6 +323,28 @@ describe('ngbRadioGroup', () => {
expect(getInput(fixture.nativeElement, 0).hasAttribute('disabled')).toBeFalsy();
});

it('should disable label and input when added dynamically in reactive forms', () => {
const forHtml = `
<form [formGroup]="disabledForm">
<div ngbRadioGroup formControlName="control">
<label ngbButtonLabel *ngIf="shown">
<input ngbButton type="radio" name="radio" [value]="'one'"/> One
</label>
</div>
</form>
`;

const fixture = createTestComponent(forHtml);
fixture.componentInstance.shown = false;
fixture.componentInstance.disabledForm.disable();
fixture.detectChanges();

fixture.componentInstance.shown = true;
fixture.detectChanges();
expect(getLabel(fixture.nativeElement, 0)).toHaveCssClass('disabled');
expect(getInput(fixture.nativeElement, 0).hasAttribute('disabled')).toBeTruthy();
});

it('should disable label and input when it is disabled using template-driven forms', async(() => {
const html = `
<form>
Expand Down
3 changes: 2 additions & 1 deletion src/buttons/radio.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Directive, forwardRef, Input, Renderer2, ElementRef, OnDestroy} from '@angular/core';
import {Directive, ElementRef, forwardRef, Input, OnDestroy, Renderer2} from '@angular/core';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';

import {NgbButtonLabel} from './label';
Expand Down Expand Up @@ -129,6 +129,7 @@ export class NgbRadio implements OnDestroy {
private _group: NgbRadioGroup, private _label: NgbButtonLabel, private _renderer: Renderer2,
private _element: ElementRef<HTMLInputElement>) {
this._group.register(this);
this.updateDisabled();
}

ngOnDestroy() { this._group.unregister(this); }
Expand Down

0 comments on commit 9b19171

Please sign in to comment.