chore: configuration of collection field default value (#5047)

* chore: configure collection field defalt value

* chore: getDefaultValueProps

* chore: collectionFieldInterface

* refactor: defaultProps

* refactor: defaultProps

* revert: field default props

* revert: field default props

* revert: field default props

* revert: field default props
This commit is contained in:
Katherine 2024-09-02 13:15:04 +08:00 committed by GitHub
parent 472190b7d5
commit c77cdf34b9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 78 additions and 100 deletions

View File

@ -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),

View File

@ -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);
}}

View File

@ -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]}}',
},
},
},
],
},
};
}
}