2022-02-07 21:52:51 +08:00
|
|
|
import { FormOutlined, TableOutlined } from '@ant-design/icons';
|
|
|
|
import { Field } from '@formily/core';
|
|
|
|
import { observer, useField } from '@formily/react';
|
|
|
|
import { SchemaComponent, SchemaComponentProvider, SchemaInitializer } from '@nocobase/client';
|
|
|
|
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-08 19:51:30 +08:00
|
|
|
const AddBlockButton = observer((props: any) => {
|
2022-02-07 21:52:51 +08:00
|
|
|
return (
|
|
|
|
<SchemaInitializer.Button
|
|
|
|
wrap={(schema) => schema}
|
|
|
|
insertPosition={'beforeBegin'}
|
|
|
|
items={[
|
|
|
|
{
|
|
|
|
title: 'Data blocks',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
title: 'Table',
|
2022-02-08 12:17:06 +08:00
|
|
|
component: TableBlockInitializerItem,
|
2022-02-07 21:52:51 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Form',
|
2022-02-08 12:17:06 +08:00
|
|
|
component: FormBlockInitializerItem,
|
2022-02-07 21:52:51 +08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
>
|
2022-02-08 19:51:30 +08:00
|
|
|
Add block
|
2022-02-07 21:52:51 +08:00
|
|
|
</SchemaInitializer.Button>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-02-08 12:17:06 +08:00
|
|
|
const TableBlockInitializerItem = (props) => {
|
2022-02-07 21:52:51 +08:00
|
|
|
const { insert } = props;
|
|
|
|
return (
|
|
|
|
<SchemaInitializer.Item
|
|
|
|
icon={<TableOutlined />}
|
|
|
|
onClick={(info) => {
|
|
|
|
console.log({ info });
|
|
|
|
insert({
|
|
|
|
type: 'void',
|
|
|
|
title: info.key,
|
|
|
|
'x-component': 'Hello',
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
items={[
|
|
|
|
{
|
|
|
|
type: 'itemGroup',
|
|
|
|
title: 'select a data source',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
key: 'users',
|
|
|
|
title: 'Users',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'posts',
|
|
|
|
title: 'Posts',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
Table
|
|
|
|
</SchemaInitializer.Item>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-02-08 12:17:06 +08:00
|
|
|
const FormBlockInitializerItem = (props) => {
|
2022-02-07 21:52:51 +08:00
|
|
|
const { insert } = props;
|
|
|
|
return (
|
|
|
|
<SchemaInitializer.Item
|
|
|
|
icon={<FormOutlined />}
|
|
|
|
onClick={(info) => {
|
|
|
|
insert({
|
|
|
|
type: 'void',
|
|
|
|
title: 'form',
|
|
|
|
'x-component': 'Hello',
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Form
|
|
|
|
</SchemaInitializer.Item>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function App() {
|
|
|
|
return (
|
2022-02-08 19:51:30 +08:00
|
|
|
<SchemaComponentProvider components={{ Hello, AddBlockButton }}>
|
2022-02-07 21:52:51 +08:00
|
|
|
<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',
|
2022-02-08 19:51:30 +08:00
|
|
|
'x-component': 'AddBlockButton',
|
2022-02-07 21:52:51 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</SchemaComponentProvider>
|
|
|
|
);
|
|
|
|
}
|