85 lines
2.1 KiB
TypeScript
85 lines
2.1 KiB
TypeScript
import { observer, useFieldSchema } from '@formily/react';
|
|
import { Switch } from 'antd';
|
|
import React from 'react';
|
|
import { SchemaInitializer } from '../..';
|
|
import { useDesignable } from '../../../schema-component';
|
|
|
|
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 InitializeAction = SchemaInitializer.itemWrap((props) => {
|
|
const { item, insert } = props;
|
|
const { exists, remove } = useCurrentActionSchema(item.schema['x-action']);
|
|
return (
|
|
<SchemaInitializer.Item
|
|
onClick={() => {
|
|
if (exists) {
|
|
return remove();
|
|
}
|
|
insert({
|
|
type: 'void',
|
|
'x-component': 'Action',
|
|
...item.schema,
|
|
});
|
|
}}
|
|
>
|
|
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
|
|
{item.title} <Switch size={'small'} checked={exists} />
|
|
</div>
|
|
</SchemaInitializer.Item>
|
|
);
|
|
});
|
|
|
|
export const DetailsActionInitializer = observer((props: any) => {
|
|
return (
|
|
<SchemaInitializer.Button
|
|
insertPosition={'beforeEnd'}
|
|
style={{ marginLeft: 8 }}
|
|
items={[
|
|
{
|
|
type: 'itemGroup',
|
|
title: 'Enable actions',
|
|
children: [
|
|
{
|
|
type: 'item',
|
|
title: 'Create',
|
|
component: InitializeAction,
|
|
schema: {
|
|
title: 'Create',
|
|
'x-action': 'posts:create',
|
|
'x-align': 'left',
|
|
},
|
|
},
|
|
{
|
|
type: 'item',
|
|
title: 'Update',
|
|
component: InitializeAction,
|
|
schema: {
|
|
title: 'Update',
|
|
'x-action': 'posts:update',
|
|
},
|
|
},
|
|
],
|
|
},
|
|
]}
|
|
>
|
|
Configure actions
|
|
</SchemaInitializer.Button>
|
|
);
|
|
});
|