mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 07:57:20 +00:00
fix(client): fix the issue where setting default values using variabl… (#4521)
* fix(client): fix the issue where setting default values using variables in the sub-table is not work * test: add unit tests
This commit is contained in:
parent
77e0ff5ccc
commit
b43f896882
@ -7,7 +7,8 @@
|
||||
* For more information, please refer to: https://www.nocobase.com/agreement.
|
||||
*/
|
||||
|
||||
import { transformValue } from '../../hooks/useSpecialCase';
|
||||
import { vi } from 'vitest';
|
||||
import { isSpecialCaseField, transformValue } from '../../hooks/useSpecialCase';
|
||||
|
||||
describe('transformValue', () => {
|
||||
it('value is an array', () => {
|
||||
@ -40,3 +41,90 @@ describe('transformValue', () => {
|
||||
`);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isSpecialCaseField', () => {
|
||||
it('should return false for field with default value including "$iteration"', () => {
|
||||
const collectionField = {
|
||||
type: 'hasOne',
|
||||
};
|
||||
const parentFieldSchema = {
|
||||
'x-collection-field': 'parentField',
|
||||
'x-component-props': {
|
||||
mode: 'SubTable',
|
||||
},
|
||||
};
|
||||
const fieldSchema: any = {
|
||||
default: '{{ $iteration.name }}',
|
||||
parent: parentFieldSchema,
|
||||
};
|
||||
|
||||
const getCollectionField = vi.fn().mockReturnValue({
|
||||
type: 'hasMany',
|
||||
});
|
||||
|
||||
const result = isSpecialCaseField({ collectionField, fieldSchema, getCollectionField });
|
||||
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false for field with collectionField type "hasOne" or "belongsTo" and no parent field with type "hasMany" or "belongsToMany"', () => {
|
||||
const collectionField = {
|
||||
type: 'hasOne',
|
||||
};
|
||||
const fieldSchema: any = {
|
||||
default: '',
|
||||
};
|
||||
const getCollectionField = vi.fn().mockReturnValue(null);
|
||||
|
||||
const result = isSpecialCaseField({ collectionField, fieldSchema, getCollectionField });
|
||||
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it('should return true for field with collectionField type "hasOne" or "belongsTo" and parent field with type "hasMany"', () => {
|
||||
const collectionField = {
|
||||
type: 'hasOne',
|
||||
};
|
||||
const parentFieldSchema = {
|
||||
'x-collection-field': 'parentField',
|
||||
'x-component-props': {
|
||||
mode: 'SubTable',
|
||||
},
|
||||
};
|
||||
const fieldSchema: any = {
|
||||
default: '',
|
||||
parent: parentFieldSchema,
|
||||
};
|
||||
|
||||
const getCollectionField = vi.fn().mockReturnValue({
|
||||
type: 'hasMany',
|
||||
});
|
||||
|
||||
const result = isSpecialCaseField({ collectionField, fieldSchema, getCollectionField });
|
||||
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it('should return true for field with collectionField type "hasOne" or "belongsTo" and parent field with type "belongsToMany"', () => {
|
||||
const collectionField = {
|
||||
type: 'hasOne',
|
||||
};
|
||||
const parentFieldSchema = {
|
||||
'x-collection-field': 'parentField',
|
||||
'x-component-props': {
|
||||
mode: 'SubTable',
|
||||
},
|
||||
};
|
||||
const fieldSchema: any = {
|
||||
default: '',
|
||||
parent: parentFieldSchema,
|
||||
};
|
||||
const getCollectionField = vi.fn().mockReturnValue({
|
||||
type: 'belongsToMany',
|
||||
});
|
||||
|
||||
const result = isSpecialCaseField({ collectionField, fieldSchema, getCollectionField });
|
||||
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
});
|
||||
|
@ -13,11 +13,11 @@ import _ from 'lodash';
|
||||
import { useCallback, useEffect, useMemo } from 'react';
|
||||
import {
|
||||
CollectionFieldOptions_deprecated,
|
||||
useCollection_deprecated,
|
||||
useCollectionManager_deprecated,
|
||||
useCollection_deprecated,
|
||||
} from '../../../../collection-manager';
|
||||
import { isSubMode } from '../../association-field/util';
|
||||
import { markRecordAsNew } from '../../../../data-source/collection-record/isNewRecord';
|
||||
import { isSubMode } from '../../association-field/util';
|
||||
|
||||
/**
|
||||
* #### 处理 `子表单` 和 `子表格` 中的特殊情况
|
||||
@ -92,6 +92,11 @@ export function isSpecialCaseField({
|
||||
fieldSchema: Schema;
|
||||
getCollectionField: (name: string) => CollectionFieldOptions_deprecated;
|
||||
}) {
|
||||
// 排除掉“当前对象”这个变量
|
||||
if (fieldSchema.default.includes('$iteration')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (collectionField && ['hasOne', 'belongsTo'].includes(collectionField.type) && fieldSchema) {
|
||||
const parentFieldSchema = getParentFieldSchema(fieldSchema);
|
||||
if (parentFieldSchema && parentFieldSchema['x-collection-field']) {
|
||||
|
Loading…
Reference in New Issue
Block a user