Merge pull request 'feat: 添加自定义组件文本类型' (#432) from fix_filterCustom into @hera/dev

Reviewed-on: daoyoucloud/tachycode#432
Reviewed-by: sealday <zhanglin@daoyoucloud.com>
This commit is contained in:
sealday 2024-03-19 18:58:56 +08:00
commit 486b51f2aa
3 changed files with 65 additions and 34 deletions

View File

@ -34,15 +34,22 @@ export const SchemaSettingsRemove: FC<SchemaSettingsRemoveProps> = (props) => {
removeParentsIfNoChildren, removeParentsIfNoChildren,
breakRemoveOn, breakRemoveOn,
}; };
const name = fieldSchema.name as string;
const fieldName = name.split('.')[1];
const title = fieldSchema.title;
if (field?.required) { if (field?.required) {
field.required = false; field.required = false;
fieldSchema['required'] = false; fieldSchema['required'] = false;
} }
await dn.remove(null, options); await dn.remove(null, options);
await confirm?.onOk?.(); await confirm?.onOk?.();
delete form.values['custom'][fieldSchema['collectionName']]; if (fieldSchema['x-component'] === 'Select' || fieldSchema['x-component'] === 'AutoComplete') {
const name = fieldSchema.name as string; delete form.values['custom'][fieldSchema['collectionName']];
const title = fieldSchema.title; } else {
if (form.values['custom']?.[fieldName]) {
delete form.values['custom'][fieldName];
}
}
for (const key in form.fields) { for (const key in form.fields) {
if (key.includes(name) && form.fields[key].title === title) { if (key.includes(name) && form.fields[key].title === title) {
delete form.fields[key]; delete form.fields[key];

View File

@ -29,7 +29,7 @@ import React, { memo, useCallback, useContext, useMemo } from 'react';
import { Schema, SchemaOptionsContext, observer, useField, useFieldSchema } from '@formily/react'; import { Schema, SchemaOptionsContext, observer, useField, useFieldSchema } from '@formily/react';
import { FormLayout } from '@formily/antd-v5'; import { FormLayout } from '@formily/antd-v5';
import { uid } from '@formily/shared'; import { uid } from '@formily/shared';
import { Field } from '@formily/core'; import { Field, onFieldValueChange } from '@formily/core';
import { import {
EditFormulaTitleField, EditFormulaTitleField,
EditTitle, EditTitle,
@ -65,9 +65,11 @@ export const FilterCustomItemInitializer: React.FC<{
const itemConfig = useSchemaInitializerItem(); const itemConfig = useSchemaInitializerItem();
const { getInterface } = useCollectionManager_deprecated(); const { getInterface } = useCollectionManager_deprecated();
const { collections } = useCollectionManager_deprecated(); const { collections } = useCollectionManager_deprecated();
collections.forEach((value) => { const allCollection = collections.map((value) => {
value['label'] = value.name; return {
value['value'] = value.name; label: value.title,
value: value.name,
};
}); });
const { options: fieldComponents, values: fieldComponentValues } = useFieldComponents(); const { options: fieldComponents, values: fieldComponentValues } = useFieldComponents();
const api = useAPIClient(); const api = useAPIClient();
@ -105,9 +107,9 @@ export const FilterCustomItemInitializer: React.FC<{
title: t('Field collection'), title: t('Field collection'),
'x-decorator': 'FormItem', 'x-decorator': 'FormItem',
'x-component': 'Select', 'x-component': 'Select',
enum: collections, enum: allCollection,
description: t('Select a collection field to use metadata of the field'), description: t('Select a collection field to use metadata of the field'),
required: true, 'x-visible': false,
}, },
}, },
}} }}
@ -121,16 +123,35 @@ export const FilterCustomItemInitializer: React.FC<{
values: { values: {
name: `f_${uid()}`, name: `f_${uid()}`,
}, },
effects() {
onFieldValueChange('component', (field) => {
const name = field.value;
const component = field.query('.collection').take() as Field;
if (name === 'Select' || name === 'AutoComplete') {
component.setDisplay('visible');
component.setRequired(true);
} else {
component.setDisplay('none');
component.setRequired(false);
}
});
},
}); });
const { name, title, component, collection } = values; const { title, component, collection } = values;
const defaultSchema = getInterface(component)?.default?.uiSchema || {}; const defaultSchema = getInterface(component)?.default?.uiSchema || {};
let name;
if (component === 'Select' || component === 'AutoComplete') {
name = collection;
} else {
name = component + uid();
}
insert( insert(
gridRowColWrap({ gridRowColWrap({
'x-component': component, 'x-component': component,
...defaultSchema, ...defaultSchema,
type: 'string', type: 'string',
title: title, title: title,
name: 'custom.' + collection, name: 'custom.' + name,
required: false, required: false,
'x-designer': 'FilterItemCustomDesigner', 'x-designer': 'FilterItemCustomDesigner',
'x-decorator': 'FilterFormItem', 'x-decorator': 'FilterFormItem',
@ -165,33 +186,36 @@ export const FilterItemCustomDesigner: React.FC = () => {
const { form } = useFormBlockContext(); const { form } = useFormBlockContext();
const field = useField(); const field = useField();
const { dn } = useDesignable(); const { dn } = useDesignable();
const component = fieldSchema['x-component'];
return ( return (
<GeneralSchemaDesigner> <GeneralSchemaDesigner>
<EditTitle /> <EditTitle />
<EditDescription /> <EditDescription />
<EditDefaultValue /> <EditDefaultValue />
<SchemaSettingsDataScope {component !== 'Input' ? (
collectionName={name} <SchemaSettingsDataScope
defaultFilter={fieldSchema?.['x-component-props']?.params?.filter || {}} collectionName={name}
form={form} defaultFilter={fieldSchema?.['x-component-props']?.params?.filter || {}}
onSubmit={({ filter }) => { form={form}
_.set(field.componentProps, 'params', { onSubmit={({ filter }) => {
...field.componentProps?.params, _.set(field.componentProps, 'params', {
filter, ...field.componentProps?.params,
}); filter,
fieldSchema['x-component-props']['params'] = field.componentProps.params; });
dn.emit('patch', { fieldSchema['x-component-props']['params'] = field.componentProps.params;
schema: { dn.emit('patch', {
['x-uid']: fieldSchema['x-uid'], schema: {
'x-component-props': fieldSchema['x-component-props'], ['x-uid']: fieldSchema['x-uid'],
}, 'x-component-props': fieldSchema['x-component-props'],
}); },
}} });
/> }}
<SchemaSettingCollection /> />
<SchemaSettingComponent /> ) : null}
<EditTitleField /> {component !== 'Input' ? <SchemaSettingCollection /> : null}
<EditFormulaTitleField /> {component !== 'Input' ? <SchemaSettingComponent /> : null}
{component !== 'Input' ? <EditTitleField /> : null}
{component !== 'Input' ? <EditFormulaTitleField /> : null}
<SchemaSettingsDivider /> <SchemaSettingsDivider />
<SchemaSettingsRemove <SchemaSettingsRemove
key="remove" key="remove"

View File

@ -530,7 +530,7 @@ export const SchemaSettingCollection = () => {
const collections = useCollectionManager(); const collections = useCollectionManager();
const options = collections?.dataSource['options']?.collections.map((value) => { const options = collections?.dataSource['options']?.collections.map((value) => {
return { return {
label: value.name, label: value.title,
value: value.name, value: value.name,
}; };
}); });