Skip to content

Commit

Permalink
Merge pull request #159 from JYChuah01/pcLookup-keydown-scroll-fix
Browse files Browse the repository at this point in the history
Pclookup keyDown scroll navigation focus fix
  • Loading branch information
pozil authored Aug 12, 2024
2 parents fe0efcc + af7bf8a commit 7741ff8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ describe('c-lookup event handling', () => {
});

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

// Create lookup with search handler
Expand All @@ -99,6 +100,10 @@ describe('c-lookup event handling', () => {
// Check selection
expect(lookupEl.selection.length).toBe(1);
expect(lookupEl.selection[0].id).toBe(SAMPLE_SEARCH_ITEMS[0].id);

// Check if the scroll focus is functional
const focusedElement = lookupEl.shadowRoot.querySelector(`[data-recordid="${SAMPLE_SEARCH_ITEMS[0].id}"]`);
expect(focusedElement.scrollIntoView).toHaveBeenCalled();
});

it('can create new record without pre-navigate callback', async () => {
Expand Down
9 changes: 9 additions & 0 deletions src/main/default/lwc/lookup/lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,15 @@ export default class Lookup extends NavigationMixin(LightningElement) {
this.template.querySelector(`[data-recordid="${selectedId}"]`).click();
event.preventDefault();
}

if (event.keyCode === KEY_ARROW_DOWN || event.keyCode === KEY_ARROW_UP) {
const focusedElement = this.template.querySelector(
`[data-recordid="${this._searchResults[this._focusedResultIndex].id}"]`
);
if (focusedElement) {
focusedElement.scrollIntoView({ block: 'nearest', inline: 'nearest' });
}
}
}

handleResultClick(event) {
Expand Down

0 comments on commit 7741ff8

Please sign in to comment.