@@ -1119,6 +1119,32 @@ describe('MatDatepicker', () => {
11191119 expect ( document . activeElement ) . toBe ( toggle , 'Expected focus to be restored to toggle.' ) ;
11201120 } ) ;
11211121
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+
11221148 it ( 'should not override focus if it was moved inside the closed event in touchUI mode' ,
11231149 fakeAsync ( ( ) => {
11241150 const focusTarget = document . createElement ( 'button' ) ;
@@ -2318,13 +2344,14 @@ class DatepickerWithFormControl {
23182344 template : `
23192345 <input [matDatepicker]="d">
23202346 <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>
23222348 ` ,
23232349} )
23242350class DatepickerWithToggle {
23252351 @ViewChild ( 'd' ) datepicker : MatDatepicker < Date > ;
23262352 @ViewChild ( MatDatepickerInput ) input : MatDatepickerInput < Date > ;
23272353 touchUI = true ;
2354+ restoreFocus = true ;
23282355 ariaLabel : string ;
23292356}
23302357
0 commit comments