import React, { ForwardedRef, forwardRef, useEffect, useImperativeHandle, useState } from 'react'; import { ConfigProvider } from "antd"; import { LtmplConfig } from "../../tmpl/interface"; import HydrocarbonService from "../../tmpl/hcservice-v3"; import { useLocale } from "../../locale/useLocale"; import ActTable from "../../table/act-table"; import { DataConfigProps } from "../data"; import './index.less'; export interface ComponentStyle { table: { background?: string; pageSize?: number; padding?: number; fontSize?: number; }, header?: { background?: string; color?: string; borderRadius?: number; }; body?: { background?: string; color?: string; showEmpty?: boolean; }; border?: { show?: boolean; color?: string; } } export interface ComponentProps { style?: ComponentStyle; data?: DataConfigProps; initSearchParams?: { [key: string]: any }; // 全局初始化外部参数,例:传参包含设备id,则在查询时带上设备id } export interface ComponentRef { updateConfig: (newConfig: ComponentProps) => void; } const Index = forwardRef((props: ComponentProps, ref: ForwardedRef) => { const { style, data: propsData } = props; const [config, setConfig] = useState({ ...style }); const [dataConfig, setDataConfig] = useState({ ...propsData }); const [ltmplConfig, setLtmplConfig] = useState(); const [pageInfo, setPageInfo] = useState({ pageNo: 1, pageSize: style?.table?.pageSize || 10 }); const [initSearchParams, setInitSearchParams] = useState<{ [key: string]: any }>(); const { translate } = useLocale(); useImperativeHandle(ref, () => ({ updateConfig: (newConfig) => { const { style: newStyle, data: newDataConfig, initSearchParams: newISParams } = newConfig; setConfig({ ...newStyle }); setDataConfig({ ...newDataConfig }); setInitSearchParams(newISParams); }, })); // 获取数据源表格配置 const getLtmplConfig = async () => { const { ltmplConfig: temConfig } = await HydrocarbonService.requestLtmplConfig(dataConfig?.serverKey || null, dataConfig?.sourceId) || {}; setLtmplConfig(temConfig); }; // 数据源数据请求 useEffect(() => { if (dataConfig?.sourceType === 'sourceId' && dataConfig?.sourceId) { getLtmplConfig(); } else { setLtmplConfig(undefined); } }, [dataConfig?.sourceType, dataConfig?.sourceId]); return (
{ltmplConfig ? setPageInfo({ pageNo, pageSize })} criteriaData={initSearchParams || {}} showEmpty={config?.body?.showEmpty} /> :
{translate("${暂无数据}")}
}
); }); export default Index;