diff --git a/packages/core/client/src/collection-manager/Configuration/AddFieldAction.tsx b/packages/core/client/src/collection-manager/Configuration/AddFieldAction.tsx index 622e5d830b..0fb0ad2965 100644 --- a/packages/core/client/src/collection-manager/Configuration/AddFieldAction.tsx +++ b/packages/core/client/src/collection-manager/Configuration/AddFieldAction.tsx @@ -16,64 +16,23 @@ import { cloneDeep } from 'lodash'; import React, { useCallback, useMemo, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useRequest } from '../../api-client'; +import { CollectionFieldInterface } from '../../data-source'; import { RecordProvider, useRecord } from '../../record-provider'; import { ActionContextProvider, SchemaComponent, useActionContext, useCompile } from '../../schema-component'; import { useResourceActionContext, useResourceContext } from '../ResourceActionProvider'; import { useCancelAction } from '../action-hooks'; import { useCollectionManager_deprecated } from '../hooks'; import useDialect from '../hooks/useDialect'; -import { IField } from '../interfaces/types'; import * as components from './components'; import { useFieldInterfaceOptions } from './interfaces'; -const getSchema = (schema: IField, record: any, compile) => { +const getSchema = (schema: CollectionFieldInterface, record: any, compile) => { if (!schema) { return; } - const properties = cloneDeep(schema.properties) as any; + const properties = schema.getConfigureFormProperties(); - if (schema.hasDefaultValue === true) { - properties['defaultValue'] = cloneDeep(schema?.default?.uiSchema); - properties.defaultValue.required = false; - properties['defaultValue']['title'] = compile('{{ t("Default value") }}'); - properties['defaultValue']['x-decorator'] = 'FormItem'; - properties['defaultValue']['x-reactions'] = [ - { - dependencies: [ - 'uiSchema.x-component-props.gmt', - 'uiSchema.x-component-props.showTime', - 'uiSchema.x-component-props.dateFormat', - 'uiSchema.x-component-props.timeFormat', - ], - fulfill: { - state: { - componentProps: { - gmt: '{{$deps[0]}}', - showTime: '{{$deps[1]}}', - dateFormat: '{{$deps[2]}}', - timeFormat: '{{$deps[3]}}', - }, - }, - }, - }, - { - dependencies: ['primaryKey', 'unique', 'autoIncrement'], - when: '{{$deps[0]||$deps[1]||$deps[2]}}', - fulfill: { - state: { - hidden: true, - value: null, - }, - }, - otherwise: { - state: { - hidden: false, - }, - }, - }, - ]; - } const initialValue: any = { name: `f_${uid()}`, ...cloneDeep(schema.default), diff --git a/packages/core/client/src/collection-manager/Configuration/EditFieldAction.tsx b/packages/core/client/src/collection-manager/Configuration/EditFieldAction.tsx index db509e36b6..3ca1486ac1 100644 --- a/packages/core/client/src/collection-manager/Configuration/EditFieldAction.tsx +++ b/packages/core/client/src/collection-manager/Configuration/EditFieldAction.tsx @@ -16,6 +16,7 @@ import set from 'lodash/set'; import React, { useMemo, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useAPIClient, useRequest } from '../../api-client'; +import { CollectionFieldInterface } from '../../data-source'; import { useCollectionParentRecordData } from '../../data-source/collection-record/CollectionRecordProvider'; import { RecordProvider, useRecord } from '../../record-provider'; import { ActionContextProvider, SchemaComponent, useActionContext, useCompile } from '../../schema-component'; @@ -23,60 +24,23 @@ import { useResourceActionContext, useResourceContext } from '../ResourceActionP import { useCancelAction } from '../action-hooks'; import { useCollectionManager_deprecated } from '../hooks'; import useDialect from '../hooks/useDialect'; -import { IField } from '../interfaces/types'; import * as components from './components'; -const getSchema = (schema: IField, record: any, compile, getContainer): ISchema => { +const getSchema = ( + schema: CollectionFieldInterface, + defaultValues: any, + record: any, + compile, + getContainer, +): ISchema => { if (!schema) { return; } - const properties = cloneDeep(schema.properties) as any; + const properties = schema.getConfigureFormProperties(); if (properties?.name) { properties.name['x-disabled'] = true; } - if (schema.hasDefaultValue === true) { - properties['defaultValue'] = cloneDeep(schema.default.uiSchema) || {}; - properties.defaultValue.required = false; - properties['defaultValue']['title'] = compile('{{ t("Default value") }}'); - properties['defaultValue']['x-decorator'] = 'FormItem'; - properties['defaultValue']['x-reactions'] = [ - { - dependencies: [ - 'uiSchema.x-component-props.gmt', - 'uiSchema.x-component-props.showTime', - 'uiSchema.x-component-props.dateFormat', - 'uiSchema.x-component-props.timeFormat', - ], - fulfill: { - state: { - componentProps: { - gmt: '{{$deps[0]}}', - showTime: '{{$deps[1]}}', - dateFormat: '{{$deps[2]}}', - timeFormat: '{{$deps[3]}}', - }, - }, - }, - }, - { - dependencies: ['primaryKey', 'unique', 'autoIncrement'], - when: '{{$deps[0]||$deps[1]||$deps[2]}}', - fulfill: { - state: { - hidden: true, - value: undefined, - }, - }, - otherwise: { - state: { - hidden: false, - }, - }, - }, - ]; - } - return { type: 'object', properties: { @@ -92,7 +56,7 @@ const getSchema = (schema: IField, record: any, compile, getContainer): ISchema return useRequest( () => Promise.resolve({ - data: cloneDeep(omit(schema.default, ['uiSchema.rawTitle'])), + data: cloneDeep(omit(defaultValues, ['uiSchema.rawTitle'])), }), options, ); @@ -230,15 +194,7 @@ export const EditFieldAction = (props) => { set(defaultValues.reverseField, 'name', `f_${uid()}`); set(defaultValues.reverseField, 'uiSchema.title', record.__parent?.title); } - const schema = getSchema( - { - ...interfaceConf, - default: defaultValues, - }, - record, - compile, - getContainer, - ); + const schema = getSchema(interfaceConf, defaultValues, record, compile, getContainer); setSchema(schema); setVisible(true); }} diff --git a/packages/core/client/src/data-source/collection-field-interface/CollectionFieldInterface.ts b/packages/core/client/src/data-source/collection-field-interface/CollectionFieldInterface.ts index c063a2a577..16cb0c008c 100644 --- a/packages/core/client/src/data-source/collection-field-interface/CollectionFieldInterface.ts +++ b/packages/core/client/src/data-source/collection-field-interface/CollectionFieldInterface.ts @@ -8,9 +8,10 @@ */ import type { ISchema } from '@formily/react'; +import { cloneDeep } from 'lodash'; import type { CollectionFieldOptions } from '../collection'; import { CollectionFieldInterfaceManager } from './CollectionFieldInterfaceManager'; - +import { defaultProps } from '../../collection-manager/interfaces/properties'; export type CollectionFieldInterfaceFactory = new ( collectionFieldInterfaceManager: CollectionFieldInterfaceManager, ) => CollectionFieldInterface; @@ -42,6 +43,7 @@ export abstract class CollectionFieldInterface { componentOptions?: CollectionFieldInterfaceComponentOption[]; isAssociation?: boolean; operators?: any[]; + properties?: any; /** * - 如果该值为空,则在 Filter 组件中该字段会被过滤掉 * - 如果该值为空,则不会在变量列表中看到该字段 @@ -82,4 +84,65 @@ export abstract class CollectionFieldInterface { } this.componentOptions.push(componentOption); } + getConfigureFormProperties() { + const defaultValueProps = this.hasDefaultValue ? this.getDefaultValueProperty() : {}; + return { + ...cloneDeep({ ...defaultProps, ...this?.properties }), + ...defaultValueProps, + }; + } + getDefaultValueProperty() { + return { + defaultValue: { + ...cloneDeep(this?.default?.uiSchema), + ...this?.properties?.uiSchema, + required: false, + title: '{{ t("Default value") }}', + 'x-decorator': 'FormItem', + 'x-reactions': [ + { + dependencies: [ + 'uiSchema.x-component-props.gmt', + 'uiSchema.x-component-props.showTime', + 'uiSchema.x-component-props.dateFormat', + 'uiSchema.x-component-props.timeFormat', + ], + fulfill: { + state: { + componentProps: { + gmt: '{{$deps[0]}}', + showTime: '{{$deps[1]}}', + dateFormat: '{{$deps[2]}}', + timeFormat: '{{$deps[3]}}', + }, + }, + }, + }, + { + dependencies: ['primaryKey', 'unique', 'autoIncrement'], + when: '{{$deps[0]||$deps[1]||$deps[2]}}', + fulfill: { + state: { + hidden: true, + value: null, + }, + }, + otherwise: { + state: { + hidden: false, + }, + }, + }, + { + dependencies: ['uiSchema.enum'], + fulfill: { + state: { + dataSource: '{{$deps[0]}}', + }, + }, + }, + ], + }, + }; + } }