@@ -1119,6 +1119,32 @@ describe('MatDatepicker', () => {
1119
1119
expect ( document . activeElement ) . toBe ( toggle , 'Expected focus to be restored to toggle.' ) ;
1120
1120
} ) ;
1121
1121
1122
+ it ( 'should allow for focus restoration to be disabled' , ( ) => {
1123
+ let toggle = fixture . debugElement . query ( By . css ( 'button' ) ) ! . nativeElement ;
1124
+
1125
+ fixture . componentInstance . touchUI = false ;
1126
+ fixture . componentInstance . restoreFocus = false ;
1127
+ fixture . detectChanges ( ) ;
1128
+
1129
+ toggle . focus ( ) ;
1130
+ expect ( document . activeElement ) . toBe ( toggle , 'Expected toggle to be focused.' ) ;
1131
+
1132
+ fixture . componentInstance . datepicker . open ( ) ;
1133
+ fixture . detectChanges ( ) ;
1134
+
1135
+ let pane = document . querySelector ( '.cdk-overlay-pane' ) ! ;
1136
+
1137
+ expect ( pane ) . toBeTruthy ( 'Expected calendar to be open.' ) ;
1138
+ expect ( pane . contains ( document . activeElement ) )
1139
+ . toBe ( true , 'Expected focus to be inside the calendar.' ) ;
1140
+
1141
+ fixture . componentInstance . datepicker . close ( ) ;
1142
+ fixture . detectChanges ( ) ;
1143
+
1144
+ expect ( document . activeElement )
1145
+ . not . toBe ( toggle , 'Expected focus not to be restored to toggle.' ) ;
1146
+ } ) ;
1147
+
1122
1148
it ( 'should not override focus if it was moved inside the closed event in touchUI mode' ,
1123
1149
fakeAsync ( ( ) => {
1124
1150
const focusTarget = document . createElement ( 'button' ) ;
@@ -2318,13 +2344,14 @@ class DatepickerWithFormControl {
2318
2344
template : `
2319
2345
<input [matDatepicker]="d">
2320
2346
<mat-datepicker-toggle [for]="d" [aria-label]="ariaLabel"></mat-datepicker-toggle>
2321
- <mat-datepicker #d [touchUi]="touchUI"></mat-datepicker>
2347
+ <mat-datepicker #d [touchUi]="touchUI" [restoreFocus]="restoreFocus" ></mat-datepicker>
2322
2348
` ,
2323
2349
} )
2324
2350
class DatepickerWithToggle {
2325
2351
@ViewChild ( 'd' ) datepicker : MatDatepicker < Date > ;
2326
2352
@ViewChild ( MatDatepickerInput ) input : MatDatepickerInput < Date > ;
2327
2353
touchUI = true ;
2354
+ restoreFocus = true ;
2328
2355
ariaLabel : string ;
2329
2356
}
2330
2357
0 commit comments