Skip to content

Commit

Permalink
fix: useCurrentFormVariable (#4428)
Browse files Browse the repository at this point in the history
* refactor: useCurrentFormVariable support detail block

* fix: useCurrentFormVariable

* fix: bug
  • Loading branch information
Katherine committed May 20, 2024
1 parent 26a2a62 commit 304f87b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
7 changes: 3 additions & 4 deletions packages/core/client/src/schema-settings/SchemaSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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,
};
Expand Down

0 comments on commit 304f87b

Please sign in to comment.