Skip to content

Commit

Permalink
✨ feat: add vertual list"
Browse files Browse the repository at this point in the history
  • Loading branch information
rdmclin2 committed Dec 30, 2023
1 parent 6fa373f commit c8e7b76
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 32 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@
"react-layout-kit": "^1.7.4",
"react-markdown": "^8.0.7",
"react-rnd": "^10.4.1",
"react-virtualized-auto-sizer": "^1.0.20",
"react-window": "^1.8.10",
"reactflow": "^11.10.1",
"rehype-katex": "^6.0.3",
"remark-gfm": "^3.0.1",
Expand All @@ -129,6 +131,7 @@
"@types/json-schema": "^7.0.15",
"@types/react": "^18.2.45",
"@types/react-dom": "^18.2.18",
"@types/react-window": "^1.8.8",
"@umijs/lint": "^4.0.90",
"@vitest/coverage-v8": "latest",
"antd": "^5.12.5",
Expand Down
6 changes: 6 additions & 0 deletions src/SortableTree/components/TreeItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ export interface TreeItemProps extends Omit<HTMLAttributes<HTMLLIElement>, 'id'>
* @title 样式类名前缀
*/
prefixCls: string;
/**
* 虚拟滚动添加样式
*/
virtualStyle?: CSSProperties;
}

const animateLayoutChanges: AnimateLayoutChanges = ({ isSorting, wasDragging }) =>
Expand All @@ -134,6 +138,7 @@ const TreeItem: FC<TreeItemProps> = memo(
hideRemove,
node,
prefixCls,
virtualStyle,
...props
}) => {
const prefix = `${prefixCls}-node`;
Expand Down Expand Up @@ -200,6 +205,7 @@ const TreeItem: FC<TreeItemProps> = memo(

transform: CSS.Translate.toString(transform),
transition,
...virtualStyle,
} as CSSProperties
}
onClick={(e) => {
Expand Down
36 changes: 10 additions & 26 deletions src/SortableTree/demos/virtual.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
import type { TreeData } from '@ant-design/pro-editor';
import { SortableTree } from '@ant-design/pro-editor';
import React from 'react';

interface DataContent {
name: string;
visible: boolean;
isLeaf: boolean;
}

interface IContentProps {
content: any;
id: string;
setCheckedId: any;
checked: boolean;
}

interface IProps {
layer: TreeData;
setLayer: any;
onLeafMove?: (leaf: any, layer: any) => void;
onLeafDelete?: (leaf: any) => void;
onLeafLock?: (leaf: any, locked: boolean) => void;
}

const LayerManager = (props: IProps) => {
const LayerManager = () => {
const treeData = [
{
id: '33',
Expand Down Expand Up @@ -3966,15 +3949,16 @@ const LayerManager = (props: IProps) => {
],
},
];
console.log('treeData:11 ', treeData);

return (
<div style={{ width: 275, padding: '0 12px', height: 800 }}>
<SortableTree<DataContent>
treeData={treeData as any}
renderContent={(item) => <div>{item.id}</div>}
/>
</div>
<SortableTree<DataContent>
treeData={treeData as any}
renderContent={(item) => <div>{item.id}</div>}
SHOW_STORE_IN_DEVTOOLS
onTreeDataChange={(data) => {
console.log('变更:', data);
}}
/>
);
};
export default LayerManager;
export default LayerManager;
27 changes: 21 additions & 6 deletions src/SortableTree/features/TreeList.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import { createStyles, cx, getStudioStylish } from '@/theme';
import { PlusOutlined } from '@ant-design/icons';
import { useMemoizedFn } from 'ahooks';
import { Button } from 'antd';
import isEqual from 'lodash.isequal';
import { memo } from 'react';
import { VariableSizeList } from 'react-window';
import { shallow } from 'zustand/shallow';

import { SortableTreeItem } from '../components';
import { dataFlattenSelector, projectedSelector, useStore } from '../store';

import { genUniqueId } from '../../utils';
import { genUniqueId } from '@/utils';
import { PlusOutlined } from '@ant-design/icons';
import { Button } from 'antd';
import type { FlattenNode } from '../types';

interface TreeNodeProps {
prefixCls: string;
node: FlattenNode;
virtualStyle?: React.CSSProperties;
}

const TreeNode = memo<TreeNodeProps>(({ prefixCls, node }) => {
const TreeNode = memo<TreeNodeProps>(({ prefixCls, node, virtualStyle }) => {
const [activeId, indentationWidth, dispatchTreeData, hideRemove] = useStore(
(s) => [s.activeId, s.indentationWidth, s.dispatchTreeData, s.hideRemove],
shallow,
Expand All @@ -44,6 +46,7 @@ const TreeNode = memo<TreeNodeProps>(({ prefixCls, node }) => {
children.length ? () => dispatchTreeData({ type: 'toggleCollapse', id }) : undefined
}
node={node}
virtualStyle={virtualStyle}
onRemove={onRemove}
/>
);
Expand Down Expand Up @@ -81,9 +84,21 @@ const TreeList = memo<TreeListProps>(({ prefixCls }) => {

return (
<>
{flattenData.map((node) => (
<VariableSizeList
itemCount={flattenData!.length}
height={800}
itemSize={() => 36}
itemData={flattenData}
width={275}
>
{({ index, data, style }) => {
return <TreeNode prefixCls={prefixCls} node={data[index]} virtualStyle={style} />;
}}
</VariableSizeList>

{/* {flattenData.map((node) => (
<TreeNode key={node.id} node={node} prefixCls={prefixCls} />
))}
))} */}
{hideAdd ? null : (
<Button
block
Expand Down
1 change: 1 addition & 0 deletions src/SortableTree/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ It can be used when editing tree-like data structures (adding, deleting, sorting
<code src="./demos/default.tsx" ></code> <code src="./demos/controlled.tsx" ></code>

<code src="./demos/renderContent.tsx" ></code> <code src="./demos/disableDrag.tsx" ></code> <code src="./demos/sortableRule.tsx" ></code>
<code src="./demos/virtual.tsx" ></code>

## API

Expand Down

0 comments on commit c8e7b76

Please sign in to comment.