mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-05 05:29:26 +08:00
Merge branch 'main' into next
This commit is contained in:
commit
20c07d9947
@ -1508,6 +1508,7 @@ export const useAssociationFilterBlockProps = () => {
|
||||
run,
|
||||
valueKey,
|
||||
labelKey,
|
||||
dataScopeFilter: filter,
|
||||
};
|
||||
};
|
||||
async function doReset({
|
||||
|
@ -41,6 +41,7 @@ export const AssociationFilterItem = withDynamicSchemaProps(
|
||||
handleSearchInput: _handleSearchInput,
|
||||
params,
|
||||
run,
|
||||
dataScopeFilter,
|
||||
valueKey: _valueKey,
|
||||
labelKey: _labelKey,
|
||||
defaultCollapse,
|
||||
@ -94,7 +95,7 @@ export const AssociationFilterItem = withDynamicSchemaProps(
|
||||
if (searchVisible || filter) {
|
||||
run({
|
||||
...params?.[0],
|
||||
filter: undefined,
|
||||
filter: dataScopeFilter,
|
||||
});
|
||||
}
|
||||
setSearchVisible(!searchVisible);
|
||||
|
@ -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, useState } 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,7 +36,8 @@ export const FilterItem = observer(
|
||||
setValue,
|
||||
collectionField,
|
||||
} = useValues();
|
||||
const [options, setOptions] = useState(compile(fields));
|
||||
const fields = useMemo(() => sortTree(_fields, 'children', 'children', false), [_fields]);
|
||||
const [options, setOptions] = useState(() => compile(fields));
|
||||
const style = useMemo(() => ({ marginBottom: 8 }), []);
|
||||
const fieldNames = useMemo(
|
||||
() => ({
|
||||
@ -84,6 +86,12 @@ export const FilterItem = observer(
|
||||
className={css`
|
||||
width: 160px;
|
||||
`}
|
||||
popupClassName={css`
|
||||
.ant-cascader-menu {
|
||||
height: fit-content;
|
||||
max-height: 50vh;
|
||||
}
|
||||
`}
|
||||
allowClear
|
||||
fieldNames={fieldNames}
|
||||
value={dataIndex}
|
||||
|
@ -535,11 +535,19 @@ export const EditOperator = () => {
|
||||
}
|
||||
|
||||
field.componentProps = componentProps;
|
||||
fieldSchema['x-component-props'] = componentProps;
|
||||
fieldSchema.default = null;
|
||||
field.value = null;
|
||||
field.initialValue = null;
|
||||
|
||||
dn.emit('patch', {
|
||||
schema: {
|
||||
['x-uid']: fieldSchema['x-uid'],
|
||||
['x-component-props']: componentProps,
|
||||
['x-filter-operator']: v,
|
||||
// Clear default value when switching operators. Some operators require the default value to be an array,
|
||||
// while others don't. Without clearing it, the filtering API would throw an error
|
||||
default: null,
|
||||
},
|
||||
});
|
||||
dn.refresh();
|
||||
|
@ -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: [] },
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
@ -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;
|
||||
});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user