Skip to content

Commit

Permalink
fix(buttons): mark the form control of a ngbRadioGroup as touched
Browse files Browse the repository at this point in the history
fix #1987

Closes #1988
  • Loading branch information
jnizet authored and pkozlowski-opensource committed Nov 24, 2017
1 parent ae8753a commit 9dde9c2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/buttons/radio.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {By} from '@angular/platform-browser';
import {createGenericTestComponent} from '../test/common';

import {Component} from '@angular/core';
import {Validators, FormControl, FormGroup, FormsModule, ReactiveFormsModule} from '@angular/forms';
import {Validators, FormControl, FormGroup, FormsModule, ReactiveFormsModule, NgModel} from '@angular/forms';

import {NgbButtonsModule} from './buttons.module';

Expand Down Expand Up @@ -476,6 +476,31 @@ describe('ngbRadioGroup', () => {
expect(inputDebugEls[1].nativeElement.parentNode).toHaveCssClass('focus');
});

it('should mark form control as touched when label loses focus', () => {
const fixture = createTestComponent(`
<div [(ngModel)]="model" ngbRadioGroup>
<label ngbButtonLabel>
<input ngbButton type="radio" name="radio" [value]="values[0]"/> {{ values[0] }}
</label>
<label ngbButtonLabel>
<input ngbButton type="radio" name="radio" [value]="values[1]"/> {{ values[1] }}
</label>
</div>
`);
fixture.detectChanges();

const inputDebugEls = fixture.debugElement.queryAll(By.css('Input'));
const ngModel = fixture.debugElement.query(By.directive(NgModel)).injector.get(NgModel);

inputDebugEls[0].triggerEventHandler('focus', {});
fixture.detectChanges();
expect(ngModel.touched).toBe(false);

inputDebugEls[0].triggerEventHandler('blur', {});
fixture.detectChanges();
expect(ngModel.touched).toBe(true);
});

it('should generate input names automatically if no name specified anywhere', () => {
const fixture = createTestComponent(`
<div [(ngModel)]="model" ngbRadioGroup>
Expand Down
3 changes: 3 additions & 0 deletions src/buttons/radio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ export class NgbRadio implements OnDestroy {
if (this._label) {
this._label.focused = isFocused;
}
if (!isFocused) {
this._group.onTouched();
}
}

get checked() { return this._checked; }
Expand Down

3 comments on commit 9dde9c2

@pkozlowski-opensource
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jnizet I'm just looking at the demo site and it seems like this change is effecting
checkbox buttons in a wired way (I can see focus indicators in places where there is no / should be no focus)

@pkozlowski-opensource
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, actually I can see the same bug in Bootstrap: http://getbootstrap.com/docs/4.0/components/buttons/

@pkozlowski-opensource
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue on Bootstrap side: twbs/bootstrap#24760

Please sign in to comment.