Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

feat(design): add `Tree` component (#716)

白熱05d70a437e2a
diff --git a/packages/design/src/components/tree/Tree.stories.tsx b/packages/design/src/components/tree/Tree.stories.tsx
new file mode 100644
index 0000000000..15aec975aa
--- /dev/null
+++ b/packages/design/src/components/tree/Tree.stories.tsx
@@ -0,0 +1,128 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { Meta } from '@storybook/react';
+import React, { useState } from 'react';
+
+import { Tree, TreeSelectionMode } from './Tree';
+
+const meta: Meta<typeof Tree> = {
+ title: 'Components / Tree',
+ component: Tree,
+ parameters: {
+ layout: 'centered',
+ },
+ tags: ['autodocs'],
+};
+
+export default meta;
+
+export const TreeBasic = {
+ render() {
+ const [value, setValue] = useState<string | number | boolean>();
+
+ const data = [
+ {
+ key: '0',
+ title: 'node 0',
+ children: [
+ { key: '0-0', title: 'node 0-0' },
+ { key: '0-1', title: 'node 0-1' },
+ {
+ key: '0-2',
+ title: 'node 0-2',
+ children: [
+ { key: '0-2-0', title: 'node 0-2-0' },
+ { key: '0-2-1', title: 'node 0-2-1' },
+ { key: '0-2-2', title: 'node 0-2-2' },
+ ],
+ },
+ { key: '0-3', title: 'node 0-3' },
+ { key: '0-4', title: 'node 0-4' },
+ { key: '0-5', title: 'node 0-5' },
+ { key: '0-6', title: 'node 0-6' },
+ { key: '0-7', title: 'node 0-7' },
+ { key: '0-8', title: 'node 0-8' },
+ {
+ key: '0-9',
+ title: 'node 0-9',
+ children: [
+ { key: '0-9-0', title: 'node 0-9-0' },
+ {
+ key: '0-9-1',
+ title: 'node 0-9-1',
+ children: [
+ { key: '0-9-1-0', title: 'node 0-9-1-0' },
+ { key: '0-9-1-1', title: 'node 0-9-1-1' },
+ { key: '0-9-1-2', title: 'node 0-9-1-2' },
+ { key: '0-9-1-3', title: 'node 0-9-1-3' },
+ { key: '0-9-1-4', title: 'node 0-9-1-4' },
+ ],
+ },
+ {
+ key: '0-9-2',
+ title: 'node 0-9-2',
+ children: [
+ { key: '0-9-2-0', title: 'node 0-9-2-0' },
+ { key: '0-9-2-1', title: 'node 0-9-2-1' },
+ ],
+ },
+ ],
+ },
+ ],
+ },
+ {
+ key: '1',
+ title: 'node 1',
+ // children: new Array(1000)
+ // .fill(null)
+ // .map((_, index) => ({ title: `auto ${index}`, key: `auto-${index}` })),
+ children: [
+ {
+ key: '1-0',
+ title: 'node 1-0',
+ children: [
+ { key: '1-0-0', title: 'node 1-0-0' },
+ {
+ key: '1-0-1',
+ title: 'node 1-0-1',
+ children: [
+ { key: '1-0-1-0', title: 'node 1-0-1-0' },
+ { key: '1-0-1-1', title: 'node 1-0-1-1' },
+ ],
+ },
+ { key: '1-0-2', title: 'node 1-0-2' },
+ ],
+ },
+ ],
+ },
+ ];
+
+ function handleChange(value: string | number | boolean) {
+ setValue(value);
+ }
+
+ return (
+ <Tree
+ data={data}
+ defaultExpandAll
+ selectionMode={TreeSelectionMode.ONLY_LEAF_NODE}
+ value={value}
+ onChange={handleChange}
+ />
+ );
+ },
+};
diff --git a/packages/design/src/components/tree/Tree.tsx b/packages/design/src/components/tree/Tree.tsx
new file mode 100644
index 0000000000..51b6881149
--- /dev/null
+++ b/packages/design/src/components/tree/Tree.tsx
@@ -0,0 +1,168 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { DropdownSingle } from '@univerjs/icons';
+import clsx from 'clsx';
+import React, { useEffect, useMemo, useState } from 'react';
+
+import styles from './index.module.less';
+
+export enum TreeSelectionMode {
+ ONLY_LEAF_NODE,
+
+ ALL,
+}
+
+interface ITreeNodeProps {
+ title: string;
+ key: string;
+ children?: ITreeNodeProps[];
+}
+
+export interface ITreeProps {
+ data?: ITreeNodeProps[];
+
+ /**
+ * Set whether to expand all nodes by default
+ * @default false
+ */
+ defaultExpandAll?: boolean;
+
+ /**
+ * Set the selection mode
+ * @default TreeSelectionMode.ALL
+ * @description
+ * - `TreeSelectionMode.ONLY_LEAF_NODE` only select leaf node
+ * - `TreeSelectionMode.ALL` select all node
+ * */
+ selectionMode?: TreeSelectionMode;
+
+ /**
+ * Used for setting the currently selected value
+ */
+ value?: string | number | boolean;
+
+ /**
+ * Set the handler to handle `click` event
+ */
+ onChange?: (value: string | number | boolean) => void;
+}
+
+type TreeItemProps = ITreeNodeProps & {
+ _selected?: boolean;
+ _expand?: boolean;
+};
+
+/**
+ * Tree Component
+ */
+export function Tree(props: ITreeProps) {
+ const { data = [], defaultExpandAll = false, selectionMode = TreeSelectionMode.ALL, value, onChange } = props;
+
+ const [expandKeys, setExpandKeys] = useState<Array<string | number | boolean>>([]);
+
+ useEffect(() => {
+ function walkData(item: ITreeNodeProps) {
+ setExpandKeys((prev) => [...prev, item.key]);
+
+ item.children?.forEach(walkData);
+ }
+ if (defaultExpandAll) {
+ data.forEach(walkData);
+ }
+ }, [defaultExpandAll, data]);
+
+ const computedData = useMemo(() => {
+ return data.map(function walkData(item): TreeItemProps {
+ const { title, key, children } = item;
+
+ return {
+ title,
+ key,
+ children: children && children.map(walkData),
+ _selected: key === value,
+ _expand: expandKeys.includes(key),
+ };
+ });
+ }, [value, expandKeys]);
+
+ function handleSelectItem(treeItem: ITreeNodeProps) {
+ if (treeItem.children) {
+ setExpandKeys((prev) => {
+ const index = prev.findIndex((key) => key === treeItem.key);
+
+ if (index === -1) {
+ return [...prev, treeItem.key];
+ }
+
+ return [...prev.slice(0, index), ...prev.slice(index + 1)];
+ });
+ }
+
+ if (selectionMode === TreeSelectionMode.ONLY_LEAF_NODE) {
+ if (treeItem.children) {
+ return;
+ }
+ }
+
+ onChange?.(treeItem.key);
+ }
+
+ function walkTree(treeItem: TreeItemProps) {
+ const { title, key, children, _selected, _expand } = treeItem;
+
+ return (
+ <li
+ key={key}
+ className={styles.treeListItem}
+ onClick={(e) => {
+ e.stopPropagation();
+ handleSelectItem(treeItem);
+ }}
+ >
+ <a
+ className={clsx(styles.treeListItemContent, {
+ [styles.treeListItemContentSelected]: _selected,
+ })}
+ >
+ <span
+ className={clsx(styles.treeIcon, {
+ [styles.treeIconExpand]: _expand,
+ })}
+ >
+ {children && <DropdownSingle />}
+ </span>
+ <span>{title}</span>
+ </a>
+ {children && (
+ <ul
+ className={clsx(styles.treeList, {
+ [styles.treeListExpand]: _expand,
+ })}
+ >
+ {children.map((item) => walkTree(item))}
+ </ul>
+ )}
+ </li>
+ );
+ }
+
+ return (
+ <section className={styles.tree}>
+ <ul className={styles.treeList}>{computedData.map((item) => walkTree(item))}</ul>
+ </section>
+ );
+}
diff --git a/packages/design/src/components/tree/__tests__/index.spec.tsx b/packages/design/src/components/tree/__tests__/index.spec.tsx
new file mode 100644
index 0000000000..1bbdea4b6c
--- /dev/null
+++ b/packages/design/src/components/tree/__tests__/index.spec.tsx
@@ -0,0 +1,43 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { render } from '@testing-library/react';
+import React from 'react';
+import { describe, expect, test } from 'vitest';
+
+import { Tree } from '../Tree';
+
+describe('Tree', () => {
+ test('defaultExpandAll', async () => {
+ const { container } = render(
+ <Tree
+ data={[
+ {
+ key: '0',
+ title: 'node 0',
+ children: [
+ { key: '0-0', title: 'node 0-0' },
+ { key: '0-1', title: 'node 0-1' },
+ ],
+ },
+ ]}
+ defaultExpandAll
+ />
+ );
+
+ expect(container);
+ });
+});
diff --git a/packages/design/src/components/tree/index.module.less b/packages/design/src/components/tree/index.module.less
new file mode 100644
index 0000000000..5376c6aab4
--- /dev/null
+++ b/packages/design/src/components/tree/index.module.less
@@ -0,0 +1,53 @@
+.tree {
+ position: relative;
+ color: rgb(var(--text-color));
+
+ &-icon {
+ position: absolute;
+ top: 7px;
+ left: 0;
+ transform: rotate(-90deg);
+
+ margin-right: 8px;
+
+ font-size: 8px;
+
+ &-expand {
+ transform: rotate(0deg);
+ }
+ }
+
+ &-list {
+ margin: 0;
+ padding: 0;
+ list-style: none;
+
+ .tree-list {
+ overflow: hidden;
+ height: 0;
+
+ &-expand {
+ height: inherit;
+ }
+ }
+
+ &-item {
+ position: relative;
+ padding-left: var(--padding-lg);
+
+ > a {
+ cursor: pointer;
+ display: block;
+ }
+
+ &-content {
+ margin-bottom: 8px;
+
+ &-selected {
+ font-weight: 500;
+ color: rgb(var(--primary-color));
+ }
+ }
+ }
+ }
+}
diff --git a/packages/design/src/components/tree/index.ts b/packages/design/src/components/tree/index.ts
new file mode 100644
index 0000000000..31029b0937
--- /dev/null
+++ b/packages/design/src/components/tree/index.ts
@@ -0,0 +1,17 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+export { type ITreeProps, Tree, TreeSelectionMode } from './Tree';
diff --git a/packages/design/src/index.ts b/packages/design/src/index.ts
index 2ed51478db..27bddc2f1b 100644
--- a/packages/design/src/index.ts
+++ b/packages/design/src/index.ts
@@ -37,6 +37,7 @@ export { type ISelectProps, Select } from './components/select';
export { type ISelectListProps, SelectList } from './components/select-list';
export { type ISliderProps, Slider } from './components/slider';
export { type ITooltipProps, Tooltip } from './components/tooltip';
+export { type ITreeProps, Tree, TreeSelectionMode } from './components/tree';
export { enUS, zhCN } from './locale';
export { type ILocale } from './locale/interface';
export { defaultTheme, greenTheme, themeInstance } from './themes';
diff --git a/packages/local-save/package.json b/packages/local-save/package.json
index 278be6b366..0fbb32709e 100644
--- a/packages/local-save/package.json
+++ b/packages/local-save/package.json
@@ -39,7 +39,7 @@
"sideEffects": [
"**/*.css"
],
- "private": false,
+ "private": true,
"scripts": {
"test": "vitest run",
"test:watch": "vitest",