mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-07-02 03:02:19 +08:00
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:
parent
472190b7d5
commit
c77cdf34b9
@ -16,64 +16,23 @@ import { cloneDeep } from 'lodash';
|
|||||||
import React, { useCallback, useMemo, useState } from 'react';
|
import React, { useCallback, useMemo, useState } from 'react';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { useRequest } from '../../api-client';
|
import { useRequest } from '../../api-client';
|
||||||
|
import { CollectionFieldInterface } from '../../data-source';
|
||||||
import { RecordProvider, useRecord } from '../../record-provider';
|
import { RecordProvider, useRecord } from '../../record-provider';
|
||||||
import { ActionContextProvider, SchemaComponent, useActionContext, useCompile } from '../../schema-component';
|
import { ActionContextProvider, SchemaComponent, useActionContext, useCompile } from '../../schema-component';
|
||||||
import { useResourceActionContext, useResourceContext } from '../ResourceActionProvider';
|
import { useResourceActionContext, useResourceContext } from '../ResourceActionProvider';
|
||||||
import { useCancelAction } from '../action-hooks';
|
import { useCancelAction } from '../action-hooks';
|
||||||
import { useCollectionManager_deprecated } from '../hooks';
|
import { useCollectionManager_deprecated } from '../hooks';
|
||||||
import useDialect from '../hooks/useDialect';
|
import useDialect from '../hooks/useDialect';
|
||||||
import { IField } from '../interfaces/types';
|
|
||||||
import * as components from './components';
|
import * as components from './components';
|
||||||
import { useFieldInterfaceOptions } from './interfaces';
|
import { useFieldInterfaceOptions } from './interfaces';
|
||||||
|
|
||||||
const getSchema = (schema: IField, record: any, compile) => {
|
const getSchema = (schema: CollectionFieldInterface, record: any, compile) => {
|
||||||
if (!schema) {
|
if (!schema) {
|
||||||
return;
|
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 = {
|
const initialValue: any = {
|
||||||
name: `f_${uid()}`,
|
name: `f_${uid()}`,
|
||||||
...cloneDeep(schema.default),
|
...cloneDeep(schema.default),
|
||||||
|
@ -16,6 +16,7 @@ import set from 'lodash/set';
|
|||||||
import React, { useMemo, useState } from 'react';
|
import React, { useMemo, useState } from 'react';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { useAPIClient, useRequest } from '../../api-client';
|
import { useAPIClient, useRequest } from '../../api-client';
|
||||||
|
import { CollectionFieldInterface } from '../../data-source';
|
||||||
import { useCollectionParentRecordData } from '../../data-source/collection-record/CollectionRecordProvider';
|
import { useCollectionParentRecordData } from '../../data-source/collection-record/CollectionRecordProvider';
|
||||||
import { RecordProvider, useRecord } from '../../record-provider';
|
import { RecordProvider, useRecord } from '../../record-provider';
|
||||||
import { ActionContextProvider, SchemaComponent, useActionContext, useCompile } from '../../schema-component';
|
import { ActionContextProvider, SchemaComponent, useActionContext, useCompile } from '../../schema-component';
|
||||||
@ -23,60 +24,23 @@ import { useResourceActionContext, useResourceContext } from '../ResourceActionP
|
|||||||
import { useCancelAction } from '../action-hooks';
|
import { useCancelAction } from '../action-hooks';
|
||||||
import { useCollectionManager_deprecated } from '../hooks';
|
import { useCollectionManager_deprecated } from '../hooks';
|
||||||
import useDialect from '../hooks/useDialect';
|
import useDialect from '../hooks/useDialect';
|
||||||
import { IField } from '../interfaces/types';
|
|
||||||
import * as components from './components';
|
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) {
|
if (!schema) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const properties = cloneDeep(schema.properties) as any;
|
const properties = schema.getConfigureFormProperties();
|
||||||
if (properties?.name) {
|
if (properties?.name) {
|
||||||
properties.name['x-disabled'] = true;
|
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 {
|
return {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
properties: {
|
properties: {
|
||||||
@ -92,7 +56,7 @@ const getSchema = (schema: IField, record: any, compile, getContainer): ISchema
|
|||||||
return useRequest(
|
return useRequest(
|
||||||
() =>
|
() =>
|
||||||
Promise.resolve({
|
Promise.resolve({
|
||||||
data: cloneDeep(omit(schema.default, ['uiSchema.rawTitle'])),
|
data: cloneDeep(omit(defaultValues, ['uiSchema.rawTitle'])),
|
||||||
}),
|
}),
|
||||||
options,
|
options,
|
||||||
);
|
);
|
||||||
@ -230,15 +194,7 @@ export const EditFieldAction = (props) => {
|
|||||||
set(defaultValues.reverseField, 'name', `f_${uid()}`);
|
set(defaultValues.reverseField, 'name', `f_${uid()}`);
|
||||||
set(defaultValues.reverseField, 'uiSchema.title', record.__parent?.title);
|
set(defaultValues.reverseField, 'uiSchema.title', record.__parent?.title);
|
||||||
}
|
}
|
||||||
const schema = getSchema(
|
const schema = getSchema(interfaceConf, defaultValues, record, compile, getContainer);
|
||||||
{
|
|
||||||
...interfaceConf,
|
|
||||||
default: defaultValues,
|
|
||||||
},
|
|
||||||
record,
|
|
||||||
compile,
|
|
||||||
getContainer,
|
|
||||||
);
|
|
||||||
setSchema(schema);
|
setSchema(schema);
|
||||||
setVisible(true);
|
setVisible(true);
|
||||||
}}
|
}}
|
||||||
|
@ -8,9 +8,10 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import type { ISchema } from '@formily/react';
|
import type { ISchema } from '@formily/react';
|
||||||
|
import { cloneDeep } from 'lodash';
|
||||||
import type { CollectionFieldOptions } from '../collection';
|
import type { CollectionFieldOptions } from '../collection';
|
||||||
import { CollectionFieldInterfaceManager } from './CollectionFieldInterfaceManager';
|
import { CollectionFieldInterfaceManager } from './CollectionFieldInterfaceManager';
|
||||||
|
import { defaultProps } from '../../collection-manager/interfaces/properties';
|
||||||
export type CollectionFieldInterfaceFactory = new (
|
export type CollectionFieldInterfaceFactory = new (
|
||||||
collectionFieldInterfaceManager: CollectionFieldInterfaceManager,
|
collectionFieldInterfaceManager: CollectionFieldInterfaceManager,
|
||||||
) => CollectionFieldInterface;
|
) => CollectionFieldInterface;
|
||||||
@ -42,6 +43,7 @@ export abstract class CollectionFieldInterface {
|
|||||||
componentOptions?: CollectionFieldInterfaceComponentOption[];
|
componentOptions?: CollectionFieldInterfaceComponentOption[];
|
||||||
isAssociation?: boolean;
|
isAssociation?: boolean;
|
||||||
operators?: any[];
|
operators?: any[];
|
||||||
|
properties?: any;
|
||||||
/**
|
/**
|
||||||
* - 如果该值为空,则在 Filter 组件中该字段会被过滤掉
|
* - 如果该值为空,则在 Filter 组件中该字段会被过滤掉
|
||||||
* - 如果该值为空,则不会在变量列表中看到该字段
|
* - 如果该值为空,则不会在变量列表中看到该字段
|
||||||
@ -82,4 +84,65 @@ export abstract class CollectionFieldInterface {
|
|||||||
}
|
}
|
||||||
this.componentOptions.push(componentOption);
|
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]}}',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user