mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-05 13:39:24 +08:00
Merge branch 'main' of github.com:nocobase/nocobase into feat/add-import-export-log
This commit is contained in:
commit
da9bfa0337
@ -1451,6 +1451,7 @@ export const useAssociationFilterBlockProps = () => {
|
|||||||
run,
|
run,
|
||||||
valueKey,
|
valueKey,
|
||||||
labelKey,
|
labelKey,
|
||||||
|
dataScopeFilter: filter,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
async function doReset({
|
async function doReset({
|
||||||
|
@ -61,14 +61,11 @@ export const TableBlockInitializer = ({
|
|||||||
|
|
||||||
export const useCreateTableBlock = () => {
|
export const useCreateTableBlock = () => {
|
||||||
const { insert } = useSchemaInitializer();
|
const { insert } = useSchemaInitializer();
|
||||||
const { getCollection } = useCollectionManager_deprecated();
|
|
||||||
|
|
||||||
const createTableBlock = ({ item }) => {
|
const createTableBlock = ({ item }) => {
|
||||||
const collection = getCollection(item.name, item.dataSource);
|
|
||||||
const schema = createTableBlockUISchema({
|
const schema = createTableBlockUISchema({
|
||||||
collectionName: item.name,
|
collectionName: item.name,
|
||||||
dataSource: item.dataSource,
|
dataSource: item.dataSource,
|
||||||
rowKey: collection.filterTargetKey || 'id',
|
|
||||||
});
|
});
|
||||||
insert(schema);
|
insert(schema);
|
||||||
};
|
};
|
||||||
|
@ -17,7 +17,7 @@ vi.mock('@formily/shared', () => {
|
|||||||
|
|
||||||
describe('createTableBLockSchemaV2', () => {
|
describe('createTableBLockSchemaV2', () => {
|
||||||
it('should create a default table block schema with minimum options', () => {
|
it('should create a default table block schema with minimum options', () => {
|
||||||
const options = { dataSource: 'abc', collectionName: 'users', association: 'users.roles', rowKey: 'rowKey' };
|
const options = { dataSource: 'abc', collectionName: 'users', association: 'users.roles' };
|
||||||
const schema = createTableBlockUISchema(options);
|
const schema = createTableBlockUISchema(options);
|
||||||
|
|
||||||
expect(schema).toMatchInlineSnapshot(`
|
expect(schema).toMatchInlineSnapshot(`
|
||||||
@ -85,7 +85,6 @@ describe('createTableBLockSchemaV2', () => {
|
|||||||
"params": {
|
"params": {
|
||||||
"pageSize": 20,
|
"pageSize": 20,
|
||||||
},
|
},
|
||||||
"rowKey": "rowKey",
|
|
||||||
"showIndex": true,
|
"showIndex": true,
|
||||||
},
|
},
|
||||||
"x-filter-targets": [],
|
"x-filter-targets": [],
|
||||||
|
@ -13,10 +13,9 @@ import { uid } from '@formily/shared';
|
|||||||
export const createTableBlockUISchema = (options: {
|
export const createTableBlockUISchema = (options: {
|
||||||
dataSource: string;
|
dataSource: string;
|
||||||
collectionName?: string;
|
collectionName?: string;
|
||||||
rowKey?: string;
|
|
||||||
association?: string;
|
association?: string;
|
||||||
}): ISchema => {
|
}): ISchema => {
|
||||||
const { collectionName, dataSource, rowKey, association } = options;
|
const { collectionName, dataSource, association } = options;
|
||||||
|
|
||||||
if (!dataSource) {
|
if (!dataSource) {
|
||||||
throw new Error('dataSource is required');
|
throw new Error('dataSource is required');
|
||||||
@ -35,7 +34,6 @@ export const createTableBlockUISchema = (options: {
|
|||||||
params: {
|
params: {
|
||||||
pageSize: 20,
|
pageSize: 20,
|
||||||
},
|
},
|
||||||
rowKey,
|
|
||||||
showIndex: true,
|
showIndex: true,
|
||||||
dragSort: false,
|
dragSort: false,
|
||||||
},
|
},
|
||||||
|
@ -11,15 +11,19 @@ import { useFieldSchema } from '@formily/react';
|
|||||||
import { useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
import { useParsedFilter } from '../../../../../block-provider/hooks/useParsedFilter';
|
import { useParsedFilter } from '../../../../../block-provider/hooks/useParsedFilter';
|
||||||
import { useParentRecordCommon } from '../../../useParentRecordCommon';
|
import { useParentRecordCommon } from '../../../useParentRecordCommon';
|
||||||
|
import { useDataSourceManager } from '../../../../../data-source';
|
||||||
|
|
||||||
export const useTableBlockDecoratorProps = (props) => {
|
export const useTableBlockDecoratorProps = (props) => {
|
||||||
const { params, parseVariableLoading } = useTableBlockParams(props);
|
const { params, parseVariableLoading } = useTableBlockParams(props);
|
||||||
const parentRecord = useParentRecordCommon(props.association);
|
const parentRecord = useParentRecordCommon(props.association);
|
||||||
|
const dm = useDataSourceManager();
|
||||||
|
const collection = dm.getDataSource(props.dataSource)?.collectionManager.getCollection(props.collection);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
params,
|
params,
|
||||||
parentRecord,
|
parentRecord,
|
||||||
parseVariableLoading,
|
parseVariableLoading,
|
||||||
|
rowKey: collection?.filterTargetKey || 'id',
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -41,6 +41,7 @@ export const AssociationFilterItem = withDynamicSchemaProps(
|
|||||||
handleSearchInput: _handleSearchInput,
|
handleSearchInput: _handleSearchInput,
|
||||||
params,
|
params,
|
||||||
run,
|
run,
|
||||||
|
dataScopeFilter,
|
||||||
valueKey: _valueKey,
|
valueKey: _valueKey,
|
||||||
labelKey: _labelKey,
|
labelKey: _labelKey,
|
||||||
defaultCollapse,
|
defaultCollapse,
|
||||||
@ -94,7 +95,7 @@ export const AssociationFilterItem = withDynamicSchemaProps(
|
|||||||
if (searchVisible || filter) {
|
if (searchVisible || filter) {
|
||||||
run({
|
run({
|
||||||
...params?.[0],
|
...params?.[0],
|
||||||
filter: undefined,
|
filter: dataScopeFilter,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
setSearchVisible(!searchVisible);
|
setSearchVisible(!searchVisible);
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
import { CloseCircleOutlined } from '@ant-design/icons';
|
import { CloseCircleOutlined } from '@ant-design/icons';
|
||||||
import { css } from '@emotion/css';
|
import { css } from '@emotion/css';
|
||||||
import { observer } from '@formily/react';
|
import { observer } from '@formily/react';
|
||||||
|
import { sortTree } from '@nocobase/utils/client';
|
||||||
import { Cascader, Select, Space } from 'antd';
|
import { Cascader, Select, Space } from 'antd';
|
||||||
import React, { useCallback, useContext, useMemo } from 'react';
|
import React, { useCallback, useContext, useMemo } from 'react';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
@ -25,7 +26,7 @@ export const FilterItem = observer(
|
|||||||
const remove = useContext(RemoveConditionContext);
|
const remove = useContext(RemoveConditionContext);
|
||||||
const {
|
const {
|
||||||
schema,
|
schema,
|
||||||
fields,
|
fields: _fields,
|
||||||
operators,
|
operators,
|
||||||
dataIndex,
|
dataIndex,
|
||||||
operator,
|
operator,
|
||||||
@ -35,6 +36,7 @@ export const FilterItem = observer(
|
|||||||
setValue,
|
setValue,
|
||||||
collectionField,
|
collectionField,
|
||||||
} = useValues();
|
} = useValues();
|
||||||
|
const fields = sortTree(_fields, 'children', 'children', false);
|
||||||
const style = useMemo(() => ({ marginBottom: 8 }), []);
|
const style = useMemo(() => ({ marginBottom: 8 }), []);
|
||||||
const fieldNames = useMemo(
|
const fieldNames = useMemo(
|
||||||
() => ({
|
() => ({
|
||||||
@ -70,6 +72,12 @@ export const FilterItem = observer(
|
|||||||
className={css`
|
className={css`
|
||||||
width: 160px;
|
width: 160px;
|
||||||
`}
|
`}
|
||||||
|
popupClassName={css`
|
||||||
|
.ant-cascader-menu {
|
||||||
|
height: fit-content;
|
||||||
|
max-height: 50vh;
|
||||||
|
}
|
||||||
|
`}
|
||||||
showSearch
|
showSearch
|
||||||
fieldNames={fieldNames}
|
fieldNames={fieldNames}
|
||||||
changeOnSelect={false}
|
changeOnSelect={false}
|
||||||
|
@ -535,11 +535,19 @@ export const EditOperator = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
field.componentProps = componentProps;
|
field.componentProps = componentProps;
|
||||||
|
fieldSchema['x-component-props'] = componentProps;
|
||||||
|
fieldSchema.default = null;
|
||||||
|
field.value = null;
|
||||||
|
field.initialValue = null;
|
||||||
|
|
||||||
dn.emit('patch', {
|
dn.emit('patch', {
|
||||||
schema: {
|
schema: {
|
||||||
['x-uid']: fieldSchema['x-uid'],
|
['x-uid']: fieldSchema['x-uid'],
|
||||||
['x-component-props']: componentProps,
|
['x-component-props']: componentProps,
|
||||||
['x-filter-operator']: v,
|
['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();
|
dn.refresh();
|
||||||
|
@ -39,7 +39,6 @@ export const RecordAssociationBlockInitializer = () => {
|
|||||||
} else {
|
} else {
|
||||||
insert(
|
insert(
|
||||||
createTableBlockUISchema({
|
createTableBlockUISchema({
|
||||||
rowKey: collection.filterTargetKey,
|
|
||||||
dataSource: collection.dataSource,
|
dataSource: collection.dataSource,
|
||||||
association: association,
|
association: association,
|
||||||
}),
|
}),
|
||||||
@ -62,7 +61,6 @@ export function useCreateAssociationTableBlock() {
|
|||||||
|
|
||||||
insert(
|
insert(
|
||||||
createTableBlockUISchema({
|
createTableBlockUISchema({
|
||||||
rowKey: collection.filterTargetKey,
|
|
||||||
dataSource: collection.dataSource,
|
dataSource: collection.dataSource,
|
||||||
association: `${field.collectionName}.${field.name}`,
|
association: `${field.collectionName}.${field.name}`,
|
||||||
}),
|
}),
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
* For more information, please refer to: https://www.nocobase.com/agreement.
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { hasEmptyValue } from '../client';
|
import { hasEmptyValue, sortTree } from '../client';
|
||||||
|
|
||||||
describe('hasEmptyValue', () => {
|
describe('hasEmptyValue', () => {
|
||||||
it('should return false when there is no empty value', () => {
|
it('should return false when there is no empty value', () => {
|
||||||
@ -80,3 +80,147 @@ describe('hasEmptyValue', () => {
|
|||||||
expect(hasEmptyValue(obj)).toBe(true);
|
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.
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import _ from 'lodash';
|
||||||
|
|
||||||
export const isString = (value: any): value is string => {
|
export const isString = (value: any): value is string => {
|
||||||
return typeof value === 'string';
|
return typeof value === 'string';
|
||||||
};
|
};
|
||||||
@ -56,12 +58,12 @@ export const nextTick = (fn: () => void) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通用树节点深度优先遍历函数
|
* Generic tree node depth-first traversal function
|
||||||
* @param {Object|Array} tree - 要遍历的树结构
|
* @param {Object|Array} tree - The tree structure to traverse
|
||||||
* @param {Function} callback - 遍历每个节点时执行的回调函数,返回真值时停止遍历并返回当前节点
|
* @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 - 配置选项
|
* @param {Object} options - Configuration options
|
||||||
* @param {string|Function} options.childrenKey - 子节点的属性名,默认为'children',也可以是一个函数
|
* @param {string|Function} options.childrenKey - The property name of child nodes, defaults to 'children', can also be a function
|
||||||
* @returns {any|undefined} - 找到的节点或undefined
|
* @returns {any|undefined} - The found node or undefined
|
||||||
*/
|
*/
|
||||||
export function treeFind<T = any>(
|
export function treeFind<T = any>(
|
||||||
tree: T | T[],
|
tree: T | T[],
|
||||||
@ -74,20 +76,20 @@ export function treeFind<T = any>(
|
|||||||
|
|
||||||
const { childrenKey = 'children' } = options;
|
const { childrenKey = 'children' } = options;
|
||||||
|
|
||||||
// 处理根节点是数组的情况
|
// Handle case where the root node is an array
|
||||||
const nodes = Array.isArray(tree) ? [...tree] : [tree];
|
const nodes = Array.isArray(tree) ? [...tree] : [tree];
|
||||||
|
|
||||||
// 深度优先搜索
|
// Depth-first search
|
||||||
for (const node of nodes) {
|
for (const node of nodes) {
|
||||||
// 对当前节点调用回调函数
|
// Call callback function on the current node
|
||||||
if (callback(node)) {
|
if (callback(node)) {
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取子节点
|
// Get child nodes
|
||||||
const children = typeof childrenKey === 'function' ? childrenKey(node) : (node as any)[childrenKey];
|
const children = typeof childrenKey === 'function' ? childrenKey(node) : (node as any)[childrenKey];
|
||||||
|
|
||||||
// 递归处理子节点
|
// Recursively process child nodes
|
||||||
if (Array.isArray(children) && children.length > 0) {
|
if (Array.isArray(children) && children.length > 0) {
|
||||||
const found = treeFind(children, callback, options);
|
const found = treeFind(children, callback, options);
|
||||||
if (found !== undefined) {
|
if (found !== undefined) {
|
||||||
@ -98,3 +100,31 @@ export function treeFind<T = any>(
|
|||||||
|
|
||||||
return undefined;
|
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