Skip to content

Commit

Permalink
Provide fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
kpustakhod committed Aug 22, 2023
1 parent 5f702e2 commit ab3a220
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 42 deletions.
20 changes: 2 additions & 18 deletions src/TreeView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ import {
symmetricDifference,
usePrevious,
usePreviousData,
isBranchSelectedAndHasSelectedDescendants,
getTreeParent,
getTreeNode,
validateTreeViewData,
noop,
isBranchNotSelectedAndHasOnlySelectedChild,
isBranchSelectedAndHasOnlySelectedChild,
getOnSelectTreeAction,
} from "./utils";
import { Node } from "./node";
import {
Expand Down Expand Up @@ -884,24 +883,9 @@ const handleKeyDown = ({
return;
}

const isSelectedAndHasSelectedDescendants = isBranchSelectedAndHasSelectedDescendants(
data,
element.id,
selectedIds
);

const isSelectedAndHasOnlySelectedChild = isBranchSelectedAndHasOnlySelectedChild(
data,
element.id,
selectedIds
);

dispatch({
type: togglableSelect
? isSelectedAndHasSelectedDescendants &&
!isSelectedAndHasOnlySelectedChild
? treeTypes.halfSelect
: treeTypes.toggleSelect
? getOnSelectTreeAction(data, id, selectedIds, disabledIds)
: treeTypes.select,
id: id,
multiSelect,
Expand Down
20 changes: 2 additions & 18 deletions src/TreeView/node.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ import {
getDescendants,
getTreeNode,
isBranchNode,
isBranchSelectedAndHasOnlySelectedChild,
isBranchSelectedAndHasSelectedDescendants,
noop,
propagatedIds,
getOnSelectTreeAction,
} from "./utils";
import { baseClassNames, clickActions } from "./constants";

Expand Down Expand Up @@ -148,25 +147,10 @@ export const Node = (props: INodeProps) => {
lastManuallyToggled: element.id,
});
} else if (event.ctrlKey || clickAction === clickActions.select) {
const isSelectedAndHasSelectedDescendants = isBranchSelectedAndHasSelectedDescendants(
data,
element.id,
selectedIds
);

const isSelectedAndHasOnlySelectedChild = isBranchSelectedAndHasOnlySelectedChild(
data,
element.id,
selectedIds
);

//Select
dispatch({
type: togglableSelect
? isSelectedAndHasSelectedDescendants &&
!isSelectedAndHasOnlySelectedChild
? treeTypes.halfSelect
: treeTypes.toggleSelect
? getOnSelectTreeAction(data, element.id, selectedIds, disabledIds)
: treeTypes.select,
id: element.id,
multiSelect,
Expand Down
55 changes: 49 additions & 6 deletions src/TreeView/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect, useRef } from "react";
import { EventCallback, INode, INodeRef, NodeId } from "./types";
import { treeTypes } from "./reducer";

// eslint-disable-next-line @typescript-eslint/no-empty-function
export const noop = () => {};
Expand Down Expand Up @@ -411,17 +412,18 @@ export const isBranchSelectedAndHasSelectedDescendants = (
);
};

export const isBranchNotSelectedAndHasAllSelectedDescendants = (
export const isBranchSelectedAndHasAllSelectedEnabledDescendants = (
data: INode[],
elementId: NodeId,
selectedIds: Set<NodeId>
selectedIds: Set<NodeId>,
disabledIds: Set<NodeId>
) => {
const children = getDescendants(data, elementId, new Set<number>());
return (
isBranchNode(data, elementId) &&
!selectedIds.has(elementId) &&
getDescendants(data, elementId, new Set<number>()).every((item) =>
selectedIds.has(item)
)
selectedIds.has(elementId) &&
children.every((item) => selectedIds.has(item)) &&
children.every((item) => !disabledIds.has(item))
);
};

Expand Down Expand Up @@ -453,6 +455,47 @@ export const isBranchSelectedAndHasOnlySelectedChild = (
);
};

export const getOnSelectTreeAction = (
data: INode[],
elementId: NodeId,
selectedIds: Set<NodeId>,
disabledIds: Set<NodeId>
) => {
const isSelectedAndHasSelectedDescendants = isBranchSelectedAndHasSelectedDescendants(
data,
elementId,
selectedIds
);

const isSelectedAndHasOnlySelectedChild = isBranchSelectedAndHasOnlySelectedChild(
data,
elementId,
selectedIds
);

const isSelectedAndHasAllSelectedEnabledDescendants = isBranchSelectedAndHasAllSelectedEnabledDescendants(
data,
elementId,
selectedIds,
disabledIds
);

if (isSelectedAndHasAllSelectedEnabledDescendants) {
// current element is branch and has no disabled and all selected descendants
return treeTypes.toggleSelect;
} else if (
// current element is branch and has any number of selected descendants
// OR
// current element is branch and has only one selected child
isSelectedAndHasSelectedDescendants &&
!isSelectedAndHasOnlySelectedChild
) {
return treeTypes.halfSelect;
}

return treeTypes.toggleSelect;
};

export const getTreeParent = (data: INode[]): INode => {
const parentNode: INode | undefined = data.find(
(node) => node.parent === null
Expand Down

0 comments on commit ab3a220

Please sign in to comment.