-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add keyboard navigation to input table
- Loading branch information
Showing
14 changed files
with
859 additions
and
8 deletions.
There are no files selected for viewing
50 changes: 47 additions & 3 deletions
50
frontend/libs/studio-components/src/components/StudioInputTable/Cell/BaseInputCell.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,67 @@ | ||
import type { ForwardedRef, ForwardRefRenderFunction, PropsWithoutRef } from 'react'; | ||
import type { ForwardedRef, ForwardRefRenderFunction, KeyboardEvent, PropsWithoutRef } from 'react'; | ||
import { forwardRef } from 'react'; | ||
import type { InputCellComponent } from '../types/InputCellComponent'; | ||
import type { HTMLCellInputElement } from '../types/HTMLCellInputElement'; | ||
import { getNextInputElement } from '../dom-utils/getNextInputElement'; | ||
|
||
export abstract class BaseInputCell<Element extends HTMLCellInputElement, Props extends {}> { | ||
type DefaultProps<Element extends HTMLCellInputElement> = { | ||
onKeyDown?: (event: KeyboardEvent<Element>) => void; | ||
tabIndex?: number; | ||
}; | ||
|
||
export abstract class BaseInputCell< | ||
Element extends HTMLCellInputElement, | ||
Props extends DefaultProps<Element>, | ||
> { | ||
displayName: string; | ||
|
||
constructor(displayName: string) { | ||
this.displayName = displayName; | ||
} | ||
|
||
component(): InputCellComponent<Props, Element> { | ||
const component = forwardRef<Element, Props>(this.render); | ||
const component = forwardRef<Element, Props>(this.renderWithDefaultProps); | ||
component.displayName = this.displayName; | ||
return component; | ||
} | ||
|
||
private renderWithDefaultProps: ForwardRefRenderFunction<Element, PropsWithoutRef<Props>> = ( | ||
props, | ||
ref, | ||
) => this.render({ ...props, ...this.defaultProps }, ref); | ||
|
||
protected abstract render( | ||
props: PropsWithoutRef<Props>, | ||
ref: ForwardedRef<Element>, | ||
): ReturnType<ForwardRefRenderFunction<Element, Props>>; | ||
|
||
private defaultProps: Required<DefaultProps<Element>> = { | ||
onKeyDown: (event) => this.handleKeyDown(event), | ||
tabIndex: -1, | ||
}; | ||
|
||
private handleKeyDown(event: KeyboardEvent<Element>): void { | ||
if (this.shouldMoveFocusOnArrowKey(event)) { | ||
this.moveFocus(event); | ||
} | ||
} | ||
|
||
protected abstract shouldMoveFocusOnArrowKey(event: KeyboardEvent<Element>): boolean; | ||
|
||
private moveFocus(event: KeyboardEvent<Element>) { | ||
const nextElement = this.getNextElement(event); | ||
if (nextElement) { | ||
event.preventDefault(); | ||
nextElement.tabIndex = 0; | ||
nextElement.focus(); | ||
event.currentTarget.tabIndex = -1; | ||
} | ||
} | ||
|
||
private getNextElement({ | ||
key, | ||
currentTarget, | ||
}: KeyboardEvent<Element>): HTMLCellInputElement | null { | ||
return getNextInputElement(currentTarget, key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.