Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rating): fixed model change by clicking on disabled rating element #3574

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/rating/rating.spec.ts
Expand Up @@ -450,6 +450,19 @@ describe('ngb-rating', () => {
});
});

it('should set tabindex to -1 when disabled', () => {
const fixture = createTestComponent('<ngb-rating></ngb-rating>');
let ratingEl = fixture.debugElement.query(By.directive(NgbRating));
let ratingComp = <NgbRating>ratingEl.componentInstance;

fixture.detectChanges();
expect(ratingEl.nativeElement.getAttribute('tabindex')).toEqual('0');

ratingComp.disabled = true;
fixture.detectChanges();
expect(ratingEl.nativeElement.getAttribute('tabindex')).toEqual('-1');
});

describe('keyboard support', () => {

it('should handle arrow keys', () => {
Expand Down Expand Up @@ -582,6 +595,40 @@ describe('ngb-rating', () => {
expect(element.nativeElement).toHaveCssClass('ng-untouched');
});

it('should not update template driven form by clicking disabled control', async(() => {
const html = `
<ngb-rating [(ngModel)]="model" class="control" max="5"></ngb-rating>
<ngb-rating [(ngModel)]="model" class="control-disabled" max="5" disabled></ngb-rating>`;

const fixture = createTestComponent(html);
const element = fixture.debugElement.query(By.css('.control'));
const disabledElement = fixture.debugElement.query(By.css('.control-disabled'));

fixture.detectChanges();
fixture.whenStable()
.then(() => {
getStar(element.nativeElement, 3).click();

fixture.detectChanges();
return fixture.whenStable();
})
.then(() => {
expect(getState(element.nativeElement)).toEqual([true, true, true, false, false]);
expect(getState(disabledElement.nativeElement)).toEqual([false, false, false, false, false]);
expect(fixture.componentInstance.model).toEqual(3);

getStar(disabledElement.nativeElement, 4).click();
fixture.detectChanges();
return fixture.whenStable();
})
.then(() => {
fixture.detectChanges();
expect(getState(element.nativeElement)).toEqual([true, true, true, false, false]);
expect(getState(disabledElement.nativeElement)).toEqual([false, false, false, false, false]);
expect(fixture.componentInstance.model).toEqual(3);
});
}));

it('should handle clicks and update form control', () => {
const html = `
<form [formGroup]="form">
Expand Down
8 changes: 6 additions & 2 deletions src/rating/rating.ts
Expand Up @@ -46,7 +46,7 @@ const NGB_RATING_VALUE_ACCESSOR = {
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
'class': 'd-inline-flex',
'tabindex': '0',
'[tabindex]': 'disabled ? -1 : 0',
'role': 'slider',
'aria-valuemin': '0',
'[attr.aria-valuemax]': 'max',
Expand Down Expand Up @@ -144,7 +144,11 @@ export class NgbRating implements ControlValueAccessor,

handleBlur() { this.onTouched(); }

handleClick(value: number) { this.update(this.resettable && this.rate === value ? 0 : value); }
handleClick(value: number) {
if (!this.readonly && !this.disabled) {
this.update(this.resettable && this.rate === value ? 0 : value);
}
}

handleKeyDown(event: KeyboardEvent) {
// tslint:disable-next-line:deprecation
Expand Down