Skip to content
Closed
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
26 changes: 24 additions & 2 deletions src/TreeView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ const useTree = ({
useEffect(() => {
if (onSelect != null && onSelect !== noop) {
for (const toggledId of toggledIds) {
if (!data.find((node) => node.id === toggledId)) continue;
const isBranch =
isBranchNode(data, toggledId) ||
!!getTreeNode(data, tabbableId)?.isBranch;
Expand Down Expand Up @@ -144,7 +145,7 @@ const useTree = ({

useEffect(() => {
if (onNodeSelect != null && onNodeSelect !== noop) {
if (lastManuallyToggled != null) {
if (lastManuallyToggled != null && data.find((node) => node.id === lastManuallyToggled)) {
if (toggledIds.size) {
onNodeSelect({
element: getTreeNode(data, lastManuallyToggled),
Expand All @@ -163,6 +164,7 @@ const useTree = ({
const toggledExpandIds = symmetricDifference(expandedIds, prevExpandedIds);
if (onExpand != null && onExpand !== noop) {
for (const id of toggledExpandIds) {
if (!data.find((node) => node.id === id)) continue;
onExpand({
element: getTreeNode(data, id),
isExpanded: expandedIds.has(id),
Expand All @@ -189,6 +191,7 @@ const useTree = ({
const toggledExpandIds = symmetricDifference(expandedIds, prevExpandedIds);
if (onLoadData) {
for (const id of toggledExpandIds) {
if (!data.find((node) => node.id === id)) continue;
onLoadData({
element: getTreeNode(data, id),
isExpanded: expandedIds.has(id),
Expand All @@ -200,6 +203,7 @@ const useTree = ({
}
if (prevData !== data && togglableSelect && propagateSelect) {
for (const id of expandedIds) {
if (!data.find((node) => node.id === id)) continue;
selectedIds.has(id) &&
dispatch({
type: treeTypes.changeSelectMany,
Expand Down Expand Up @@ -265,13 +269,16 @@ const useTree = ({
toggledControlledIds.size &&
dispatch({
type: treeTypes.controlledSelectMany,
ids: controlledSelectedIds,
ids: controlledSelectedIds.filter((id) =>
data.some((node) => node.id === id)
),
multiSelect,
});
}

if (selectedIdsToUse) {
for (const id of selectedIdsToUse) {
if (!data.find((node) => node.id === id)) continue;
propagateSelect &&
!disabledIds.has(id) &&
dispatch({
Expand Down Expand Up @@ -300,6 +307,7 @@ const useTree = ({
//controlled collapsing
if (diffCollapseIds.size) {
for (const id of diffCollapseIds) {
if (!data.find((node) => node.id === id)) continue;
if (isBranchNode(data, id) || getTreeNode(data, id).isBranch) {
const ids = [id, ...getDescendants(data, id, new Set<number>())];
dispatch({
Expand All @@ -313,6 +321,7 @@ const useTree = ({
//controlled expanding
if (diffExpandedIds.size) {
for (const id of diffExpandedIds) {
if (!data.find((node) => node.id === id)) continue;
if (isBranchNode(data, id) || getTreeNode(data, id).isBranch) {
const parentId = getParent(data, id);
if (parentId) {
Expand Down Expand Up @@ -571,6 +580,8 @@ export interface ITreeViewProps<M extends IFlatMetadata = IFlatMetadata> {
treeState: ITreeViewState;
dispatch: React.Dispatch<TreeViewAction>;
}) => void;
/** Custom onKeyDown event handler */
onKeyDown?: React.KeyboardEventHandler<HTMLUListElement>;
/** Id of the node to focus */
focusedId?: NodeId;
}
Expand Down Expand Up @@ -725,6 +736,9 @@ const handleKeyDown = ({
}) => (event: React.KeyboardEvent) => {
const element = getTreeNode(data, tabbableId);
const id = element.id;
if (event.metaKey || event.altKey) {
return;
}
if (event.ctrlKey) {
if (event.key === "a" && clickAction !== clickActions.focus) {
event.preventDefault();
Expand Down Expand Up @@ -973,12 +987,20 @@ const handleKeyDown = ({
return;
default:
if (event.key.length === 1) {
const visited = new Set<NodeId>([id]);
let currentId = getNextAccessible(data, id, expandedIds);
while (currentId !== id) {
if (currentId == null) {
currentId = getTreeParent(data).children[0];
if (currentId == null || visited.has(currentId)) {
break;
}
continue;
}
if (visited.has(currentId)) {
break;
}
visited.add(currentId);
if (
getTreeNode(data, currentId).name[0].toLowerCase() ===
event.key.toLowerCase()
Expand Down
2 changes: 1 addition & 1 deletion src/TreeView/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ export type IFlatMetadata = Record<
string | number | boolean | undefined | null
>;

interface ITreeNode<M extends IFlatMetadata = IFlatMetadata> {
export interface ITreeNode<M extends IFlatMetadata = IFlatMetadata> {
id?: NodeId;
name: string;
isBranch?: boolean;
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import {
IBranchProps,
LeafProps,
} from "./TreeView/types";
import { flattenTree } from "./TreeView/utils";
import { flattenTree, ITreeNode, IFlatMetadata } from "./TreeView/utils";

export {
flattenTree,
ITreeNode,
IFlatMetadata,
ITreeViewProps,
INode,
NodeId,
Expand Down