tachybase_todo/packages/client/src/schema-initializer/Initializers/BlockInitializer/FormBlock.tsx

83 lines
2.0 KiB
TypeScript
Raw Normal View History

2022-02-18 02:05:42 +08:00
import { FormOutlined } from '@ant-design/icons';
import { ISchema } from '@formily/react';
import React from 'react';
import { useTranslation } from 'react-i18next';
import { SchemaInitializer } from '../..';
import { useCollectionManager } from '../../../collection-manager';
const createSchema = (collectionName) => {
const schema: ISchema = {
type: 'void',
'x-collection': 'collections',
'x-decorator': 'ResourceActionProvider',
'x-decorator-props': {
collection: collectionName,
request: {
resource: collectionName,
action: 'get',
params: {},
},
},
2022-02-20 01:22:10 +08:00
'x-designer': 'Form.Designer',
2022-02-18 02:05:42 +08:00
'x-component': 'CardItem',
properties: {
form: {
type: 'void',
'x-decorator': 'Form',
'x-decorator-props': {},
properties: {
grid: {
type: 'void',
'x-component': 'Grid',
'x-item-initializer': 'FormItemInitializer',
properties: {},
},
actions: {
type: 'void',
'x-action-initializer': 'FormActionInitializer',
2022-02-18 20:29:24 +08:00
'x-component': 'ActionBar',
'x-component-props': {
layout: 'one-column',
style: {
marginTop: 24,
},
},
2022-02-18 02:05:42 +08:00
properties: {},
},
},
},
},
};
return schema;
};
const itemWrap = SchemaInitializer.itemWrap;
export const FormBlock = itemWrap((props) => {
const { insert } = props;
const { collections } = useCollectionManager();
const { t } = useTranslation();
return (
<SchemaInitializer.Item
{...props}
icon={<FormOutlined />}
onClick={({ item }) => {
insert(createSchema(item.name));
}}
items={[
{
type: 'itemGroup',
title: t('Select data source'),
children: collections?.map((item) => {
return {
type: 'item',
name: item.name,
title: item.title,
};
}),
},
]}
/>
);
});