Skip to content

An open source alternative to Tableau. Embeddable visual analytic

feat: change component type to override (#293)

islxyqwe97f36fbd55b0
diff --git a/packages/graphic-walker/package.json b/packages/graphic-walker/package.json
index d988a284..1ff4c3b7 100644
--- a/packages/graphic-walker/package.json
+++ b/packages/graphic-walker/package.json
@@ -95,6 +95,7 @@
"@vercel/analytics": "^0.1.8",
"@vitejs/plugin-react": "^3.1.0",
"styled-components": "^5.3.6",
+ "ts-xor": "^1.3.0",
"typescript": "^5.1.6",
"vite": "^4.1.1"
},
diff --git a/packages/graphic-walker/src/App.tsx b/packages/graphic-walker/src/App.tsx
index 83d6b777..96727bb5 100644
--- a/packages/graphic-walker/src/App.tsx
+++ b/packages/graphic-walker/src/App.tsx
@@ -1,7 +1,7 @@
import React, { useEffect, useMemo, useRef, useCallback } from 'react';
import { observer } from 'mobx-react-lite';
import { useTranslation } from 'react-i18next';
-import { ISegmentKey, IAppI18nProps, IVizProps, IErrorHandlerProps, IVizAppProps, ISpecProps, IComputationContextProps } from './interfaces';
+import { ISegmentKey, IAppI18nProps, IVizProps, IErrorHandlerProps, IVizAppProps, ISpecProps, IComputationContextProps, IRemoteComputationProps, IComputationProps } from './interfaces';
import type { IReactVegaHandler } from './vis/react-vega';
import VisualSettings from './visualSettings';
import PosFields from './fields/posFields';
@@ -232,7 +232,7 @@ export const VizApp = observer(function VizApp(props: BaseVizProps) {
);
});
-export function VizAppWithContext(props: IVizAppProps) {
+export function VizAppWithContext(props: IVizAppProps & IComputationProps) {
const { computation, safeMetas, onMetaChange } = useMemo(() => {
if (props.dataSource) {
if (props.fieldKeyGuard) {
diff --git a/packages/graphic-walker/src/Table.tsx b/packages/graphic-walker/src/Table.tsx
index 9c7b754d..a610c38d 100644
--- a/packages/graphic-walker/src/Table.tsx
+++ b/packages/graphic-walker/src/Table.tsx
@@ -2,7 +2,7 @@ import { observer } from 'mobx-react-lite';
import React, { useEffect, useCallback, useMemo } from 'react';
import { ErrorBoundary } from 'react-error-boundary';
import { useTranslation } from 'react-i18next';
-import { IAppI18nProps, IErrorHandlerProps, IComputationContextProps, ITableProps, ITableSpecProps } from './interfaces';
+import { IAppI18nProps, IErrorHandlerProps, IComputationContextProps, ITableProps, ITableSpecProps, IComputationProps } from './interfaces';
import { mergeLocaleRes, setLocaleLanguage } from './locales/i18n';
import { useVizStore, withErrorReport, withTimeout, ComputationContext, VizStoreWrapper } from './store';
import { parseErrorMessage } from './utils';
@@ -76,7 +76,7 @@ export const TableApp = observer(function VizApp(props: BaseTableProps) {
);
});
-export function TableAppWithContext(props: ITableProps) {
+export function TableAppWithContext(props: ITableProps & IComputationProps) {
const { computation, safeMetas } = useMemo(() => {
if (props.dataSource) {
if (props.fieldKeyGuard) {
diff --git a/packages/graphic-walker/src/components/filterWalker/index.tsx b/packages/graphic-walker/src/components/filterWalker/index.tsx
index 64ae3be7..0116c8bc 100644
--- a/packages/graphic-walker/src/components/filterWalker/index.tsx
+++ b/packages/graphic-walker/src/components/filterWalker/index.tsx
@@ -261,6 +261,21 @@ export function createFilterContext(components: {
return <>{loadingContent}</>;
}
return children(filteredComputation, elements);
+ } as {
+ (props: {
+ configs: FilterConfig[];
+ dataSource: IRow[];
+ loadingContent?: React.ReactNode | Iterable<React.ReactNode>;
+ rawFields: IMutField[];
+ children: (computation: IComputationFunction, filterComponents: JSX.Element[]) => React.ReactNode;
+ }): JSX.Element;
+ (props: {
+ configs: FilterConfig[];
+ computation: IComputationFunction;
+ loadingContent?: React.ReactNode | Iterable<React.ReactNode>;
+ rawFields: IMutField[];
+ children: (computation: IComputationFunction, filterComponents: JSX.Element[]) => React.ReactNode;
+ }): JSX.Element;
};
}
diff --git a/packages/graphic-walker/src/index.tsx b/packages/graphic-walker/src/index.tsx
index 3bcbe8e8..b8e09d0e 100644
--- a/packages/graphic-walker/src/index.tsx
+++ b/packages/graphic-walker/src/index.tsx
@@ -7,18 +7,27 @@ import AppRoot from './components/appRoot';
import type {
IDataSourceListener,
IDataSourceProvider,
- IChart, IGWHandler,
+ IChart,
+ IGWHandler,
IGWHandlerInsider,
IGWProps,
- IMutField, IRow, ITableProps,
+ IMutField,
+ IRow,
+ ITableProps,
IVizAppProps,
+ ILocalComputationProps,
+ IRemoteComputationProps,
+ IComputationProps,
} from './interfaces';
import './empty_sheet.css';
import { TableAppWithContext } from './Table';
+export type ILocalVizAppProps = IVizAppProps & ILocalComputationProps & React.RefAttributes<IGWHandler>;
+export type IRemoteVizAppProps = IVizAppProps & IRemoteComputationProps & React.RefAttributes<IGWHandler>;
+
export const GraphicWalker = observer(
- forwardRef<IGWHandler, IVizAppProps>((props, ref) => {
+ forwardRef<IGWHandler, IVizAppProps & (ILocalComputationProps | IRemoteComputationProps)>((props, ref) => {
const [shadowRoot, setShadowRoot] = useState<ShadowRoot | null>(null);
const handleMount = (shadowRoot: ShadowRoot) => {
@@ -38,10 +47,16 @@ export const GraphicWalker = observer(
</AppRoot>
);
})
-);
+) as {
+ (p: ILocalVizAppProps): JSX.Element;
+ (p: IRemoteVizAppProps): JSX.Element;
+};
+
+export type ILocalTableProps = ITableProps & ILocalComputationProps & React.RefAttributes<IGWHandler>;
+export type IRemoteTableProps = ITableProps & IRemoteComputationProps & React.RefAttributes<IGWHandler>;
export const TableWalker = observer(
- forwardRef<IGWHandler, ITableProps>((props, ref) => {
+ forwardRef<IGWHandler, ITableProps & IComputationProps>((props, ref) => {
const [shadowRoot, setShadowRoot] = useState<ShadowRoot | null>(null);
const handleMount = (shadowRoot: ShadowRoot) => {
@@ -61,9 +76,13 @@ export const TableWalker = observer(
</AppRoot>
);
})
-);
+) as {
+ (p: ILocalTableProps): JSX.Element;
+ (p: IRemoteTableProps): JSX.Element;
+};
export { default as PureRenderer } from './renderer/pureRenderer';
+export type { ILocalPureRendererProps, IRemotePureRendererProps } from './renderer/pureRenderer';
export { embedGraphicWalker } from './vanilla';
export type { IGWProps, ITableProps, IVizAppProps, IDataSourceProvider, IMutField, IRow, IDataSourceListener, IChart };
export { VizSpecStore } from './store/visualSpecStore';
@@ -73,7 +92,7 @@ export { getGlobalConfig } from './config';
export { DataSourceSegmentComponent } from './dataSource';
export * from './models/visSpecHistory';
export * from './dataSourceProvider';
-
export { getComputation } from './computation/clientComputation';
export { addFilterForQuery, chartToWorkflow } from './utils/workflow';
export * from './components/filterWalker';
+
diff --git a/packages/graphic-walker/src/interfaces.ts b/packages/graphic-walker/src/interfaces.ts
index e3ab5880..131befcc 100644
--- a/packages/graphic-walker/src/interfaces.ts
+++ b/packages/graphic-walker/src/interfaces.ts
@@ -7,6 +7,7 @@ import { ToolbarItemProps } from './components/toolbar';
import { GWGlobalConfig } from './vis/theme';
import { VizSpecStore } from './store/visualSpecStore';
import { CommonStore } from './store/commonStore';
+import type { XOR } from 'ts-xor';
export type DeepReadonly<T extends Record<keyof any, any>> = {
readonly [K in keyof T]: T[K] extends Record<keyof any, any> ? DeepReadonly<T[K]> : T[K];
@@ -895,28 +896,26 @@ export interface IVizStoreProps {
onMetaChange?: (fid: string, meta: Partial<IMutField>) => void;
}
-export type IComputationProps = {
+export interface ILocalComputationProps {
+ /**
+ * auto parse field key into a safe string. default is true
+ */
+ fieldKeyGuard?: boolean;
+ dataSource: any[];
computationTimeout?: number;
-} & (
- | {
- /**
- * auto parse field key into a safe string. default is true
- */
- fieldKeyGuard?: boolean;
- dataSource: any[];
- }
- | {
- fieldKeyGuard?: undefined;
- dataSource?: undefined;
- computation: IComputationFunction;
- }
-);
+}
+
+export interface IRemoteComputationProps {
+ computation: IComputationFunction;
+}
export interface IComputationContextProps {
computation?: IComputationFunction;
computationTimeout?: number;
}
+export type IComputationProps = XOR<IRemoteComputationProps,ILocalComputationProps>;
+
export type IGWProps = IAppI18nProps &
IVizProps &
IThemeProps &
@@ -934,9 +933,8 @@ export interface ITableSpecProps {
pageSize?: number;
}
-export type IVizAppProps = IAppI18nProps & IVizProps & IThemeProps & IErrorHandlerProps & IVizStoreProps & IComputationProps & ISpecProps;
-
-export type ITableProps = IAppI18nProps & IThemeProps & IErrorHandlerProps & IVizStoreProps & IComputationProps & ITableSpecProps;
+export interface IVizAppProps extends IAppI18nProps, IVizProps, IThemeProps, IErrorHandlerProps, IVizStoreProps, ISpecProps {}
+export interface ITableProps extends IAppI18nProps, ITableSpecProps, IThemeProps, IErrorHandlerProps, IVizStoreProps {}
export interface IAskVizFeedback {
action: 'voteup' | 'votedown' | 'report';
diff --git a/packages/graphic-walker/src/renderer/pureRenderer.tsx b/packages/graphic-walker/src/renderer/pureRenderer.tsx
index 2664c1a0..ba7da0cf 100644
--- a/packages/graphic-walker/src/renderer/pureRenderer.tsx
+++ b/packages/graphic-walker/src/renderer/pureRenderer.tsx
@@ -23,35 +23,37 @@ import { getComputation } from '../computation/clientComputation';
import { getSort } from '../utils';
import { GWGlobalConfig } from '../vis/theme';
-type IPureRendererProps =
- | {
- className?: string;
- name?: string;
- themeKey?: IThemeKey;
- themeConfig?: GWGlobalConfig;
- dark?: IDarkMode;
- visualState: DraggableFieldState;
- visualConfig: IVisualConfigNew | IVisualConfig;
- visualLayout?: IVisualLayout;
- locale?: string;
- channelScales?: IChannelScales;
- overrideSize?: IVisualLayout['size'];
- } & (
- | {
- type: 'remote';
- computation: IComputationFunction;
- }
- | {
- type?: 'local';
- rawData: IRow[];
- }
- );
+type IPureRendererProps = {
+ className?: string;
+ name?: string;
+ themeKey?: IThemeKey;
+ themeConfig?: GWGlobalConfig;
+ dark?: IDarkMode;
+ visualState: DraggableFieldState;
+ visualConfig: IVisualConfigNew | IVisualConfig;
+ visualLayout?: IVisualLayout;
+ locale?: string;
+ channelScales?: IChannelScales;
+ overrideSize?: IVisualLayout['size'];
+};
+
+type LocalProps = {
+ type?: 'local';
+ rawData: IRow[];
+};
+
+type RemoteProps = {
+ type: 'remote';
+ computation: IComputationFunction;
+};
+export type IRemotePureRendererProps = IPureRendererProps & RemoteProps & React.RefAttributes<IReactVegaHandler>;
+export type ILocalPureRendererProps = IPureRendererProps & LocalProps & React.RefAttributes<IReactVegaHandler>;
/**
* Render a readonly chart with given visualization schema.
* This is a pure component, which means it will not depend on any global state.
*/
-const PureRenderer = forwardRef<IReactVegaHandler, IPureRendererProps>(function PureRenderer(props, ref) {
+const PureRenderer = forwardRef<IReactVegaHandler, IPureRendererProps & (LocalProps | RemoteProps)>(function PureRenderer(props, ref) {
const { name, className, themeKey, dark, visualState, visualConfig, visualLayout: layout, overrideSize, locale, type, themeConfig, channelScales } = props;
const computation = useMemo(() => {
if (props.type === 'remote') {
@@ -125,8 +127,8 @@ const PureRenderer = forwardRef<IReactVegaHandler, IPureRendererProps>(function
const isSpatial = coordSystem === 'geographic';
return (
- <ShadowDom style={sizeMode === 'full' ? {width: '100%', height: '100%'} : undefined} className={className}>
- <div className="relative" style={sizeMode === 'full' ? {width: '100%', height: '100%'} : undefined}>
+ <ShadowDom style={sizeMode === 'full' ? { width: '100%', height: '100%' } : undefined} className={className}>
+ <div className="relative" style={sizeMode === 'full' ? { width: '100%', height: '100%' } : undefined}>
{isSpatial && (
<div className="max-w-full" style={{ height: LEAFLET_DEFAULT_HEIGHT, flexGrow: 1 }}>
<LeafletRenderer data={data} draggableFieldState={visualState} visualConfig={visualConfig} visualLayout={visualLayout} />
@@ -153,4 +155,7 @@ const PureRenderer = forwardRef<IReactVegaHandler, IPureRendererProps>(function
);
});
-export default observer(withAppRoot<IPureRendererProps>(PureRenderer));
+export default observer(withAppRoot<IPureRendererProps>(PureRenderer)) as {
+ (p: ILocalPureRendererProps): JSX.Element;
+ (p: IRemotePureRendererProps): JSX.Element;
+};
diff --git a/yarn.lock b/yarn.lock
index 92027f33..4663600e 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -4627,6 +4627,11 @@ ts-jest@^29.1.1:
semver "^7.5.3"
yargs-parser "^21.0.1"
+ts-xor@^1.3.0:
+ version "1.3.0"
+ resolved "https://registry.npmmirror.com/ts-xor/-/ts-xor-1.3.0.tgz#3e59f24f0321f9f10f350e0cee3b534b89a2c70b"
+ integrity sha512-RLXVjliCzc1gfKQFLRpfeD0rrWmjnSTgj7+RFhoq3KRkUYa8LE/TIidYOzM5h+IdFBDSjjSgk9Lto9sdMfDFEA==
+
tslib@^2.0.0, tslib@^2.4.0, tslib@^2.5.3, tslib@^2.6.1, tslib@~2.6.2:
version "2.6.2"
resolved "https://registry.npmjs.org/tslib/-/tslib-2.6.2.tgz#703ac29425e7b37cd6fd456e92404d46d1f3e4ae"