Skip to content

Commit

Permalink
🐛 fix(sortable-tree): 修正 SortableTree 在 server 端的兼容性问题
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Jun 18, 2023
1 parent a423934 commit 8570903
Showing 1 changed file with 6 additions and 19 deletions.
25 changes: 6 additions & 19 deletions src/SortableTree/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { arrayMove } from '@dnd-kit/sortable';
import type { FlattenNode, TreeData, TreeNode } from '../types';
import { flattenTree } from './treeNode';

export const iOS = /iPad|iPhone|iPod/.test(navigator.platform);
export const iOS = /iPad|iPhone|iPod/.test(navigator?.platform);

function getDragDepth(offset: number, indentationWidth: number) {
return Math.round(offset / indentationWidth);
Expand Down Expand Up @@ -77,10 +77,7 @@ function getMinDepth({ nextItem }: { nextItem: FlattenNode }) {
return 0;
}

export function findItemDeep(
items: TreeData,
itemId: UniqueIdentifier,
): TreeNode | undefined {
export function findItemDeep(items: TreeData, itemId: UniqueIdentifier): TreeNode | undefined {
for (const item of items) {
const { id, children } = item;

Expand Down Expand Up @@ -116,10 +113,7 @@ export function getChildCount(items: TreeData, id: UniqueIdentifier) {
return item ? countChildren(item.children) : 0;
}

export function removeChildrenOf(
items: FlattenNode[],
ids: UniqueIdentifier[],
) {
export function removeChildrenOf(items: FlattenNode[], ids: UniqueIdentifier[]) {
const excludeParentIds = [...ids];

return items.filter((item) => {
Expand All @@ -134,19 +128,12 @@ export function removeChildrenOf(
});
}

export const getFlattenedData = (
treeData: TreeData,
activeId: UniqueIdentifier,
) => {
export const getFlattenedData = (treeData: TreeData, activeId: UniqueIdentifier) => {
const flattenedTree = flattenTree(treeData);
const collapsedItems = flattenedTree.reduce<UniqueIdentifier[]>(
(acc, { children, collapsed, id }) =>
collapsed && children.length ? [...acc, id] : acc,
(acc, { children, collapsed, id }) => (collapsed && children.length ? [...acc, id] : acc),
[],
);

return removeChildrenOf(
flattenedTree,
activeId ? [activeId, ...collapsedItems] : collapsedItems,
);
return removeChildrenOf(flattenedTree, activeId ? [activeId, ...collapsedItems] : collapsedItems);
};

0 comments on commit 8570903

Please sign in to comment.