From 4f1e3df6630818336197262baafc4b62715f1061 Mon Sep 17 00:00:00 2001 From: Zeke Zhang <958414905@qq.com> Date: Mon, 28 Apr 2025 14:11:46 +0800 Subject: [PATCH] chore(Filter): improve user experience (#6779) --- .../antd/filter/FilterItem.tsx | 10 +- .../core/utils/src/__tests__/common.test.ts | 146 +++++++++++++++++- packages/core/utils/src/common.ts | 52 +++++-- 3 files changed, 195 insertions(+), 13 deletions(-) diff --git a/packages/core/client/src/schema-component/antd/filter/FilterItem.tsx b/packages/core/client/src/schema-component/antd/filter/FilterItem.tsx index 88fbe23505..3da02019ea 100644 --- a/packages/core/client/src/schema-component/antd/filter/FilterItem.tsx +++ b/packages/core/client/src/schema-component/antd/filter/FilterItem.tsx @@ -10,6 +10,7 @@ import { CloseCircleOutlined } from '@ant-design/icons'; import { css } from '@emotion/css'; import { observer } from '@formily/react'; +import { sortTree } from '@nocobase/utils/client'; import { Cascader, Select, Space } from 'antd'; import React, { useCallback, useContext, useMemo } from 'react'; import { useTranslation } from 'react-i18next'; @@ -25,7 +26,7 @@ export const FilterItem = observer( const remove = useContext(RemoveConditionContext); const { schema, - fields, + fields: _fields, operators, dataIndex, operator, @@ -35,6 +36,7 @@ export const FilterItem = observer( setValue, collectionField, } = useValues(); + const fields = sortTree(_fields, 'children', 'children', false); const style = useMemo(() => ({ marginBottom: 8 }), []); const fieldNames = useMemo( () => ({ @@ -70,6 +72,12 @@ export const FilterItem = observer( className={css` width: 160px; `} + popupClassName={css` + .ant-cascader-menu { + height: fit-content; + max-height: 50vh; + } + `} showSearch fieldNames={fieldNames} changeOnSelect={false} diff --git a/packages/core/utils/src/__tests__/common.test.ts b/packages/core/utils/src/__tests__/common.test.ts index 1768547f79..6bcc3da6a4 100644 --- a/packages/core/utils/src/__tests__/common.test.ts +++ b/packages/core/utils/src/__tests__/common.test.ts @@ -7,7 +7,7 @@ * For more information, please refer to: https://www.nocobase.com/agreement. */ -import { hasEmptyValue } from '../client'; +import { hasEmptyValue, sortTree } from '../client'; describe('hasEmptyValue', () => { it('should return false when there is no empty value', () => { @@ -80,3 +80,147 @@ describe('hasEmptyValue', () => { expect(hasEmptyValue(obj)).toBe(true); }); }); + +describe('sortTree', () => { + it('should return the original tree when tree is empty', () => { + expect(sortTree(null, 'order')).toBeNull(); + expect(sortTree([], 'order')).toEqual([]); + }); + + it('should sort tree nodes by a specific field in ascending order', () => { + const tree = [ + { id: 3, name: 'C', children: [] }, + { id: 1, name: 'A', children: [] }, + { id: 2, name: 'B', children: [] }, + ]; + + const result = sortTree(tree, 'id'); + + expect(result).toEqual([ + { id: 1, name: 'A', children: [] }, + { id: 2, name: 'B', children: [] }, + { id: 3, name: 'C', children: [] }, + ]); + }); + + it('should sort tree nodes by a specific field in descending order', () => { + const tree = [ + { id: 1, name: 'A', children: [] }, + { id: 3, name: 'C', children: [] }, + { id: 2, name: 'B', children: [] }, + ]; + + const result = sortTree(tree, 'id', 'children', false); + + expect(result).toEqual([ + { id: 3, name: 'C', children: [] }, + { id: 2, name: 'B', children: [] }, + { id: 1, name: 'A', children: [] }, + ]); + }); + + it('should sort tree nodes with nested children', () => { + const tree = [ + { + id: 3, + name: 'C', + items: [ + { id: 2, name: 'C-2' }, + { id: 1, name: 'C-1' }, + ], + }, + { id: 1, name: 'A', items: [] }, + { + id: 2, + name: 'B', + items: [ + { id: 3, name: 'B-3' }, + { id: 1, name: 'B-1' }, + ], + }, + ]; + + const result = sortTree(tree, 'id', 'items'); + + expect(result).toEqual([ + { id: 1, name: 'A', items: [] }, + { + id: 2, + name: 'B', + items: [ + { id: 1, name: 'B-1' }, + { id: 3, name: 'B-3' }, + ], + }, + { + id: 3, + name: 'C', + items: [ + { id: 1, name: 'C-1' }, + { id: 2, name: 'C-2' }, + ], + }, + ]); + }); + + it('should support sorting by function', () => { + const tree = [ + { id: 3, name: 'C', children: [] }, + { id: 1, name: 'A', children: [] }, + { id: 2, name: 'B', children: [] }, + ]; + + const sortByName = (node) => node.name; + const result = sortTree(tree, sortByName); + + expect(result).toEqual([ + { id: 1, name: 'A', children: [] }, + { id: 2, name: 'B', children: [] }, + { id: 3, name: 'C', children: [] }, + ]); + }); + + it('should handle complex nested structures', () => { + const tree = [ + { + id: 2, + name: 'B', + children: [ + { + id: 3, + name: 'B-3', + children: [ + { id: 2, name: 'B-3-2' }, + { id: 1, name: 'B-3-1' }, + ], + }, + { id: 1, name: 'B-1', children: [] }, + ], + }, + { id: 3, name: 'C', children: [] }, + { id: 1, name: 'A', children: [] }, + ]; + + const result = sortTree(tree, 'id'); + + expect(result).toEqual([ + { id: 1, name: 'A', children: [] }, + { + id: 2, + name: 'B', + children: [ + { id: 1, name: 'B-1', children: [] }, + { + id: 3, + name: 'B-3', + children: [ + { id: 1, name: 'B-3-1' }, + { id: 2, name: 'B-3-2' }, + ], + }, + ], + }, + { id: 3, name: 'C', children: [] }, + ]); + }); +}); diff --git a/packages/core/utils/src/common.ts b/packages/core/utils/src/common.ts index f6e9aef0b7..374b4d1cde 100644 --- a/packages/core/utils/src/common.ts +++ b/packages/core/utils/src/common.ts @@ -7,6 +7,8 @@ * For more information, please refer to: https://www.nocobase.com/agreement. */ +import _ from 'lodash'; + export const isString = (value: any): value is string => { return typeof value === 'string'; }; @@ -56,12 +58,12 @@ export const nextTick = (fn: () => void) => { }; /** - * 通用树节点深度优先遍历函数 - * @param {Object|Array} tree - 要遍历的树结构 - * @param {Function} callback - 遍历每个节点时执行的回调函数,返回真值时停止遍历并返回当前节点 - * @param {Object} options - 配置选项 - * @param {string|Function} options.childrenKey - 子节点的属性名,默认为'children',也可以是一个函数 - * @returns {any|undefined} - 找到的节点或undefined + * Generic tree node depth-first traversal function + * @param {Object|Array} tree - The tree structure to traverse + * @param {Function} callback - The callback function executed for each node, stops traversing and returns the current node when a truthy value is returned + * @param {Object} options - Configuration options + * @param {string|Function} options.childrenKey - The property name of child nodes, defaults to 'children', can also be a function + * @returns {any|undefined} - The found node or undefined */ export function treeFind( tree: T | T[], @@ -74,20 +76,20 @@ export function treeFind( const { childrenKey = 'children' } = options; - // 处理根节点是数组的情况 + // Handle case where the root node is an array const nodes = Array.isArray(tree) ? [...tree] : [tree]; - // 深度优先搜索 + // Depth-first search for (const node of nodes) { - // 对当前节点调用回调函数 + // Call callback function on the current node if (callback(node)) { return node; } - // 获取子节点 + // Get child nodes const children = typeof childrenKey === 'function' ? childrenKey(node) : (node as any)[childrenKey]; - // 递归处理子节点 + // Recursively process child nodes if (Array.isArray(children) && children.length > 0) { const found = treeFind(children, callback, options); if (found !== undefined) { @@ -98,3 +100,31 @@ export function treeFind( return undefined; } + +/** + * Sort a tree structure + * @param {Array} tree - Tree structure array + * @param {string|Function} sortBy - Sort field or sort function + * @param {string} childrenKey - The key name of child nodes, defaults to 'children' + * @param {boolean} isAsc - Whether to sort in ascending order, defaults to true + * @returns {Array} - The sorted tree structure + */ +export function sortTree(tree: any[], sortBy: string | Function, childrenKey = 'children', isAsc = true) { + if (!tree || !Array.isArray(tree) || tree.length === 0) { + return tree; + } + + // Sort nodes at the current level + const sortedTree = _.orderBy(tree, sortBy, isAsc ? 'asc' : 'desc'); + + // Recursively sort child nodes + return sortedTree.map((node) => { + if (node[childrenKey] && node[childrenKey].length > 0) { + return { + ...node, + [childrenKey]: sortTree(node[childrenKey], sortBy, childrenKey, isAsc), + }; + } + return node; + }); +}