Skip to content

Commit 7741ff8

Browse files
authored
Merge pull request #159 from JYChuah01/pcLookup-keydown-scroll-fix
Pclookup keyDown scroll navigation focus fix
2 parents fe0efcc + af7bf8a commit 7741ff8

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/main/default/lwc/lookup/__tests__/lookupEventHandling.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ describe('c-lookup event handling', () => {
7979
});
8080

8181
it('can select item with keyboard', async () => {
82+
Element.prototype.scrollIntoView = jest.fn();
8283
jest.useFakeTimers();
8384

8485
// Create lookup with search handler
@@ -99,6 +100,10 @@ describe('c-lookup event handling', () => {
99100
// Check selection
100101
expect(lookupEl.selection.length).toBe(1);
101102
expect(lookupEl.selection[0].id).toBe(SAMPLE_SEARCH_ITEMS[0].id);
103+
104+
// Check if the scroll focus is functional
105+
const focusedElement = lookupEl.shadowRoot.querySelector(`[data-recordid="${SAMPLE_SEARCH_ITEMS[0].id}"]`);
106+
expect(focusedElement.scrollIntoView).toHaveBeenCalled();
102107
});
103108

104109
it('can create new record without pre-navigate callback', async () => {

src/main/default/lwc/lookup/lookup.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,15 @@ export default class Lookup extends NavigationMixin(LightningElement) {
258258
this.template.querySelector(`[data-recordid="${selectedId}"]`).click();
259259
event.preventDefault();
260260
}
261+
262+
if (event.keyCode === KEY_ARROW_DOWN || event.keyCode === KEY_ARROW_UP) {
263+
const focusedElement = this.template.querySelector(
264+
`[data-recordid="${this._searchResults[this._focusedResultIndex].id}"]`
265+
);
266+
if (focusedElement) {
267+
focusedElement.scrollIntoView({ block: 'nearest', inline: 'nearest' });
268+
}
269+
}
261270
}
262271

263272
handleResultClick(event) {

0 commit comments

Comments
 (0)