fix: useCurrentFormVariable (#4428)

* refactor: useCurrentFormVariable support detail block

* fix: useCurrentFormVariable

* fix: bug
This commit is contained in:
Katherine 2024-05-20 20:55:30 +08:00 committed by GitHub
parent 26a2a6204a
commit 304f87bee2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 9 deletions

View File

@ -969,6 +969,8 @@ export const SchemaSettingsLinkageRules = function LinkageRules(props) {
const { type: formBlockType } = useFormBlockType();
const type = props?.type || fieldSchema?.['x-action'] ? 'button' : 'field';
const gridSchema = findGridSchema(fieldSchema) || fieldSchema;
const options = useLinkageCollectionFilterOptions(collectionName);
const linkageOptions = useLinkageCollectionFieldOptions(collectionName, readPretty);
const schema = useMemo<ISchema>(
() => ({
type: 'object',
@ -977,14 +979,11 @@ export const SchemaSettingsLinkageRules = function LinkageRules(props) {
fieldReaction: {
'x-component': FormLinkageRules,
'x-use-component-props': () => {
// eslint-disable-next-line react-hooks/rules-of-hooks
const options = useLinkageCollectionFilterOptions(collectionName);
return {
options,
defaultValues: gridSchema?.['x-linkage-rules'] || fieldSchema?.['x-linkage-rules'],
type,
// eslint-disable-next-line react-hooks/rules-of-hooks
linkageOptions: useLinkageCollectionFieldOptions(collectionName, readPretty),
linkageOptions,
collectionName,
form,
variables,

View File

@ -10,8 +10,13 @@
import { Form } from '@formily/core';
import { Schema } from '@formily/json-schema';
import { useTranslation } from 'react-i18next';
import { useFormBlockContext } from '../../../block-provider';
import { CollectionFieldOptions_deprecated } from '../../../collection-manager';
import { useBlockContext, useDetailsBlockContext, useFormBlockContext } from '../../../block-provider';
import {
CollectionFieldOptions_deprecated,
useResourceActionContext,
useResourceContext,
} from '../../../collection-manager';
import { useDataBlockRequest } from '../../../data-source';
import { useBaseVariable } from './useBaseVariable';
interface Props {
@ -60,6 +65,11 @@ export const useFormVariable = ({ collectionName, collectionField, schema, noDis
return result;
};
const useCurrentFormData = () => {
const ctx = useDataBlockRequest();
return ctx?.data?.data?.[0] || ctx?.data?.data;
};
/**
* `当前表单`
* @param param0
@ -74,7 +84,8 @@ export const useCurrentFormVariable = ({
}: Props = {}) => {
// const { getActiveFieldsName } = useFormActiveFields() || {};
const { t } = useTranslation();
const { form, collectionName, service } = useFormBlockContext();
const { form, collectionName } = useFormBlockContext();
const formData = useCurrentFormData();
const currentFormSettings = useBaseVariable({
collectionField,
uiSchema: schema,
@ -103,9 +114,9 @@ export const useCurrentFormVariable = ({
currentFormSettings,
/** 变量值 */
currentFormCtx:
formInstance?.values && Object.keys(formInstance?.values)?.length
formInstance?.readPretty === false && formInstance?.values && Object.keys(formInstance?.values)?.length
? formInstance?.values
: service?.data?.data || formInstance?.values,
: formData || formInstance?.values,
/** 用来判断是否可以显示`当前表单`变量 */
shouldDisplayCurrentForm: formInstance && !formInstance.readPretty,
};