From 151c3a32b8413d10d7ed87837967f5b8602530c7 Mon Sep 17 00:00:00 2001 From: chenos Date: Tue, 21 Jun 2022 11:30:37 +0800 Subject: [PATCH] feat: improve chart component --- .../schema-component/antd/g2plot/G2Plot.tsx | 36 +++++++++++++------ .../antd/g2plot/G2PlotDesigner.tsx | 22 ++++++++++-- .../antd/g2plot/demos/demo1.tsx | 12 ++++--- 3 files changed, 52 insertions(+), 18 deletions(-) diff --git a/packages/core/client/src/schema-component/antd/g2plot/G2Plot.tsx b/packages/core/client/src/schema-component/antd/g2plot/G2Plot.tsx index a64943111..93ff1fafa 100644 --- a/packages/core/client/src/schema-component/antd/g2plot/G2Plot.tsx +++ b/packages/core/client/src/schema-component/antd/g2plot/G2Plot.tsx @@ -5,6 +5,7 @@ import { Spin } from 'antd'; import cls from 'classnames'; import React, { forwardRef, useEffect, useRef } from 'react'; import { useTranslation } from 'react-i18next'; +import { useAPIClient } from '../../../api-client'; import { G2PlotDesigner } from './G2PlotDesigner'; export type ReactG2PlotProps = { @@ -60,28 +61,43 @@ export const G2Plot: any = observer((props: any) => { const { plot, config } = props; const field = useField(); const { t } = useTranslation(); + const api = useAPIClient(); useEffect(() => { field.data = field.data || {}; - if (typeof config?.data?.then === 'function') { - field.data.loaded = false; - config?.data?.then((data) => { - field.componentProps.config.data = data; - field.data.loaded = true; - }); + field.data.loading = true; + const fn = config?.data; + if (typeof fn === 'function') { + const result = fn.bind({ api })(); + if (result?.then) { + result.then(data => { + if (Array.isArray(data)) { + field.componentProps.config.data = data; + } + field.data.loading = false; + }); + } else { + field.data.loading = false; + } } else { - field.data.loaded = true; + field.data.loading = false; } - }, []); + }, []) if (!plot || !config) { return
{t('In configuration')}...
; } - if (!field?.data?.loaded) { + if (field?.data?.loading !== false) { return ; } return (
{field.title &&

{field.title}

} - +
); }); diff --git a/packages/core/client/src/schema-component/antd/g2plot/G2PlotDesigner.tsx b/packages/core/client/src/schema-component/antd/g2plot/G2PlotDesigner.tsx index 7e582236c..aec0ee646 100644 --- a/packages/core/client/src/schema-component/antd/g2plot/G2PlotDesigner.tsx +++ b/packages/core/client/src/schema-component/antd/g2plot/G2PlotDesigner.tsx @@ -1,6 +1,7 @@ import { ISchema, useField, useFieldSchema } from '@formily/react'; import React from 'react'; import { useTranslation } from 'react-i18next'; +import { useAPIClient } from '../../../api-client'; import { GeneralSchemaDesigner, SchemaSettings } from '../../../schema-settings'; import { useCompile, useDesignable } from '../../hooks'; @@ -29,9 +30,9 @@ export const G2PlotDesigner = () => { const fieldSchema = useFieldSchema(); const field = useField(); const compile = useCompile(); + const api = useAPIClient(); return ( - { }, } as ISchema } - onSubmit={({ plot, title, config }) => { + // {{ fetchData(api, { url: 'chartData:get' }) }} + onSubmit={async ({ plot, title, config }) => { field.title = compile(title); field.componentProps.plot = plot; - field.componentProps.config = compile(JSON.parse(config)); + const conf = compile(JSON.parse(config)); + const fn = conf?.data; + if (typeof fn === 'function') { + const result = fn.bind({ api })(); + if (result?.then) { + result.then(data => { + if (Array.isArray(data)) { + field.componentProps.config.data = data; + } + }); + } + } else { + field.componentProps.config = conf; + } fieldSchema.title = title; fieldSchema['x-component-props']['plot'] = plot; fieldSchema['x-component-props']['config'] = JSON.parse(config); dn.emit('patch', { schema: { + title, 'x-uid': fieldSchema['x-uid'], 'x-component-props': fieldSchema['x-component-props'], }, diff --git a/packages/core/client/src/schema-component/antd/g2plot/demos/demo1.tsx b/packages/core/client/src/schema-component/antd/g2plot/demos/demo1.tsx index 794bfbca7..92cbf2242 100644 --- a/packages/core/client/src/schema-component/antd/g2plot/demos/demo1.tsx +++ b/packages/core/client/src/schema-component/antd/g2plot/demos/demo1.tsx @@ -47,9 +47,11 @@ mock.onGet('/test').reply(200, { ], }); -const fetchData = async (api: APIClient, options) => { - const response = await api.request(options); - return response?.data?.data; +const requestChartData = (options) => { + return async function (this: { api: APIClient }) { + const response = await this.api.request(options); + return response?.data?.data; + }; }; const schema = { @@ -61,7 +63,7 @@ const schema = { 'x-component-props': { plot: 'Line', config: { - data: '{{ fetchData(api, { url: "/test" }) }}', + data: '{{ requestChartData({ url: "/test" }) }}', padding: 'auto', xField: 'Date', yField: 'scales', @@ -77,7 +79,7 @@ export default () => { return ( - + );