Skip to content

Commit

Permalink
fix(buttons): proerly handle disabled propery on a button level
Browse files Browse the repository at this point in the history
Fixes #1088
Fixes #1089
Closes #1095
Closes #1141
  • Loading branch information
pkozlowski-opensource committed Dec 15, 2016
1 parent 186c0e1 commit cc88ed2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
31 changes: 30 additions & 1 deletion src/buttons/radio.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,36 @@ describe('ngbRadioGroup', () => {
});
}));

it('should disable individual label and input using template-driven forms', async(() => {
const html = `
<form>
<div ngbRadioGroup [(ngModel)]="model" name="control">
<label class="btn">
<input type="radio" value="foo" [disabled]="disabled"/>
</label>
</div>
</form>`;

const fixture = createTestComponent(html);

fixture.whenStable()
.then(() => {
fixture.componentInstance.disabled = true;
fixture.detectChanges();
expect(getLabel(fixture.nativeElement, 0)).toHaveCssClass('disabled');
expect(getInput(fixture.nativeElement, 0).hasAttribute('disabled')).toBeTruthy();

fixture.componentInstance.disabled = false;
fixture.detectChanges();
return fixture.whenStable();
})
.then(() => {
fixture.detectChanges();
expect(getLabel(fixture.nativeElement, 0)).not.toHaveCssClass('disabled');
expect(getInput(fixture.nativeElement, 0).hasAttribute('disabled')).toBeFalsy();
});
}));

it('should select a radio button when checked attribute is used', () => {
const html1 = `
<input type="radio" name="State" value="0" /> Foo <br>
Expand Down Expand Up @@ -419,7 +449,6 @@ describe('ngbRadioGroup', () => {

expect(getInput(fixture.nativeElement, 0).disabled).toBeTruthy();
expect(getInput(fixture.nativeElement, 1).disabled).toBeFalsy();

});

it('handle multiple cases for binded checked attribute.', () => {
Expand Down
28 changes: 15 additions & 13 deletions src/buttons/radio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const NGB_RADIO_VALUE_ACCESSOR = {
providers: [NGB_RADIO_VALUE_ACCESSOR]
})
export class NgbRadioGroup implements ControlValueAccessor {
private _disabled: boolean;
private _radios: Set<NgbRadio> = new Set<NgbRadio>();
private _value = null;

Expand All @@ -29,27 +28,25 @@ export class NgbRadioGroup implements ControlValueAccessor {
this.onChange(radio.value);
}

onRadioValueUpdate() { this._updateRadios(); }
onRadioValueUpdate() { this._updateRadiosValue(); }

register(radio: NgbRadio) { this._radios.add(radio); }

registerOnChange(fn: (value: any) => any): void { this.onChange = fn; }

registerOnTouched(fn: () => any): void { this.onTouched = fn; }

setDisabledState(isDisabled: boolean): void {
this._disabled = isDisabled;
this._updateRadios();
}
setDisabledState(isDisabled: boolean): void { this._updateRadiosDisabled(isDisabled); }

unregister(radio: NgbRadio) { this._radios.delete(radio); }

writeValue(value) {
this._value = value;
this._updateRadios();
this._updateRadiosValue();
}

private _updateRadios() { this._radios.forEach((radio) => radio.update(this._value, this._disabled)); }
private _updateRadiosValue() { this._radios.forEach((radio) => radio.updateValue(this._value)); }
private _updateRadiosDisabled(isDisabled) { this._radios.forEach((radio) => radio.updateDisabled(isDisabled)); }
}


Expand Down Expand Up @@ -103,8 +100,8 @@ export class NgbRadio implements OnDestroy {
}

@Input('disabled')
set disabled(value: any) {
this._disabled = this._element.nativeElement.hasAttribute('disabled') ? true : value;
set disabled(isDisabled: any) {
this.updateDisabled(isDisabled !== false);
}

set focused(isFocused: boolean) {
Expand Down Expand Up @@ -139,10 +136,15 @@ export class NgbRadio implements OnDestroy {
}
}

update(value, isDisabled) {
updateValue(value) {
this._checked = (this.value === value && value !== null);
this._disabled = isDisabled;
this._label.active = this._checked;
this._label.disabled = this._disabled;
}

updateDisabled(isDisabled) {
this._disabled = isDisabled;
if (this._label) {
this._label.disabled = this._disabled;
}
}
}

0 comments on commit cc88ed2

Please sign in to comment.