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',
|
2022-02-22 11:17:24 +08:00
|
|
|
'x-initializer': 'GridFormItemInitializers',
|
2022-02-18 02:05:42 +08:00
|
|
|
properties: {},
|
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
type: 'void',
|
2022-02-22 11:17:24 +08:00
|
|
|
'x-initializer': 'FormActionInitializers',
|
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;
|
|
|
|
};
|
|
|
|
|
2022-02-22 11:17:24 +08:00
|
|
|
export const FormBlockInitializer = (props) => {
|
2022-02-18 02:05:42 +08:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
);
|
2022-02-22 11:17:24 +08:00
|
|
|
};
|