mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-09 15:39:24 +08:00
fix(core): support selecting the first level of variables as the default value (#4439)
This commit is contained in:
parent
3f14a9b24d
commit
cedb5046dd
@ -10,7 +10,8 @@
|
||||
import { ISchema, Schema } from '@formily/json-schema';
|
||||
import { Tooltip } from 'antd';
|
||||
import React, { useContext, useMemo } from 'react';
|
||||
import { CollectionFieldOptions_deprecated, useCollectionManager_deprecated } from '../../../collection-manager';
|
||||
import { CollectionFieldOptions_deprecated } from '../../../collection-manager';
|
||||
import { useCollectionManager } from '../../../data-source/collection/CollectionManagerProvider';
|
||||
import { useCompile, useGetFilterOptions } from '../../../schema-component';
|
||||
import { isSpecialCaseField } from '../../../schema-component/antd/form-item/hooks/useSpecialCase';
|
||||
import { FieldOption, Option } from '../type';
|
||||
@ -171,7 +172,7 @@ export const useBaseVariable = ({
|
||||
const compile = useCompile();
|
||||
const getFilterOptions = useGetFilterOptions();
|
||||
const { isDisabled } = useContext(BaseVariableContext) || {};
|
||||
const { getCollectionField } = useCollectionManager_deprecated(dataSource);
|
||||
const cm = useCollectionManager();
|
||||
|
||||
const loadChildren = (option: Option): Promise<void> => {
|
||||
if (!option.field?.target) {
|
||||
@ -192,7 +193,7 @@ export const useBaseVariable = ({
|
||||
loadChildren,
|
||||
compile,
|
||||
isDisabled: isDisabled || isDisabledDefault,
|
||||
getCollectionField,
|
||||
getCollectionField: cm.getCollectionField,
|
||||
deprecated,
|
||||
}) || []
|
||||
)
|
||||
|
@ -28,10 +28,13 @@ import { uniq } from './utils/uniq';
|
||||
export const VariablesContext = createContext<VariablesContextType>(null);
|
||||
VariablesContext.displayName = 'VariablesContext';
|
||||
|
||||
const variableToCollectionName: {
|
||||
const variableToCollectionName: Record<
|
||||
string,
|
||||
{
|
||||
collectionName?: string;
|
||||
dataSource?: string;
|
||||
} = {};
|
||||
}
|
||||
> = {};
|
||||
|
||||
const getFieldPath = (variablePath: string, variableToCollectionName: Record<string, any>) => {
|
||||
let dataSource;
|
||||
@ -260,17 +263,18 @@ const VariablesProvider = ({ children }) => {
|
||||
localVariables = _.isArray(localVariables) ? localVariables : [localVariables];
|
||||
}
|
||||
|
||||
const path = getPath(variableString);
|
||||
const { fieldPath, dataSource } = getFieldPath(
|
||||
path,
|
||||
mergeVariableToCollectionNameWithLocalVariables(variableToCollectionName, localVariables as VariableOption[]),
|
||||
const _variableToCollectionName = mergeVariableToCollectionNameWithLocalVariables(
|
||||
variableToCollectionName,
|
||||
localVariables as VariableOption[],
|
||||
);
|
||||
const path = getPath(variableString);
|
||||
const { fieldPath, dataSource } = getFieldPath(path, _variableToCollectionName);
|
||||
let result = getCollectionJoinField(fieldPath, dataSource);
|
||||
|
||||
// 当仅有一个例如 `$user` 这样的字符串时,需要拼一个假的 `collectionField` 返回
|
||||
if (!result && !path.includes('.')) {
|
||||
result = {
|
||||
target: variableToCollectionName[path]?.collectionName,
|
||||
target: _variableToCollectionName[path]?.collectionName,
|
||||
};
|
||||
}
|
||||
|
||||
@ -335,7 +339,13 @@ function mergeCtxWithLocalVariables(ctx: Record<string, any>, localVariables?: V
|
||||
}
|
||||
|
||||
function mergeVariableToCollectionNameWithLocalVariables(
|
||||
variableToCollectionName: Record<string, any>,
|
||||
variableToCollectionName: Record<
|
||||
string,
|
||||
{
|
||||
collectionName?: string;
|
||||
dataSource?: string;
|
||||
}
|
||||
>,
|
||||
localVariables?: VariableOption[],
|
||||
) {
|
||||
variableToCollectionName = { ...variableToCollectionName };
|
||||
|
@ -694,6 +694,36 @@ describe('useVariables', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('getCollectionFiled with only variable name', async () => {
|
||||
const { result } = renderHook(() => useVariables(), {
|
||||
wrapper: Providers,
|
||||
});
|
||||
|
||||
await waitFor(async () => {
|
||||
expect(await result.current.getCollectionField('{{ $user }}')).toEqual({
|
||||
target: 'users',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('getCollectionFiled with local variable name', async () => {
|
||||
const { result } = renderHook(() => useVariables(), {
|
||||
wrapper: Providers,
|
||||
});
|
||||
|
||||
await waitFor(async () => {
|
||||
expect(
|
||||
await result.current.getCollectionField('{{ $local }}', {
|
||||
name: '$local',
|
||||
ctx: {},
|
||||
collectionName: 'local',
|
||||
}),
|
||||
).toEqual({
|
||||
target: 'local',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('getCollectionField with no exist variable', async () => {
|
||||
const { result } = renderHook(() => useVariables(), {
|
||||
wrapper: Providers,
|
||||
|
Loading…
x
Reference in New Issue
Block a user