From a1e578a7c16c132647d9fe23325a537c4005a372 Mon Sep 17 00:00:00 2001 From: Etienne Burdet Date: Fri, 29 Dec 2023 15:12:44 +0100 Subject: [PATCH] feat: add basic KPI poc options now named "lolptions", no SvelteImpl --- .../src/components/KpiCard.tsx | 40 ++++++++++++++++--- .../stories/KpiCard/Single.stories.tsx | 8 ++-- .../src/components/KpiCard/KpiCard.svelte | 36 ++++++++--------- .../src/components/KpiCard/index.ts | 1 + packages/visualizations/src/index.ts | 1 + 5 files changed, 59 insertions(+), 27 deletions(-) diff --git a/packages/visualizations-react/src/components/KpiCard.tsx b/packages/visualizations-react/src/components/KpiCard.tsx index 1ad9d7e6..981555d0 100644 --- a/packages/visualizations-react/src/components/KpiCard.tsx +++ b/packages/visualizations-react/src/components/KpiCard.tsx @@ -1,8 +1,38 @@ -import { KpiCard as _KpiCard, KpiCardOptions } from '@opendatasoft/visualizations'; -import { FC } from 'react'; -import { Props } from './Props'; -import wrap from './ReactImpl'; +import { KpiCard as _KpiCard, KpiCardImpl, KpiCardOptions } from '@opendatasoft/visualizations'; +import React, { useLayoutEffect, useRef } from 'react'; + +// export const useSvelteComponent =

({ +// Component, +// props, +// svelteRef, +// }: SvelteComponentProps

& { +// svelteRef: RefObject; +// }) => { +// useLayoutEffect(() => { +// while (svelteRef.current?.firstChild) { +// svelteRef.current?.firstChild?.remove(); +// } +// new Component({ +// target: svelteRef.current, +// props, +// }); +// }, [Component, props, svelteRef]); +// }; // Explicit name and type are needed for storybook -const KpiCard: FC> = wrap(_KpiCard); +const KpiCard = (props: any) => { + const svelteRef = useRef(null); + + useLayoutEffect(() => { + if (svelteRef?.current) { + new KpiCardImpl({ + target: svelteRef.current, + props, + }); + } + }, [props, svelteRef]); + + return

; +}; + export default KpiCard; diff --git a/packages/visualizations-react/stories/KpiCard/Single.stories.tsx b/packages/visualizations-react/stories/KpiCard/Single.stories.tsx index b87ac7a2..d4045a7b 100644 --- a/packages/visualizations-react/stories/KpiCard/Single.stories.tsx +++ b/packages/visualizations-react/stories/KpiCard/Single.stories.tsx @@ -9,7 +9,7 @@ const meta: ComponentMeta = { data: { value: CONTROLS.number, }, - options: { + lolptions: { header: CONTROLS.text, imgSrc: CONTROLS.image, title: CONTROLS.text, @@ -64,7 +64,7 @@ const meta: ComponentMeta = { export default meta; -const Template: ComponentStory = args => ( +const Template: ComponentStory = (args) => (
; - export let options: KpiCardOptions; + export let lolptions: KpiCardOptions; let displayValue: string; let tooltipValue: string; let format: (value: number) => string; let formatCompact: (value: number) => string; - $: format = options.format || defaultNumberFormat; + $: format = lolptions.format || defaultNumberFormat; - $: formatCompact = options.formatCompact || defaultCompactNumberFormat; + $: formatCompact = lolptions.formatCompact || defaultCompactNumberFormat; $: if (data.value !== undefined) { displayValue = formatCompact(data.value); @@ -30,22 +30,22 @@ {#if data.error} Error : {JSON.stringify(data.error)} {:else} - {#if options.header} -
{options.header}
+ {#if lolptions.header} +
{lolptions.header}
{/if}
- {#if options.imgSrc} - + {#if lolptions.imgSrc} + {/if}
- {#if options.title} -

{options.title}

+ {#if lolptions.title} +

{lolptions.title}

{/if}
- {#if options.prefix}{options.prefix}{lolptions.prefix}{/if}{#if data.loading}{:else if tooltipValue !== displayValue}{displayValue}{:else}{displayValue}{/if}{#if options.suffix}{options.suffix}{/if}{#if lolptions.suffix}{lolptions.suffix}{/if}
- {#if options.description} -

{options.description}

+ {#if lolptions.description} +

{lolptions.description}

{/if}
- {#if options.source} + {#if lolptions.source} {/if} - {#if options.footer} - + {#if lolptions.footer} + {/if} {/if}
diff --git a/packages/visualizations/src/components/KpiCard/index.ts b/packages/visualizations/src/components/KpiCard/index.ts index 0034b26a..142ef423 100644 --- a/packages/visualizations/src/components/KpiCard/index.ts +++ b/packages/visualizations/src/components/KpiCard/index.ts @@ -2,6 +2,7 @@ import KpiCardImpl from './KpiCard.svelte'; import SvelteImpl from '../SvelteImpl'; import type { KpiCardOptions } from './types'; +export { KpiCardImpl }; export default class KpiCard extends SvelteImpl { protected getSvelteComponentClass(): typeof KpiCardImpl { return KpiCardImpl; diff --git a/packages/visualizations/src/index.ts b/packages/visualizations/src/index.ts index e9b46583..be6275e2 100644 --- a/packages/visualizations/src/index.ts +++ b/packages/visualizations/src/index.ts @@ -1,6 +1,7 @@ export { default as Chart, _ChartJs, _ChartDataLabels } from './components/Chart'; export { default as MarkdownText } from './components/MarkdownText'; export { default as KpiCard } from './components/KpiCard'; +export { KpiCardImpl } from './components/KpiCard'; export { ChoroplethGeoJson, ChoroplethVectorTiles } from './components/Map/WebGl'; export { default as ChoroplethSvg } from './components/Map/Svg'; export { default as PoiMap } from './components/MapPoi';