被雨水过滤的空气-Rairn 6eed9ac2bb
refactor: fix warning of antd 4.x (#1998)
* refactor: fix warning by codemod

* refactor: fix warning of Dropdown

* perf: use memo

* refactor: resolve SchemaInitializer

* refactor: fix lint

* refactor: remove SettingsForm

* refactor: resolve SchemaInitializer

* refactor: fix lint

* refactor: move useMenuItem to root dir

* chore: fix conflicts

* refactor: resolve SchemaSetting

* refactor: fix lint

* test: fix failed

* chore: upgrade Vite

* fix: fix style

* refactor: fix lint

* refactor: extract component

* refactor: resovle Menu

* refactor: resolve Tabs

* refactor(getPopupContainer): should return the unique div

* refactor(Drawer): change style to rootStyle and className to rootClassName

* chore: update yarn.lock

* fix: fix T-432

* fix: fix T-338

* fix: fix T-490

* fix: collection fields

* fix: fix style

* fix: fix T-500

* fix: fix SettingMenu error (close T-516)

* fix: fix tanslation of Map (T-506)

* style: fix style (T-508)

* fix: fix schemaSetting switch of mobile (T-517)

* fix: fix T-518

* fix: fix T-524

* fix: fix T-507

* perf: optimize SchemaInitializer.Button

* perf: optimize SchemaSettings

* fix: fix serch of SchemaInitializer (T-547)

* chore: change delay

* fix: fix button style (T-548)

* fix: fix scroll bar

* fix: update yarn.lock

* fix: fix build error

* fix: should update sideMenu when change it

* fix: fix build error

* chore: mouseEnterDelay

* fix: fix group menu can not selected
2023-06-22 19:51:16 +08:00

177 lines
4.6 KiB
TypeScript

import { DownOutlined, PlusOutlined } from '@ant-design/icons';
import { createForm } from '@formily/core';
import { ISchema, useForm } from '@formily/react';
import { uid } from '@formily/shared';
import {
ActionContextProvider,
SchemaComponent,
useActionContext,
useRecord,
useResourceActionContext,
useResourceContext,
} from '@nocobase/client';
import { Button, Dropdown, MenuProps } from 'antd';
import React, { useMemo, useState } from 'react';
import { useChartQueryMetadataContext } from '../ChartQueryMetadataProvider';
import { lang } from '../locale';
import { getQueryTypeSchema } from './queryTypes';
const useCreateAction = () => {
const { setVisible } = useActionContext();
const form = useForm();
const { refresh } = useResourceActionContext();
const { resource } = useResourceContext();
const ctx = useChartQueryMetadataContext();
return {
async run() {
await form.submit();
await resource.create({ values: form.values });
setVisible(false);
await form.reset();
refresh();
ctx.refresh();
},
};
};
const useUpdateAction = () => {
const { setVisible } = useActionContext();
const form = useForm();
const { refresh } = useResourceActionContext();
const { resource, targetKey } = useResourceContext();
const { [targetKey]: filterByTk } = useRecord();
const ctx = useChartQueryMetadataContext();
return {
async run() {
await form.submit();
await resource.update({ filterByTk, values: form.values });
setVisible(false);
await form.reset();
refresh();
ctx.refresh();
},
};
};
const useCloseAction = () => {
const { setVisible } = useActionContext();
return {
async run() {
setVisible(false);
},
};
};
const getSchema = (initialValue, { form, isNewRecord }) => {
const type = initialValue.type;
const schema: ISchema = {
type: 'void',
name: uid(),
'x-component': 'Action.Drawer',
'x-decorator': 'Form',
'x-decorator-props': {
form,
// initialValue: JSON.parse(JSON.stringify(initialValue)),
},
title: isNewRecord ? lang('Add query') : lang('Edit query'),
properties: {
title: {
title: lang('Title'),
required: true,
'x-component': 'Input',
'x-decorator': 'FormItem',
},
options: getQueryTypeSchema(type),
footer: {
type: 'void',
'x-component': 'Action.Drawer.Footer',
properties: {
cancel: {
'x-component': 'Action',
title: lang('Cancel'),
'x-component-props': {
useAction: '{{ useCloseAction }}',
},
},
submit: {
'x-component': 'Action',
title: lang('Submit'),
'x-component-props': {
type: 'primary',
useAction: '{{ useSubmitAction }}',
},
},
},
},
},
};
return schema;
};
export const AddNewQuery = () => {
const [visible, setVisible] = useState(false);
const [schema, setSchema] = useState({});
const form = useMemo(() => createForm(), []);
const menu = useMemo<MenuProps>(() => {
return {
onClick: (info) => {
setVisible(true);
form.setValues({ type: info.key });
setSchema(getSchema({ type: info.key }, { form, isNewRecord: true }));
},
items: [
{
key: 'json',
label: 'JSON',
},
{
key: 'sql',
label: 'SQL',
},
{
key: 'api',
label: 'API',
disabled: true,
},
{
key: 'collection',
label: 'Collection',
disabled: true,
},
],
};
}, [form]);
return (
<ActionContextProvider value={{ visible, setVisible }}>
<Dropdown menu={menu}>
<Button icon={<PlusOutlined />} type={'primary'}>
{lang('Add query')} <DownOutlined />
</Button>
</Dropdown>
<SchemaComponent schema={schema} scope={{ useCloseAction, useSubmitAction: useCreateAction }} />
</ActionContextProvider>
);
};
export const EditQuery = () => {
const [visible, setVisible] = useState(false);
const record = useRecord();
const form = useMemo(() => createForm(), []);
const schema = getSchema(record, { form, isNewRecord: false });
return (
<ActionContextProvider value={{ visible, setVisible }}>
<a
onClick={() => {
form.setValues(record);
setVisible(true);
}}
>
{lang('Edit')}
</a>
<SchemaComponent schema={schema} scope={{ useCloseAction, useSubmitAction: useUpdateAction }} />
</ActionContextProvider>
);
};