Skip to content

Commit 9efa7c2

Browse files
authored
Merge pull request #331 from UiPath/fix/change-focus-logic
fix(suggest): fix focus logic when navigating with TAB
2 parents 3dacfcb + 4100f8f commit 9efa7c2

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

projects/angular/components/ui-grid/src/ui-grid.component.scss

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,7 @@ ui-grid {
321321
justify-content: center;
322322
display: flex;
323323
align-items: center;
324-
325-
mat-icon {
326-
margin-right: $ui-grid-default-spacing;
327-
}
324+
margin-right: $ui-grid-default-spacing;
328325
}
329326

330327
&-dropdown-filter {

projects/angular/components/ui-suggest/src/ui-suggest.component.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
(keydown.space)="preventDefault($event)"
3838
(focus)="onFocus();"
3939
(blur)="onBlur()"
40+
#displayContainer
4041
matRipple
4142
class="display"
4243
role="combobox">
@@ -173,7 +174,7 @@
173174
(keydown.esc)="preventDefault($event)"
174175
(keyup.esc)="preventDefault($event);
175176
close();"
176-
(keydown.tab)="close(false)"
177+
(keydown.tab)="onOptionsDropdownTabPressed()"
177178
(keydown.shift.tab)="close()"
178179
tabindex="-1"
179180
class="ui-suggest-dropdown-item-list-container">

projects/angular/components/ui-suggest/src/ui-suggest.component.spec.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,29 @@ const sharedSpecifications = (
990990
assert.isOpen();
991991
});
992992

993+
it('should focus the display element if Tab key is pressed and the dropdown is open', () => {
994+
fixture.detectChanges();
995+
996+
const display = fixture.debugElement.query(By.css('.display'));
997+
display.nativeElement.dispatchEvent(
998+
EventGenerator.keyDown(Key.Enter),
999+
);
1000+
1001+
fixture.detectChanges();
1002+
1003+
const itemContainer = fixture.debugElement.query(By.css('.ui-suggest-dropdown-item-list-container'));
1004+
itemContainer.nativeElement.dispatchEvent(
1005+
EventGenerator.keyDown(Key.Tab),
1006+
);
1007+
1008+
fixture.detectChanges();
1009+
1010+
// We can't test if the next element is focused because jasmine can't simulate the default behavior of the TAB key.
1011+
// We will test if the implemented logic is working.
1012+
// https://stackoverflow.com/questions/25032300/jasmine-test-simulate-tab-keydown-and-detect-newly-focused-element
1013+
expect(document.activeElement).toBe(display.nativeElement);
1014+
});
1015+
9931016
it('should close if Tab is pressed', () => {
9941017
fixture.detectChanges();
9951018
const display = fixture.debugElement.query(By.css('.display'));
@@ -2760,7 +2783,7 @@ describe('Component: UiSuggest', () => {
27602783
[drillDown]="drillDown"
27612784
[compact]="compact"
27622785
formControlName="test">
2763-
</ui-suggest>
2786+
</ui-suggest>
27642787
</mat-form-field>
27652788
</form>
27662789
`,

projects/angular/components/ui-suggest/src/ui-suggest.component.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,7 @@ export class UiSuggestComponent extends UiSuggestMatFormFieldDirective
684684
suggestContainerWidth = 0;
685685

686686
@ViewChild('suggestContainer') suggestContainer!: ElementRef;
687+
@ViewChild('displayContainer') displayContainer?: ElementRef;
687688

688689
@ViewChild(CdkVirtualScrollViewport)
689690
protected set _virtualScrollerQuery(value: CdkVirtualScrollViewport) {
@@ -1294,6 +1295,13 @@ export class UiSuggestComponent extends UiSuggestMatFormFieldDirective
12941295
: this.defaultValue;
12951296
}
12961297

1298+
onOptionsDropdownTabPressed() {
1299+
if (this.isOpen && !this.expandInline) {
1300+
this.displayContainer?.nativeElement.focus();
1301+
}
1302+
this.close(false);
1303+
}
1304+
12971305
private _getDropdownPositionAccordingToDirection() {
12981306
return this.direction === 'up' ? this.upPosition : this.downPosition;
12991307
}

0 commit comments

Comments
 (0)