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:
Zeke Zhang 2024-05-29 17:27:29 +08:00 committed by GitHub
parent 77e0ff5ccc
commit b43f896882
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 96 additions and 3 deletions

View File

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

View File

@ -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']) {