tachybase_todo/packages/plugins/workflow/src/client/components/CollectionBlockInitializer.tsx
Junyi e5bab3249a
refactor(plugin-workflow): manual collection block migration (#2064)
* refactor(plugin-workflow): change all collection node initializer in manual config

* refactor(plugin-workflow): clean code

* fix(plugin-workflow): fix custom form field in manual config

* refactor(plugin-workflow): add migration

* fix(plugin-workflow): fix custom form block migration

* fix(plugin-workflow): fix detail read-pretty for association fields
2023-06-15 23:06:47 -07:00

72 lines
2.0 KiB
TypeScript

import React from 'react';
import {
CollectionProvider,
SchemaInitializer,
SchemaInitializerItemOptions,
useCollectionManager,
useRecordCollectionDataSourceItems,
useSchemaTemplateManager,
} from '@nocobase/client';
import { traverseSchema } from '../nodes/manual/utils';
function InnerCollectionBlockInitializer({ insert, collection, dataSource, ...props }) {
const { getTemplateSchemaByMode } = useSchemaTemplateManager();
const { getCollection } = useCollectionManager();
const items = useRecordCollectionDataSourceItems('FormItem') as SchemaInitializerItemOptions[];
const resovledCollection = getCollection(collection);
async function onConfirm({ item }) {
const template = item.template ? await getTemplateSchemaByMode(item) : null;
const result = {
type: 'void',
name: resovledCollection.name,
title: resovledCollection.title,
'x-decorator': 'DetailsBlockProvider',
'x-decorator-props': {
collection,
dataSource,
},
'x-component': 'CardItem',
'x-component-props': {
title: props.title,
},
'x-designer': 'SimpleDesigner',
properties: {
grid: {
type: 'void',
'x-component': 'FormV2',
'x-component-props': {
useProps: '{{useDetailsBlockProps}}',
},
'x-read-pretty': true,
properties: {
grid: template || {
type: 'void',
'x-component': 'Grid',
'x-initializer': 'ReadPrettyFormItemInitializers',
properties: {},
},
},
},
},
};
traverseSchema(result, (node) => {
if (node['x-uid']) {
delete node['x-uid'];
}
});
insert(result);
}
return <SchemaInitializer.Item {...props} onClick={onConfirm} items={items} />;
}
export function CollectionBlockInitializer(props) {
return (
<CollectionProvider collection={props.collection}>
<InnerCollectionBlockInitializer {...props} />
</CollectionProvider>
);
}