@@ -5,6 +5,7 @@ import { IgxFocusTrapDirective } from './focus-trap.directive';
5
5
6
6
import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
7
7
import { UIInteractions } from '../../test-utils/ui-interactions.spec' ;
8
+ import { IgxTimePickerComponent } from '../../time-picker/time-picker.component' ;
8
9
9
10
describe ( 'igxFocusTrap' , ( ) => {
10
11
beforeEach ( waitForAsync ( ( ) => {
@@ -161,6 +162,39 @@ describe('igxFocusTrap', () => {
161
162
fix . detectChanges ( ) ;
162
163
expect ( document . activeElement ) . toEqual ( button . nativeElement ) ;
163
164
} ) ) ;
165
+
166
+ it ( 'should focus only visible focusable elements on Tab key pressed' , ( ) => {
167
+ const fix = TestBed . createComponent ( TrapFocusTestComponent ) ;
168
+ fix . detectChanges ( ) ;
169
+
170
+ fix . componentInstance . showTimePicker = true ;
171
+ fix . detectChanges ( ) ;
172
+
173
+ const focusTrap = fix . debugElement . query ( By . directive ( IgxFocusTrapDirective ) ) ;
174
+ const buttons = fix . debugElement . queryAll ( By . css ( 'button' ) ) ;
175
+ const inputs = fix . debugElement . queryAll ( By . css ( 'input' ) ) ;
176
+ const timePickerInput = fix . debugElement . query ( By . css ( '.igx-input-group__input' ) ) ;
177
+
178
+ UIInteractions . triggerEventHandlerKeyDown ( 'Tab' , focusTrap ) ;
179
+ fix . detectChanges ( ) ;
180
+ expect ( document . activeElement ) . toEqual ( inputs [ 0 ] . nativeElement ) ;
181
+
182
+ UIInteractions . triggerEventHandlerKeyDown ( 'Tab' , focusTrap ) ;
183
+ fix . detectChanges ( ) ;
184
+ expect ( document . activeElement ) . toEqual ( inputs [ 1 ] . nativeElement ) ;
185
+
186
+ UIInteractions . triggerEventHandlerKeyDown ( 'Tab' , focusTrap ) ;
187
+ fix . detectChanges ( ) ;
188
+ expect ( document . activeElement ) . toEqual ( timePickerInput . nativeElement ) ;
189
+
190
+ UIInteractions . triggerEventHandlerKeyDown ( 'Tab' , focusTrap ) ;
191
+ fix . detectChanges ( ) ;
192
+ expect ( document . activeElement ) . toEqual ( buttons [ buttons . length - 1 ] . nativeElement ) ;
193
+
194
+ UIInteractions . triggerEventHandlerKeyDown ( 'Tab' , focusTrap ) ;
195
+ fix . detectChanges ( ) ;
196
+ expect ( document . activeElement ) . toEqual ( inputs [ 0 ] . nativeElement ) ;
197
+ } ) ;
164
198
} ) ;
165
199
166
200
@@ -177,14 +211,19 @@ describe('igxFocusTrap', () => {
177
211
<input type="password" placeholder="Enter Password" name="psw">
178
212
}
179
213
<br>
214
+ @if (showTimePicker) {
215
+ <igx-time-picker> </igx-time-picker>
216
+ }
217
+ <br>
180
218
@if (showButton) {
181
219
<button>SIGN IN</button>
182
220
}
183
221
</div>` ,
184
- imports : [ IgxFocusTrapDirective ]
222
+ imports : [ IgxFocusTrapDirective , IgxTimePickerComponent ]
185
223
} )
186
224
class TrapFocusTestComponent {
187
225
public showInput = true ;
188
226
public showButton = true ;
189
227
public focusTrap = true ;
228
+ public showTimePicker = false ;
190
229
}
0 commit comments