Skip to content

Commit

Permalink
feat(datepicker): add aria attributes for navigation
Browse files Browse the repository at this point in the history
Closes #2345
  • Loading branch information
maxokorokov authored and pkozlowski-opensource committed Jun 1, 2018
1 parent f266b7f commit cd01d32
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
15 changes: 12 additions & 3 deletions src/datepicker/datepicker-navigation-select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import {NgbDate} from './ngb-date';
const createTestComponent = (html: string) =>
createGenericTestComponent(html, TestComponent) as ComponentFixture<TestComponent>;

function getOptionValues(element: HTMLSelectElement): string[] {
return Array.from(element.options).map(x => (x as HTMLOptionElement).value);
}
const getOptions = (element: HTMLSelectElement): HTMLOptionElement[] => Array.from(element.options);
const getOptionValues = (element: HTMLSelectElement): string[] => getOptions(element).map(x => x.value);

function changeSelect(element: HTMLSelectElement, value: string) {
element.value = value;
Expand Down Expand Up @@ -102,12 +101,22 @@ describe('ngb-datepicker-navigation-select', () => {
expect(getYearSelect(fixture.nativeElement).disabled).toBe(true);
});

it('should have correct aria attributes on select boxes', () => {
const fixture =
createTestComponent(`<ngb-datepicker-navigation-select [date]="date" [months]="[7, 8, 9]" [years]="years">`);

getOptions(getMonthSelect(fixture.nativeElement)).forEach((option, index) => {
expect(option.getAttribute('aria-label')).toBe(fixture.componentInstance.ariaMonths[index]);
});
});

});

@Component({selector: 'test-cmp', template: ''})
class TestComponent {
date = new NgbDate(2016, 8, 22);
months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
ariaMonths = ['July', 'August', 'September'];
years = [2015, 2016, 2017];

onSelect = () => {};
Expand Down
2 changes: 1 addition & 1 deletion src/datepicker/datepicker-navigation-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {NgbDatepickerI18n} from './datepicker-i18n';
class="custom-select"
[value]="date?.month"
(change)="changeMonth($event.target.value)">
<option *ngFor="let m of months" [value]="m">{{ i18n.getMonthShortName(m) }}</option>
<option *ngFor="let m of months" [attr.aria-label]="i18n.getMonthFullName(m)" [value]="m">{{ i18n.getMonthShortName(m) }}</option>
</select><select
[disabled]="disabled"
class="custom-select"
Expand Down
10 changes: 10 additions & 0 deletions src/datepicker/datepicker-navigation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ describe('ngb-datepicker-navigation', () => {
links.forEach((link) => { expect(link.getAttribute('type')).toBe('button'); });
});

it('should have correct titles and aria attributes on buttons', () => {
const fixture = createTestComponent(`<ngb-datepicker-navigation></ngb-datepicker-navigation>`);

const links = getNavigationLinks(fixture.nativeElement);
expect(links[0].getAttribute('aria-label')).toBe('Previous month');
expect(links[1].getAttribute('aria-label')).toBe('Next month');
expect(links[0].getAttribute('title')).toBe('Previous month');
expect(links[1].getAttribute('title')).toBe('Next month');
});

});

@Component({selector: 'test-cmp', template: ''})
Expand Down
10 changes: 6 additions & 4 deletions src/datepicker/datepicker-navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ import {NgbDatepickerI18n} from './datepicker-i18n';
`],
template: `
<div class="ngb-dp-arrow">
<button type="button" class="btn btn-link ngb-dp-arrow-btn"
(click)="!!navigate.emit(navigation.PREV)" [disabled]="prevDisabled">
<button type="button" class="btn btn-link ngb-dp-arrow-btn" (click)="!!navigate.emit(navigation.PREV)" [disabled]="prevDisabled"
i18n-aria-label="@@ngb.datepicker.previous-month" aria-label="Previous month"
i18n-title="@@ngb.datepicker.previous-month" title="Previous month">
<span class="ngb-dp-navigation-chevron"></span>
</button>
</div>
Expand All @@ -92,8 +93,9 @@ import {NgbDatepickerI18n} from './datepicker-i18n';
<div class="ngb-dp-arrow" *ngIf="i !== months.length - 1"></div>
</ng-template>
<div class="ngb-dp-arrow right">
<button type="button" class="btn btn-link ngb-dp-arrow-btn"
(click)="!!navigate.emit(navigation.NEXT)" [disabled]="nextDisabled">
<button type="button" class="btn btn-link ngb-dp-arrow-btn" (click)="!!navigate.emit(navigation.NEXT)" [disabled]="nextDisabled"
i18n-aria-label="@@ngb.datepicker.next-month" aria-label="Next month"
i18n-title="@@ngb.datepicker.next-month" title="Next month">
<span class="ngb-dp-navigation-chevron"></span>
</button>
</div>
Expand Down

0 comments on commit cd01d32

Please sign in to comment.