mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-09 23:49:27 +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 { ISchema, Schema } from '@formily/json-schema';
|
||||||
import { Tooltip } from 'antd';
|
import { Tooltip } from 'antd';
|
||||||
import React, { useContext, useMemo } from 'react';
|
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 { useCompile, useGetFilterOptions } from '../../../schema-component';
|
||||||
import { isSpecialCaseField } from '../../../schema-component/antd/form-item/hooks/useSpecialCase';
|
import { isSpecialCaseField } from '../../../schema-component/antd/form-item/hooks/useSpecialCase';
|
||||||
import { FieldOption, Option } from '../type';
|
import { FieldOption, Option } from '../type';
|
||||||
@ -171,7 +172,7 @@ export const useBaseVariable = ({
|
|||||||
const compile = useCompile();
|
const compile = useCompile();
|
||||||
const getFilterOptions = useGetFilterOptions();
|
const getFilterOptions = useGetFilterOptions();
|
||||||
const { isDisabled } = useContext(BaseVariableContext) || {};
|
const { isDisabled } = useContext(BaseVariableContext) || {};
|
||||||
const { getCollectionField } = useCollectionManager_deprecated(dataSource);
|
const cm = useCollectionManager();
|
||||||
|
|
||||||
const loadChildren = (option: Option): Promise<void> => {
|
const loadChildren = (option: Option): Promise<void> => {
|
||||||
if (!option.field?.target) {
|
if (!option.field?.target) {
|
||||||
@ -192,7 +193,7 @@ export const useBaseVariable = ({
|
|||||||
loadChildren,
|
loadChildren,
|
||||||
compile,
|
compile,
|
||||||
isDisabled: isDisabled || isDisabledDefault,
|
isDisabled: isDisabled || isDisabledDefault,
|
||||||
getCollectionField,
|
getCollectionField: cm.getCollectionField,
|
||||||
deprecated,
|
deprecated,
|
||||||
}) || []
|
}) || []
|
||||||
)
|
)
|
||||||
|
@ -28,10 +28,13 @@ import { uniq } from './utils/uniq';
|
|||||||
export const VariablesContext = createContext<VariablesContextType>(null);
|
export const VariablesContext = createContext<VariablesContextType>(null);
|
||||||
VariablesContext.displayName = 'VariablesContext';
|
VariablesContext.displayName = 'VariablesContext';
|
||||||
|
|
||||||
const variableToCollectionName: {
|
const variableToCollectionName: Record<
|
||||||
|
string,
|
||||||
|
{
|
||||||
collectionName?: string;
|
collectionName?: string;
|
||||||
dataSource?: string;
|
dataSource?: string;
|
||||||
} = {};
|
}
|
||||||
|
> = {};
|
||||||
|
|
||||||
const getFieldPath = (variablePath: string, variableToCollectionName: Record<string, any>) => {
|
const getFieldPath = (variablePath: string, variableToCollectionName: Record<string, any>) => {
|
||||||
let dataSource;
|
let dataSource;
|
||||||
@ -260,17 +263,18 @@ const VariablesProvider = ({ children }) => {
|
|||||||
localVariables = _.isArray(localVariables) ? localVariables : [localVariables];
|
localVariables = _.isArray(localVariables) ? localVariables : [localVariables];
|
||||||
}
|
}
|
||||||
|
|
||||||
const path = getPath(variableString);
|
const _variableToCollectionName = mergeVariableToCollectionNameWithLocalVariables(
|
||||||
const { fieldPath, dataSource } = getFieldPath(
|
variableToCollectionName,
|
||||||
path,
|
localVariables as VariableOption[],
|
||||||
mergeVariableToCollectionNameWithLocalVariables(variableToCollectionName, localVariables as VariableOption[]),
|
|
||||||
);
|
);
|
||||||
|
const path = getPath(variableString);
|
||||||
|
const { fieldPath, dataSource } = getFieldPath(path, _variableToCollectionName);
|
||||||
let result = getCollectionJoinField(fieldPath, dataSource);
|
let result = getCollectionJoinField(fieldPath, dataSource);
|
||||||
|
|
||||||
// 当仅有一个例如 `$user` 这样的字符串时,需要拼一个假的 `collectionField` 返回
|
// 当仅有一个例如 `$user` 这样的字符串时,需要拼一个假的 `collectionField` 返回
|
||||||
if (!result && !path.includes('.')) {
|
if (!result && !path.includes('.')) {
|
||||||
result = {
|
result = {
|
||||||
target: variableToCollectionName[path]?.collectionName,
|
target: _variableToCollectionName[path]?.collectionName,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -335,7 +339,13 @@ function mergeCtxWithLocalVariables(ctx: Record<string, any>, localVariables?: V
|
|||||||
}
|
}
|
||||||
|
|
||||||
function mergeVariableToCollectionNameWithLocalVariables(
|
function mergeVariableToCollectionNameWithLocalVariables(
|
||||||
variableToCollectionName: Record<string, any>,
|
variableToCollectionName: Record<
|
||||||
|
string,
|
||||||
|
{
|
||||||
|
collectionName?: string;
|
||||||
|
dataSource?: string;
|
||||||
|
}
|
||||||
|
>,
|
||||||
localVariables?: VariableOption[],
|
localVariables?: VariableOption[],
|
||||||
) {
|
) {
|
||||||
variableToCollectionName = { ...variableToCollectionName };
|
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 () => {
|
it('getCollectionField with no exist variable', async () => {
|
||||||
const { result } = renderHook(() => useVariables(), {
|
const { result } = renderHook(() => useVariables(), {
|
||||||
wrapper: Providers,
|
wrapper: Providers,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user