-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e3ed3a0
commit 5854348
Showing
14 changed files
with
191 additions
and
8 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
packages/visualizations-react/src/components/CategoryLegend.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { CategoryLegend as _CategoryLegend, CategoryLegendOptions } from '@opendatasoft/visualizations'; | ||
import { FC } from 'react'; | ||
import { SimpleProps } from './Props'; | ||
import wrap from './ReactSimpleImpl'; | ||
|
||
// Explicit name and type are needed for storybook | ||
const CategoryLegend: FC<SimpleProps<CategoryLegendOptions>> = wrap(_CategoryLegend); | ||
export default CategoryLegend; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/visualizations-react/src/components/ReactSimpleImpl.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { SimpleComponent } from '@opendatasoft/visualizations'; | ||
import React, { FC, ForwardedRef, forwardRef, useEffect, useRef } from 'react'; | ||
import { useMergeRefs } from 'use-callback-ref'; | ||
import { SimpleProps } from './Props'; | ||
|
||
// FIXME: Test the wrap method | ||
|
||
// Represent one of our simple component class's constructor like CategoryLegend | ||
type ComponentConstructor<Options, ComponentClass extends SimpleComponent<Options>> = | ||
new (container: HTMLElement, options: Options) => ComponentClass; | ||
|
||
// The wrapper build a function component for the given component class | ||
export default function wrap<Options, ComponentClass extends SimpleComponent<Options>>( | ||
ComponentConstructor: ComponentConstructor<Options, ComponentClass> | ||
): FC<SimpleProps<Options>> { | ||
// We use forwardRef to forward the actual ref of the container | ||
return forwardRef((props: SimpleProps< Options>, forwardedRef: ForwardedRef<HTMLElement>) => { | ||
const { tag, options, ...elementProps } = props; | ||
|
||
// This ref will hold our SDK component instance | ||
const componentRef = useRef<ComponentClass | null>(null); | ||
// This ref will hold the container element | ||
const containerRef = useRef<HTMLElement | null>(null); | ||
// By merging container ref and forwarded ref parent component could also access the container ref ! | ||
const ref = useMergeRefs([forwardedRef, containerRef]); | ||
|
||
// Update options (put before creating the component to skip the initial render) | ||
useEffect(() => { | ||
componentRef.current?.updateOptions(options); | ||
}, [options]); | ||
|
||
// Create and destroy | ||
useEffect(() => { | ||
const container = containerRef.current; | ||
if (container) { | ||
const component = new ComponentConstructor(container, options); | ||
componentRef.current = component; | ||
return () => { | ||
component.destroy(); | ||
componentRef.current = null; | ||
}; | ||
} | ||
|
||
throw new Error('Container was expected to be available in useEffect. This is a bug.'); | ||
// We only want to create or destroy on mount, hot reload or tag change. | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [tag]); | ||
|
||
// With React 17 we should be able to just use jsx runtime. | ||
return React.createElement(tag || 'div', { | ||
...elementProps, // such as style... | ||
ref, | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
export type { Props } from './components/Props'; | ||
export type { Props, SimpleProps } from './components/Props'; | ||
export { default as Chart } from './components/Chart'; | ||
export { default as MarkdownText } from './components/MarkdownText'; | ||
export { default as KpiCard } from './components/KpiCard'; | ||
export { default as ChoroplethGeoJson } from './components/ChoroplethGeoJson'; | ||
export { default as ChoroplethVectorTiles } from './components/ChoroplethVectorTiles'; | ||
export { default as ChoroplethSvg } from './components/ChoroplethSvg'; | ||
export { default as PoiMap } from './components/PoiMap'; | ||
export { default as CategoryLegend } from './components/CategoryLegend'; |
27 changes: 27 additions & 0 deletions
27
packages/visualizations-react/stories/Legend/CategoryLegend.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { ComponentMeta } from '@storybook/react'; | ||
import type { CategoryLegendOptions } from '@opendatasoft/visualizations'; | ||
import { CATEGORY_ITEM_VARIANT } from '@opendatasoft/visualizations'; | ||
import { CategoryLegend, SimpleProps } from '../../src'; | ||
import CategoryLegendTemplate from './CategoryLegendTemplate'; | ||
|
||
const meta: ComponentMeta<typeof CategoryLegend> = { | ||
title: 'Legend', | ||
component: CategoryLegend, | ||
}; | ||
|
||
export default meta; | ||
|
||
export const CategoryCircleLegend = CategoryLegendTemplate.bind({}); | ||
const CategoryCircleLegendArgs: SimpleProps<CategoryLegendOptions> = { | ||
options: { | ||
type: 'category' as const, | ||
title: "I Am Legend", | ||
items: [ | ||
{ label: 'category 1', color: '#F5C2C1', borderColor: 'red', variant: CATEGORY_ITEM_VARIANT.Circle }, | ||
{ label: 'category 2', color: '#90EE90', borderColor: 'green', variant: CATEGORY_ITEM_VARIANT.Circle }, | ||
{ label: 'category 3', color: '#ADD8E6', borderColor: 'blue', variant: CATEGORY_ITEM_VARIANT.Circle }, | ||
], | ||
align: 'start' as const, | ||
}, | ||
}; | ||
CategoryCircleLegend.args = CategoryCircleLegendArgs; |
20 changes: 20 additions & 0 deletions
20
packages/visualizations-react/stories/Legend/CategoryLegendTemplate.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import React from 'react'; | ||
import { ComponentStory } from '@storybook/react'; | ||
import { CategoryLegendOptions } from '@opendatasoft/visualizations'; | ||
import { CategoryLegend, SimpleProps } from '../../src'; | ||
|
||
const CategoryLegendTemplate: ComponentStory<typeof CategoryLegend> = ( | ||
args: SimpleProps<CategoryLegendOptions> | ||
) => ( | ||
<div | ||
style={{ | ||
display: 'flex', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
}} | ||
> | ||
<CategoryLegend {...args} style={{ width: '60vw' }} /> | ||
</div> | ||
); | ||
|
||
export default CategoryLegendTemplate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import CategoryLegendImpl from './CategoryLegend.svelte'; | ||
import SvelteSimpleImpl from '../SvelteSimpleImpl'; | ||
import type { CategoryLegendOptions } from './types'; | ||
|
||
export default class CategoryLegendComponent extends SvelteSimpleImpl<CategoryLegendOptions> { | ||
protected getSvelteSimpleComponentClass(): typeof CategoryLegendImpl { | ||
return CategoryLegendImpl; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/visualizations/src/components/SvelteSimpleImpl.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { SvelteComponent } from 'svelte'; | ||
import { SimpleComponent } from '../types'; | ||
|
||
export default abstract class SvelteImpl<Options> extends SimpleComponent<Options> { | ||
protected abstract getSvelteSimpleComponentClass(): typeof SvelteComponent; | ||
|
||
private svelteComponent: SvelteComponent | undefined; | ||
|
||
protected onCreate() { | ||
const ComponentClass = this.getSvelteSimpleComponentClass(); | ||
this.svelteComponent = new ComponentClass({ | ||
target: this.container, | ||
props: { | ||
options: this.options, | ||
}, | ||
}); | ||
} | ||
|
||
protected onOptionsUpdated() { | ||
this.svelteComponent?.$$set?.({ | ||
options: this.options, | ||
}); | ||
} | ||
|
||
protected onDestroy() { | ||
this.svelteComponent?.$destroy(); | ||
this.svelteComponent = undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters