Skip to content

Commit

Permalink
Add keyboard navigation in table-stateless
Browse files Browse the repository at this point in the history
Refs: #6567
  • Loading branch information
anicyne committed Aug 5, 2024
1 parent 6b5f4da commit d0d8406
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion packages/components/src/components/table-stateless/component.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { JSX } from '@stencil/core';
import { Component, Element, h, Host, Prop, State, Watch } from '@stencil/core';
import { Component, Element, h, Host, Listen, Prop, State, Watch } from '@stencil/core';

import type {
KoliBriTableCell,
Expand Down Expand Up @@ -55,6 +55,8 @@ export class KolTableStateless implements TableStatelessAPI {
private cellsToRenderTimeouts = new Map<HTMLElement, ReturnType<typeof setTimeout>>();
private dataToKeyMap = new Map<KoliBriTableDataType, string>();

private checkboxRefs: HTMLKolInputCheckboxElement[] = [];

@State()
private tableDivElementHasScrollbar = false;

Expand Down Expand Up @@ -136,6 +138,26 @@ export class KolTableStateless implements TableStatelessAPI {
validateTableSelection(this, value);
}

@Listen('keydown')
public handleKeyDown(event: KeyboardEvent) {
if (event.key === 'ArrowUp' || event.key === 'ArrowDown') {
const focusedElement = this.host?.shadowRoot?.activeElement as HTMLKolInputCheckboxElement;
let index = this.checkboxRefs.indexOf(focusedElement);
console.log(focusedElement);

Check failure on line 146 in packages/components/src/components/table-stateless/component.tsx

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement

if (focusedElement && this.checkboxRefs.includes(focusedElement))
if (event.key === 'ArrowDown') {
event.preventDefault();
index = (index + 1) % this.checkboxRefs.length;
this.checkboxRefs[index].focus();

Check failure on line 152 in packages/components/src/components/table-stateless/component.tsx

View workflow job for this annotation

GitHub Actions / build

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
} else if (event.key === 'ArrowUp') {
event.preventDefault();
index = (index - 1) % this.checkboxRefs.length;
this.checkboxRefs[index].focus();

Check failure on line 156 in packages/components/src/components/table-stateless/component.tsx

View workflow job for this annotation

GitHub Actions / build

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
}
}
}

public componentDidRender(): void {
this.checkDivElementScrollbar();
}
Expand Down Expand Up @@ -391,6 +413,7 @@ export class KolTableStateless implements TableStatelessAPI {
return (
<td key={`tbody-${rowIndex}-selection`} class="selection-cell">
<KolInputCheckboxTag
ref={(el) => el && this.checkboxRefs.push(el)}
_label={label}
_hideLabel
_checked={selected}
Expand Down

0 comments on commit d0d8406

Please sign in to comment.