2022-02-22 11:17:24 +08:00
|
|
|
|
import { TableOutlined } from '@ant-design/icons';
|
2022-02-07 21:52:51 +08:00
|
|
|
|
import { Field } from '@formily/core';
|
|
|
|
|
import { observer, useField } from '@formily/react';
|
2022-02-22 11:17:24 +08:00
|
|
|
|
import {
|
|
|
|
|
SchemaComponent,
|
|
|
|
|
SchemaComponentProvider,
|
|
|
|
|
SchemaInitializer,
|
|
|
|
|
SchemaInitializerProvider,
|
|
|
|
|
useSchemaInitializer
|
|
|
|
|
} from '@nocobase/client';
|
2022-02-07 21:52:51 +08:00
|
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
|
|
const Hello = observer((props) => {
|
|
|
|
|
const field = useField<Field>();
|
|
|
|
|
return (
|
|
|
|
|
<div style={{ marginBottom: 20, padding: '0 20px', height: 50, lineHeight: '50px', background: '#f1f1f1' }}>
|
|
|
|
|
{field.title}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2022-02-22 11:17:24 +08:00
|
|
|
|
const TableBlockInitializer = SchemaInitializer.itemWrap((props) => {
|
2022-02-07 21:52:51 +08:00
|
|
|
|
const { insert } = props;
|
2022-02-09 00:13:42 +08:00
|
|
|
|
const items: any = [
|
|
|
|
|
{
|
|
|
|
|
type: 'itemGroup',
|
|
|
|
|
title: 'select a data source',
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
type: 'item',
|
|
|
|
|
title: 'Users',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: 'item',
|
|
|
|
|
title: 'Posts',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
];
|
2022-02-07 21:52:51 +08:00
|
|
|
|
return (
|
|
|
|
|
<SchemaInitializer.Item
|
|
|
|
|
icon={<TableOutlined />}
|
2022-02-09 00:13:42 +08:00
|
|
|
|
items={items}
|
|
|
|
|
onClick={({ item }) => {
|
|
|
|
|
// 如果有 items 时,onClick 里会返回点击的 item
|
|
|
|
|
// TODO: 实际情况,这里还需要补充更完整的初始化逻辑
|
2022-02-07 21:52:51 +08:00
|
|
|
|
insert({
|
|
|
|
|
type: 'void',
|
2022-02-09 00:13:42 +08:00
|
|
|
|
title: item.title,
|
2022-02-07 21:52:51 +08:00
|
|
|
|
'x-component': 'Hello',
|
|
|
|
|
});
|
|
|
|
|
}}
|
2022-02-09 00:13:42 +08:00
|
|
|
|
/>
|
2022-02-07 21:52:51 +08:00
|
|
|
|
);
|
2022-02-09 00:13:42 +08:00
|
|
|
|
});
|
2022-02-07 21:52:51 +08:00
|
|
|
|
|
2022-02-22 11:17:24 +08:00
|
|
|
|
const initializers = {
|
|
|
|
|
AddBlock: {
|
|
|
|
|
title: 'Add block',
|
|
|
|
|
insertPosition: 'beforeBegin',
|
|
|
|
|
items: [
|
|
|
|
|
{
|
|
|
|
|
type: 'itemGroup',
|
|
|
|
|
title: 'Data blocks',
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
type: 'item',
|
|
|
|
|
title: 'Table',
|
|
|
|
|
component: 'TableBlockInitializer',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: 'item',
|
|
|
|
|
title: 'Form',
|
|
|
|
|
component: 'GeneralInitializer',
|
|
|
|
|
schema: {
|
|
|
|
|
type: 'void',
|
|
|
|
|
title: 'Form',
|
|
|
|
|
'x-component': 'Hello',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const AddBlockButton = observer((props: any) => {
|
|
|
|
|
const { render } = useSchemaInitializer('AddBlock');
|
|
|
|
|
return <>{render()}</>;
|
2022-02-09 00:13:42 +08:00
|
|
|
|
});
|
2022-02-07 21:52:51 +08:00
|
|
|
|
|
|
|
|
|
export default function App() {
|
|
|
|
|
return (
|
2022-02-22 11:17:24 +08:00
|
|
|
|
<SchemaComponentProvider components={{ TableBlockInitializer, Hello, AddBlockButton }}>
|
|
|
|
|
<SchemaInitializerProvider initializers={initializers}>
|
|
|
|
|
<SchemaComponent
|
|
|
|
|
schema={{
|
|
|
|
|
type: 'void',
|
|
|
|
|
name: 'page',
|
|
|
|
|
'x-component': 'div',
|
|
|
|
|
properties: {
|
|
|
|
|
hello1: {
|
|
|
|
|
type: 'void',
|
|
|
|
|
title: 'Test1',
|
|
|
|
|
'x-component': 'Hello',
|
|
|
|
|
},
|
|
|
|
|
hello2: {
|
|
|
|
|
type: 'void',
|
|
|
|
|
title: 'Test2',
|
|
|
|
|
'x-component': 'Hello',
|
|
|
|
|
},
|
|
|
|
|
initializer: {
|
|
|
|
|
type: 'void',
|
|
|
|
|
'x-component': 'AddBlockButton',
|
|
|
|
|
},
|
2022-02-07 21:52:51 +08:00
|
|
|
|
},
|
2022-02-22 11:17:24 +08:00
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</SchemaInitializerProvider>
|
2022-02-07 21:52:51 +08:00
|
|
|
|
</SchemaComponentProvider>
|
|
|
|
|
);
|
|
|
|
|
}
|