chore(Filter): improve user experience (#6779)

This commit is contained in:
Zeke Zhang 2025-04-28 14:11:46 +08:00 committed by GitHub
parent 281b813f0c
commit 4f1e3df663
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 195 additions and 13 deletions

View File

@ -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}

View File

@ -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: [] },
]);
});
});

View File

@ -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<T = any>(
tree: T | T[],
@ -74,20 +76,20 @@ export function treeFind<T = any>(
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<T = any>(
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;
});
}