Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add up arrow key press to global search #1587

Merged
merged 8 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion core/src/ConfirmationModal.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ <h3 class="fd-dialog__title">{settings.header}</h3>
<script>
import { createEventDispatcher, onMount } from 'svelte';
import { LuigiI18N } from './core-api';
import { KEYCODE_ESC } from './utilities/keycode.js';

const dispatch = createEventDispatcher();

Expand All @@ -58,7 +59,7 @@ <h3 class="fd-dialog__title">{settings.header}</h3>
// [svelte-upgrade suggestion]
// review these functions and remove unnecessary 'export' keywords
export function handleKeydown(event) {
if (event.keyCode === 27) {
if (event.keyCode === KEYCODE_ESC) {
dispatch('modalDismiss');
}
}
Expand Down
3 changes: 2 additions & 1 deletion core/src/Modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ <h3 class="fd-dialog__title">{modalSettings.title}</h3>
RoutingHelpers
} from './utilities/helpers';
import { LuigiConfig } from './core-api';
import { KEYCODE_ESC } from './utilities/keycode.js';

export let modalSettings;
export let isDataPrepared = false;
Expand Down Expand Up @@ -179,7 +180,7 @@ <h3 class="fd-dialog__title">{modalSettings.title}</h3>
// [svelte-upgrade suggestion]
// review these functions and remove unnecessary 'export' keywords
export function handleKeydown(event) {
if (event.keyCode === 27) {
if (event.keyCode === KEYCODE_ESC) {
dispatch('close');
}
}
Expand Down
65 changes: 40 additions & 25 deletions core/src/navigation/GlobalSearch.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@
import { LuigiAuth, LuigiConfig } from '../core-api';
import { GenericHelpers } from '../utilities/helpers';
import { Routing } from '../services/routing';
import {
KEYCODE_ARROW_UP,
KEYCODE_ARROW_DOWN,
KEYCODE_ENTER,
KEYCODE_ESC
} from '../utilities/keycode.js';
export let isSearchFieldVisible;
export let searchResult = [];
export let displaySearchResult;
Expand Down Expand Up @@ -122,19 +128,19 @@
dispatch('closeSearchResult');
}

function onKeyUp(event) {
function onKeyUp({ keyCode }) {
if (search) {
if (
GenericHelpers.isFunction(search.searchProvider.onEnter) &&
event.keyCode === 13
keyCode === KEYCODE_ENTER
) {
search.searchProvider.onEnter();
} else if (
GenericHelpers.isFunction(search.searchProvider.onEscape) &&
event.keyCode === 27
keyCode === KEYCODE_ESC
) {
search.searchProvider.onEscape();
} else if (event.keyCode === 40) {
} else if (keyCode === KEYCODE_ARROW_DOWN) {
if (displaySearchResult) {
document
.querySelector('.luigi-search-result-item__0')
Expand All @@ -154,18 +160,29 @@
luigiCustomSearchItemRenderer__slotContainer.children;
if (renderedSearchResultItems) {
for (let index = 0; index < renderedSearchResultItems.length; index++) {
let element = renderedSearchResultItems[index];
if (element.childNodes[0].getAttribute('aria-selected') === 'true') {
if (direction === 40) {
let nextSibling =
element.nextSibling != null
? element.nextSibling
: renderedSearchResultItems[0];
element.childNodes[0].setAttribute('aria-selected', 'false');
nextSibling.childNodes[0].setAttribute('aria-selected', 'true');
nextSibling.focus();
break;
let {
childNodes,
nextSibling,
previousSibling
} = renderedSearchResultItems[index];
let nodeSibling;
if (childNodes[0].getAttribute('aria-selected') === 'true') {
if (direction === KEYCODE_ARROW_DOWN) {
nodeSibling =
nextSibling !== null ? nextSibling : renderedSearchResultItems[0];
}
if (direction === KEYCODE_ARROW_UP) {
nodeSibling =
previousSibling !== null
? previousSibling
: renderedSearchResultItems[
renderedSearchResultItems.length - 1
];
}
childNodes[0].setAttribute('aria-selected', 'false');
nodeSibling.childNodes[0].setAttribute('aria-selected', 'true');
nodeSibling.focus();
break;
}
}
}
Expand Down Expand Up @@ -194,23 +211,21 @@
search.searchProvider.onSearchResultItemSelected(searchResultItem);
} else if (
GenericHelpers.isFunction(search.searchProvider.onEscape) &&
event.keyCode === 27
event.keyCode === KEYCODE_ESC
) {
search.searchProvider.onEscape();
}
}

function handleKeydown(result, event) {
if (event.keyCode === 13) {
function handleKeydown(result, { keyCode }) {
if (keyCode === KEYCODE_ENTER) {
search.searchProvider.onSearchResultItemSelected(result);
}
if (event.keyCode === 38) {
calcSearchResultItemSelected(38);
} else if (event.keyCode === 40) {
calcSearchResultItemSelected(40);
if (keyCode === KEYCODE_ARROW_UP || keyCode === KEYCODE_ARROW_DOWN) {
calcSearchResultItemSelected(keyCode);
} else if (
GenericHelpers.isFunction(search.searchProvider.onEscape) &&
event.keyCode === 27
keyCode === KEYCODE_ESC
) {
clearAriaSelected();
setTimeout(() => {
Expand Down Expand Up @@ -277,9 +292,9 @@
max-height: 70vh;
overflow: auto;
.fd-menu__item {
padding-bottom: 10px;
margin-bottom: 10px;
&:first-child {
padding-top: 10px;
margin-top: 10px;
}
&:last-child .fd-menu__link {
border-radius: 0;
Expand Down
4 changes: 4 additions & 0 deletions core/src/utilities/keycode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const KEYCODE_ARROW_UP = 38;
export const KEYCODE_ARROW_DOWN = 40;
export const KEYCODE_ENTER = 13;
export const KEYCODE_ESC = 27;
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ describe('Luigi client linkManager', () => {
.type($inputTypeModal);

cy.wrap($modal)
.find('button')
.contains('Click here')
.click();
});
cy.wrap($iframeBody).should('contain', $inputTypeModal);
Expand Down