Skip to content

Commit

Permalink
Keyboard navigation
Browse files Browse the repository at this point in the history
Refs: #4646
  • Loading branch information
Makko74 committed Aug 14, 2024
1 parent 490e6c4 commit 0ce91e8
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 33 deletions.
3 changes: 1 addition & 2 deletions packages/components/src/components/@shared/kol-menu.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
@mixin kol-menu-styles {

.menu {
&__listbox {
list-style-type: none;
Expand Down Expand Up @@ -29,4 +28,4 @@
min-height: rem(50);
}
}
}
}
50 changes: 25 additions & 25 deletions packages/components/src/components/@shared/kol-menu.tsx
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
import { FunctionalComponent, h } from '@stencil/core';
import { JSX } from '@stencil/core/internal';
import { Option } from '../../schema';
import type { FunctionalComponent } from '@stencil/core';
import { h } from '@stencil/core';
import type { JSX } from '@stencil/core/internal';
import type { Option } from '../../schema';
import clsx from 'clsx';

type KolMenuProps = {
options: Option<number>[];
focusedOptionIndex: number;
onClick: (event: Event, option: unknown) => void;
onItemClick: (event: Event, option: unknown) => void;
renderOption?: (option: Option<number>) => JSX.Element;
selectedValue?: number | string;
};
export const KolMenu: FunctionalComponent<KolMenuProps> = ({ options, onClick, renderOption, selectedValue }) => {
let listItems: HTMLElement | undefined = undefined;
// const index: number = 0;
function handleKeyDown(event: KeyboardEvent): void {
export const KolMenu: FunctionalComponent<KolMenuProps> = ({ options, onItemClick, renderOption, selectedValue }) => {
const listItems: (HTMLElement | undefined)[] = [];

function handleKeyDown(event: KeyboardEvent, index: number, option: unknown): void {
if (event.key === 'ArrowDown') {
console.log(event.target);
console.log(listItems);
const nextIndex = index + 1 < listItems.length ? index + 1 : 0;
listItems[nextIndex]?.focus();
event.preventDefault();
} else if (event.key === 'ArrowUp') {
const prevIndex = index - 1 >= 0 ? index - 1 : listItems.length - 1;
listItems[prevIndex]?.focus();
event.preventDefault();
} else if (event.key === 'Enter' || event.key === ' ') {
onItemClick(event, option);
event.preventDefault();
}
// if (event.key === 'ArrowDown') {
// const nextIndex = index + 1 < listItems.length ? index + 1 : 0;
// listItems[nextIndex].focus();
// event.preventDefault();
// } else if (event.key === 'ArrowUp') {
// const prevIndex = index - 1 >= 0 ? index - 1 : listItems.length - 1;
// listItems[prevIndex].focus();
// event.preventDefault();
// } else if (event.key === 'Enter' || event.key === ' ') {
// onClick(event, option);
// event.preventDefault();
// }
}

return (
<ul role="listbox" class={clsx('menu__listbox')} onKeyDown={(event) => handleKeyDown(event)} ref={(el) => (listItems = el)}>
<ul role="listbox" class={clsx('menu__listbox')}>
{options.length > 0 &&
options.map((option, index) => (
<li
ref={(el) => (listItems[index] = el)}
id={`option-${index}`}
key={index}
tabIndex={-1}
role="option"
aria-selected={selectedValue === option.value}
onClick={(event) => onClick(event, option)}
class="menu__item"
aria-selected={selectedValue === option.value}
onKeyDown={(event) => handleKeyDown(event, index, option)}
onClick={(event) => onItemClick(event, option)}
>
{renderOption && renderOption(option)}
</li>
Expand Down
3 changes: 1 addition & 2 deletions packages/components/src/components/combobox/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
max-height: rem(250);
}


&__item {
&.highlighted {
background-color: #f0f0f0;
Expand All @@ -61,4 +60,4 @@
}
}
}
}
}
4 changes: 2 additions & 2 deletions packages/components/src/components/pagination/shadow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ export class KolPagination implements PaginationAPI {
<KolMenu
options={this.state._pageSizeOptions}
selectedValue={this.state._pageSize}
onClick={(event, value) => {
this.onChangePageSize(event, value);
onItemClick={(event, option: unknown) => {
this.onChangePageSize(event, (option as { value: string }).value);
}}
focusedOptionIndex={-1}
renderOption={(option: Option<number>) => (
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/components/pagination/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
.separator:before {
content: '•••';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@
min-height: rem(50);
}
}
}
}

0 comments on commit 0ce91e8

Please sign in to comment.