Skip to content

Commit 653d0cc

Browse files
committed
feat(material/timepicker): add option template
close #31209
1 parent a656584 commit 653d0cc

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

src/material/timepicker/timepicker.spec.ts

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import {Component, Injector, Provider, signal, ViewChild, ViewEncapsulation} from '@angular/core';
1+
import {
2+
Component,
3+
inject,
4+
Injector,
5+
Provider,
6+
signal,
7+
ViewChild,
8+
ViewEncapsulation,
9+
} from '@angular/core';
210
import {ComponentFixture, fakeAsync, flush, TestBed} from '@angular/core/testing';
311
import {DateAdapter, MATERIAL_ANIMATIONS, provideNativeDateAdapter} from '../core';
412
import {
@@ -652,6 +660,24 @@ describe('MatTimepicker', () => {
652660
fixture.detectChanges();
653661
expect(fixture.componentInstance.timepicker.interval()).toBe(null);
654662
});
663+
664+
it('should be able to apply custom options template', () => {
665+
const fixture = TestBed.createComponent(TimepickerOptionTemplate);
666+
fixture.detectChanges();
667+
668+
getInput(fixture).click();
669+
fixture.detectChanges();
670+
expect(getOptions().map(o => o.textContent.trim())).toEqual([
671+
'12:00 AM (off-hours)',
672+
'3:00 AM (off-hours)',
673+
'6:00 AM (off-hours)',
674+
'9:00 AM (working hours)',
675+
'12:00 PM (working hours)',
676+
'3:00 PM (working hours)',
677+
'6:00 PM (off-hours)',
678+
'9:00 PM (off-hours)',
679+
]);
680+
});
655681
});
656682

657683
describe('mat-form-field integration', () => {
@@ -1512,3 +1538,28 @@ class TimepickerWithoutInput {
15121538
encapsulation: ViewEncapsulation.ShadowDom,
15131539
})
15141540
class TimepickerInShadowDom {}
1541+
1542+
@Component({
1543+
template: `
1544+
<input [matTimepicker]="picker" />
1545+
<mat-timepicker #picker interval="3h">
1546+
<ng-template let-option>
1547+
{{ option.label }}
1548+
{{ (dateAdapter.compareTime(option.value, workStartTime) > 0 && dateAdapter.compareTime(option.value, workEndTime) < 0) ? '(working hours)' : '(off-hours)' }}
1549+
</ng-template>
1550+
</mat-timepicker>
1551+
`,
1552+
imports: [MatTimepicker, MatTimepickerInput],
1553+
})
1554+
class TimepickerOptionTemplate {
1555+
readonly dateAdapter = inject<DateAdapter<Date>>(DateAdapter);
1556+
readonly workStartTime: Date;
1557+
readonly workEndTime: Date;
1558+
1559+
constructor() {
1560+
this.workStartTime = new Date();
1561+
this.workStartTime.setHours(8, 30, 0);
1562+
this.workEndTime = new Date();
1563+
this.workEndTime.setHours(17, 0, 0);
1564+
}
1565+
}

0 commit comments

Comments
 (0)