import { FormDialog, FormLayout } from '@formily/antd'; import { SchemaOptionsContext } from '@formily/react'; import React, { useContext } from 'react'; import { useTranslation } from 'react-i18next'; import { SchemaComponent, SchemaComponentOptions } from '../../..'; import { useRoleRecheck } from '../../../../acl'; import { SchemaInitializer } from '../../../../schema-initializer'; export const MenuItemInitializers = (props: any) => { const { t } = useTranslation(); const recheck = useRoleRecheck(); return ( { recheck?.(); }} {...props} items={[ { type: 'item', title: t('Group'), component: GroupItem, }, { type: 'item', title: t('Page'), component: PageMenuItem, }, { type: 'item', title: t('Link'), component: LinkMenuItem, }, ]} > {t('Add menu item')} ); }; const itemWrap = SchemaInitializer.itemWrap; export const GroupItem = itemWrap((props) => { const { insert } = props; const { t } = useTranslation(); const options = useContext(SchemaOptionsContext); return ( { const values = await FormDialog(t('Add group'), () => { return ( ); }).open({ initialValues: {}, }); const { title, icon } = values; insert({ type: 'void', title, 'x-component': 'Menu.SubMenu', 'x-decorator': 'ACLMenuItemProvider', 'x-component-props': { icon, }, 'x-server-hooks': [ { type: 'onSelfCreate', method: 'bindMenuToRole', }, ], }); }} /> ); }); export const PageMenuItem = itemWrap((props) => { const { insert } = props; const { t } = useTranslation(); const options = useContext(SchemaOptionsContext); return ( { const values = await FormDialog(t('Add page'), () => { return ( ); }).open({ initialValues: {}, }); const { title, icon } = values; insert({ type: 'void', title, 'x-component': 'Menu.Item', 'x-decorator': 'ACLMenuItemProvider', 'x-component-props': { icon, }, 'x-server-hooks': [ { type: 'onSelfCreate', method: 'bindMenuToRole', }, ], properties: { page: { type: 'void', 'x-component': 'Page', 'x-async': true, properties: { grid: { type: 'void', 'x-component': 'Grid', 'x-initializer': 'BlockInitializers', properties: {}, }, }, }, }, }); }} /> ); }); export const LinkMenuItem = itemWrap((props) => { const { insert } = props; const { t } = useTranslation(); const options = useContext(SchemaOptionsContext); return ( { const values = await FormDialog(t('Add link'), () => { return ( ); }).open({ initialValues: {}, }); const { title, href, icon } = values; insert({ type: 'void', title, 'x-component': 'Menu.URL', 'x-decorator': 'ACLMenuItemProvider', 'x-component-props': { icon, href, }, 'x-server-hooks': [ { type: 'onSelfCreate', method: 'bindMenuToRole', }, ], }); }} /> ); });