Skip to content

[♻️][TreeView]: Refactor to improve interactivity #66

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

Merged
merged 1 commit into from
Mar 11, 2024
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
62 changes: 61 additions & 1 deletion lib/TreeView/TreeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
useControlledProp,
useDeterministicId,
useEventCallback,
useEventListener,
useForkedRefs,
useJumpToChar,
} from "../utils";
Expand Down Expand Up @@ -253,6 +254,8 @@ const TreeViewBase = (props: Props, ref: React.Ref<HTMLDivElement>) => {
if (currentFocusedElement?.item) {
switch (event.key) {
case SystemKeys.HOME: {
event.preventDefault();

const { item } = getAvailableItem(items, 0, true);

setActiveElement(item);
Expand All @@ -261,6 +264,8 @@ const TreeViewBase = (props: Props, ref: React.Ref<HTMLDivElement>) => {
}

case SystemKeys.END: {
event.preventDefault();

const { item } = getAvailableItem(items, items.length - 1, true);

setActiveElement(item);
Expand All @@ -269,8 +274,18 @@ const TreeViewBase = (props: Props, ref: React.Ref<HTMLDivElement>) => {
}

case SystemKeys.UP: {
event.preventDefault();

const { index } = currentFocusedElement;

if (index === -1) {
const { item } = getAvailableItem(items, items.length - 1, false);

setActiveElement(item);

break;
}

const nextIdx = (index - 1 + items.length) % items.length;
const { item } = getAvailableItem(items, nextIdx, false);

Expand All @@ -280,8 +295,18 @@ const TreeViewBase = (props: Props, ref: React.Ref<HTMLDivElement>) => {
}

case SystemKeys.DOWN: {
event.preventDefault();

const { index } = currentFocusedElement;

if (index === -1) {
const { item } = getAvailableItem(items, 0, true);

setActiveElement(item);

break;
}

const nextIdx = (index + 1) % items.length;
const { item } = getAvailableItem(items, nextIdx, true);

Expand All @@ -291,6 +316,8 @@ const TreeViewBase = (props: Props, ref: React.Ref<HTMLDivElement>) => {
}

case SystemKeys.RIGHT: {
event.preventDefault();

const { item } = currentFocusedElement;

const isExpandable = item.getAttribute("data-expandable") === "true";
Expand Down Expand Up @@ -323,6 +350,8 @@ const TreeViewBase = (props: Props, ref: React.Ref<HTMLDivElement>) => {
}

case SystemKeys.LEFT: {
event.preventDefault();

const { item } = currentFocusedElement;

const isExpandable = item.getAttribute("data-expandable") === "true";
Expand Down Expand Up @@ -353,6 +382,8 @@ const TreeViewBase = (props: Props, ref: React.Ref<HTMLDivElement>) => {
}

case SystemKeys.SPACE: {
event.preventDefault();

if (!isSelectable) break;

const { item } = currentFocusedElement;
Expand All @@ -367,6 +398,8 @@ const TreeViewBase = (props: Props, ref: React.Ref<HTMLDivElement>) => {
}

case SystemKeys.ENTER: {
event.preventDefault();

const { item } = currentFocusedElement;

const isExpandable = item.getAttribute("data-expandable") === "true";
Expand Down Expand Up @@ -411,7 +444,9 @@ const TreeViewBase = (props: Props, ref: React.Ref<HTMLDivElement>) => {
rootRef.current = event.currentTarget as HTMLDivElement;
}

if (event.target === rootRef.current && !activeElement) {
if (event.target !== event.currentTarget) rootRef.current?.focus();

if (!activeElement) {
const items = getListItems(id);
const currentFocusedElement = getCurrentFocusedElement(items, null);

Expand All @@ -421,6 +456,21 @@ const TreeViewBase = (props: Props, ref: React.Ref<HTMLDivElement>) => {
onFocus?.(event as React.FocusEvent<HTMLDivElement>);
});

const handleOutsideClick = useEventCallback<MouseEvent>(event => {
if (!event.target) return;
if (!rootRef.current) return;

const target = event.target as HTMLElement;

if (rootRef.current === target) return;

const root = target.closest<HTMLElement>(`[data-slot='${RootSlot}']`);

if (root) return;

setActiveElement(null);
});

const context: TreeViewContextValue = {
activeElement,
isSelectable,
Expand Down Expand Up @@ -457,6 +507,16 @@ const TreeViewBase = (props: Props, ref: React.Ref<HTMLDivElement>) => {

const { validChildren, sizeOfSet } = getValidChildren(children, "TreeView");

if (typeof document !== "undefined") {
// eslint-disable-next-line react-hooks/rules-of-hooks
useEventListener({
target: document,
eventType: "click",
handler: handleOutsideClick,
options: { capture: true },
});
}

return (
<div
{...otherProps}
Expand Down
4 changes: 3 additions & 1 deletion lib/TreeView/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ export const getCurrentFocusedElement = (

const idx = selectedItemIdx === -1 ? 0 : selectedItemIdx;

return getAvailableItem(items, idx, true);
const availableItem = getAvailableItem(items, idx, true);

return { item: availableItem.item, index: availableItem.index || -1 };
};
/* eslint-enable @typescript-eslint/no-non-null-assertion */