2022-02-08 12:17:06 +08:00
|
|
|
import { observer, useFieldSchema } from '@formily/react';
|
|
|
|
import {
|
|
|
|
Action,
|
|
|
|
ActionBar,
|
|
|
|
SchemaComponent,
|
|
|
|
SchemaComponentProvider,
|
|
|
|
SchemaInitializer,
|
|
|
|
useDesignable
|
|
|
|
} from '@nocobase/client';
|
|
|
|
import { Switch } from 'antd';
|
2022-02-07 21:52:51 +08:00
|
|
|
import React from 'react';
|
|
|
|
|
2022-02-08 12:17:06 +08:00
|
|
|
const ActionInitializerButton = observer((props: any) => {
|
2022-02-07 21:52:51 +08:00
|
|
|
return (
|
|
|
|
<SchemaInitializer.Button
|
2022-02-07 23:30:24 +08:00
|
|
|
insertPosition={'beforeEnd'}
|
|
|
|
style={{ marginLeft: 8 }}
|
2022-02-07 21:52:51 +08:00
|
|
|
items={[
|
|
|
|
{
|
2022-02-07 23:30:24 +08:00
|
|
|
title: 'Enable actions',
|
2022-02-07 21:52:51 +08:00
|
|
|
children: [
|
|
|
|
{
|
2022-02-07 23:30:24 +08:00
|
|
|
title: 'Create',
|
|
|
|
key: 'create',
|
2022-02-08 12:17:06 +08:00
|
|
|
component: 'ActionInitializerItem',
|
2022-02-07 23:30:24 +08:00
|
|
|
schema: {
|
2022-02-08 12:17:06 +08:00
|
|
|
title: 'Create',
|
|
|
|
'x-action': 'posts:create',
|
2022-02-07 23:30:24 +08:00
|
|
|
'x-align': 'left',
|
|
|
|
},
|
2022-02-07 21:52:51 +08:00
|
|
|
},
|
|
|
|
{
|
2022-02-07 23:30:24 +08:00
|
|
|
title: 'Update',
|
|
|
|
key: 'update',
|
2022-02-08 12:17:06 +08:00
|
|
|
schema: {
|
|
|
|
title: 'Update',
|
|
|
|
'x-action': 'posts:update',
|
|
|
|
},
|
|
|
|
component: 'ActionInitializerItem',
|
2022-02-07 21:52:51 +08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
Configure actions
|
|
|
|
</SchemaInitializer.Button>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-02-08 12:17:06 +08:00
|
|
|
const useCurrentActionSchema = (action: string) => {
|
|
|
|
const fieldSchema = useFieldSchema();
|
|
|
|
const { remove } = useDesignable();
|
|
|
|
const schema = fieldSchema.reduceProperties((buf, s) => {
|
|
|
|
if (s['x-action'] === action) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
schema,
|
|
|
|
exists: !!schema,
|
|
|
|
remove() {
|
|
|
|
schema && remove(schema);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const ActionInitializerItem = (props) => {
|
2022-02-07 23:30:24 +08:00
|
|
|
const { title, schema, insert } = props;
|
2022-02-08 12:17:06 +08:00
|
|
|
const { exists, remove } = useCurrentActionSchema(schema['x-action']);
|
2022-02-07 21:52:51 +08:00
|
|
|
return (
|
|
|
|
<SchemaInitializer.Item
|
|
|
|
onClick={(info) => {
|
2022-02-08 12:17:06 +08:00
|
|
|
if (exists) {
|
|
|
|
return remove();
|
|
|
|
}
|
2022-02-07 21:52:51 +08:00
|
|
|
insert({
|
|
|
|
type: 'void',
|
2022-02-07 23:30:24 +08:00
|
|
|
'x-component': 'Action',
|
|
|
|
...schema,
|
2022-02-07 21:52:51 +08:00
|
|
|
});
|
|
|
|
}}
|
|
|
|
>
|
2022-02-08 12:17:06 +08:00
|
|
|
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
|
|
|
|
{title} <Switch size={'small'} checked={exists} />
|
|
|
|
</div>
|
2022-02-07 21:52:51 +08:00
|
|
|
</SchemaInitializer.Item>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function App() {
|
|
|
|
return (
|
2022-02-08 12:17:06 +08:00
|
|
|
<SchemaComponentProvider components={{ ActionBar, Action, ActionInitializerItem, ActionInitializerButton }}>
|
2022-02-07 21:52:51 +08:00
|
|
|
<SchemaComponent
|
|
|
|
schema={{
|
|
|
|
type: 'void',
|
|
|
|
name: 'page',
|
2022-02-07 23:30:24 +08:00
|
|
|
'x-component': 'ActionBar',
|
2022-02-08 12:17:06 +08:00
|
|
|
'x-action-initializer': 'ActionInitializerButton',
|
2022-02-07 21:52:51 +08:00
|
|
|
properties: {
|
2022-02-07 23:30:24 +08:00
|
|
|
action1: {
|
2022-02-07 21:52:51 +08:00
|
|
|
type: 'void',
|
2022-02-08 12:17:06 +08:00
|
|
|
title: 'Update',
|
|
|
|
'x-action': 'posts:update',
|
2022-02-07 23:30:24 +08:00
|
|
|
'x-component': 'Action',
|
2022-02-07 21:52:51 +08:00
|
|
|
},
|
2022-02-08 12:17:06 +08:00
|
|
|
// action2: {
|
|
|
|
// type: 'void',
|
|
|
|
// title: 'Create',
|
|
|
|
// 'x-action': 'posts:create',
|
|
|
|
// 'x-align': 'left',
|
|
|
|
// 'x-component': 'Action',
|
|
|
|
// },
|
2022-02-07 21:52:51 +08:00
|
|
|
},
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</SchemaComponentProvider>
|
|
|
|
);
|
|
|
|
}
|