close
Skip to content
Merged
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
52 changes: 27 additions & 25 deletions src/TreeNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,6 @@ const TreeNode: React.FC<Readonly<TreeNodeProps>> = props => {

const [dragNodeHighlight, setDragNodeHighlight] = React.useState<boolean>(false);

// ======= State: Selectable Check =======
const memoizedIsSelectable = React.useMemo<boolean>(() => {
// Ignore when selectable is undefined or null
if (typeof selectable === 'boolean') {
return selectable;
}
return context.selectable;
}, [selectable, context.selectable]);

// ======= State: Disabled State =======
const isDisabled = React.useMemo<boolean>(() => {
return !!(context.disabled || props.disabled || unstableContext.nodeDisabled?.(data));
Expand Down Expand Up @@ -86,10 +77,19 @@ const TreeNode: React.FC<Readonly<TreeNodeProps>> = props => {
context.onNodeCheck(e, convertNodePropsToEventData(props), !checked);
};

// ======= State: Selectable Check =======
const isSelectable = React.useMemo<boolean>(() => {
// Ignore when selectable is undefined or null
if (typeof selectable === 'boolean') {
return selectable;
}
return context.selectable;
}, [selectable, context.selectable]);

const onSelectorClick = (e: React.MouseEvent<HTMLSpanElement, MouseEvent>) => {
// Click trigger before select/check operation
context.onNodeClick(e, convertNodePropsToEventData(props));
if (memoizedIsSelectable) {
if (isSelectable) {
onSelect(e);
} else {
onCheck(e);
Expand All @@ -112,6 +112,14 @@ const TreeNode: React.FC<Readonly<TreeNodeProps>> = props => {
context.onNodeContextMenu(e, convertNodePropsToEventData(props));
};

// ======= Drag: Drag Enabled =======
const isDraggable = React.useMemo<boolean>(() => {
return !!(
context.draggable &&
(!context.draggable.nodeDraggable || context.draggable.nodeDraggable(data))
);
}, [context.draggable, data]);

// ======= Drag: Drag Event Handlers =======
const onDragStart = (e: React.DragEvent<HTMLDivElement>) => {
e.stopPropagation();
Expand Down Expand Up @@ -182,14 +190,6 @@ const TreeNode: React.FC<Readonly<TreeNodeProps>> = props => {
);
}, [isLeaf, context.loadData, hasChildren, props.loaded]);

// ============== State: Node State (Open/Close) ==============
const nodeState = React.useMemo<typeof ICON_OPEN | typeof ICON_CLOSE>(() => {
if (memoizedIsLeaf) {
return null;
}
return expanded ? ICON_OPEN : ICON_CLOSE;
}, [memoizedIsLeaf, expanded]);

// Load data to avoid default expanded tree without data
// ============== Effect: Sync Load Data ==============
const syncLoadData = () => {
Expand All @@ -204,17 +204,11 @@ const TreeNode: React.FC<Readonly<TreeNodeProps>> = props => {
}
};

// ============== Effect ==============
React.useEffect(() => {
syncLoadData();
}, [loading, context.loadData, context.onNodeLoad, expanded, memoizedIsLeaf, props]);

const isDraggable = React.useMemo<boolean>(() => {
return !!(
context.draggable &&
(!context.draggable.nodeDraggable || context.draggable.nodeDraggable(data))
);
}, [context.draggable, data]);

// ==================== Render: Drag Handler ====================
const dragHandlerNode = React.useMemo<React.ReactNode>(() => {
if (!context.draggable?.icon) {
Expand Down Expand Up @@ -290,6 +284,14 @@ const TreeNode: React.FC<Readonly<TreeNodeProps>> = props => {
);
}, [isCheckable, checked, halfChecked, isDisabled, props.disableCheckbox, props.title]);

// ============== State: Node State (Open/Close) ==============
const nodeState = React.useMemo<typeof ICON_OPEN | typeof ICON_CLOSE>(() => {
if (memoizedIsLeaf) {
return null;
}
return expanded ? ICON_OPEN : ICON_CLOSE;
}, [memoizedIsLeaf, expanded]);

// ==================== Render: Title + Icon ====================
const iconNode = React.useMemo<React.ReactNode>(() => {
return (
Expand Down