Skip to content

Commit

Permalink
feat(heatmap): migrate canvas implementation to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
plouc committed Jan 12, 2022
1 parent 15efb6e commit d0cd500
Show file tree
Hide file tree
Showing 19 changed files with 704 additions and 855 deletions.
34 changes: 23 additions & 11 deletions packages/heatmap/src/HeatMap.tsx
Expand Up @@ -7,7 +7,7 @@ import {
HeatMapDatum,
HeatMapCommonProps,
HeatMapSvgProps,
LayerId,
LayerId, CustomLayerProps,
} from './types'
import { useHeatMap } from './hooks'
import { svgDefaultProps } from './defaults'
Expand All @@ -28,13 +28,15 @@ const InnerHeatMap = <Datum extends HeatMapDatum, ExtraProps extends object>({
width,
height,
margin: partialMargin,
forceSquare = svgDefaultProps.forceSquare,
// forceSquare = svgDefaultProps.forceSquare,
xInnerPadding = svgDefaultProps.xInnerPadding,
xOuterPadding = svgDefaultProps.xOuterPadding,
yInnerPadding = svgDefaultProps.yInnerPadding,
yOuterPadding = svgDefaultProps.yOuterPadding,
sizeVariation = svgDefaultProps.sizeVariation,
cellComponent = svgDefaultProps.cellComponent,
// sizeVariation = svgDefaultProps.sizeVariation,
cellComponent = svgDefaultProps.cellComponent as NonNullable<
HeatMapSvgProps<Datum, ExtraProps>['cellComponent']
>,
opacity = svgDefaultProps.opacity,
activeOpacity = svgDefaultProps.activeOpacity,
inactiveOpacity = svgDefaultProps.inactiveOpacity,
Expand All @@ -48,10 +50,10 @@ const InnerHeatMap = <Datum extends HeatMapDatum, ExtraProps extends object>({
axisBottom = svgDefaultProps.axisBottom,
axisLeft = svgDefaultProps.axisLeft,
enableLabels = svgDefaultProps.enableLabels,
label = svgDefaultProps.label,
labelTextColor = svgDefaultProps.labelTextColor,
colors = svgDefaultProps.colors,
nanColor = svgDefaultProps.nanColor,
label = svgDefaultProps.label as HeatMapCommonProps<Datum>['label'],
labelTextColor = svgDefaultProps.labelTextColor as HeatMapCommonProps<Datum>['labelTextColor'],
colors = svgDefaultProps.colors as HeatMapCommonProps<Datum>['colors'],
emptyColor = svgDefaultProps.emptyColor,
legends = svgDefaultProps.legends,
annotations = svgDefaultProps.annotations as HeatMapCommonProps<Datum>['annotations'],
isInteractive = svgDefaultProps.isInteractive,
Expand All @@ -72,7 +74,7 @@ const InnerHeatMap = <Datum extends HeatMapDatum, ExtraProps extends object>({
partialMargin
)

const { xScale, yScale, cells, colorScale } = useHeatMap<Datum, ExtraProps>({
const { xScale, yScale, cells, colorScale, activeCell, setActiveCell } = useHeatMap<Datum, ExtraProps>({
data,
valueFormat,
width: innerWidth,
Expand All @@ -82,12 +84,14 @@ const InnerHeatMap = <Datum extends HeatMapDatum, ExtraProps extends object>({
yInnerPadding,
yOuterPadding,
colors,
emptyColor,
opacity,
activeOpacity,
inactiveOpacity,
borderColor,
label,
labelTextColor,
hoverTarget,
})

const layerById: Record<LayerId, ReactNode> = {
Expand Down Expand Up @@ -131,9 +135,11 @@ const InnerHeatMap = <Datum extends HeatMapDatum, ExtraProps extends object>({
<Fragment key="cells">
<HeatMapCells<Datum, ExtraProps>
cells={cells}
cellComponent={cellComponent}
borderRadius={borderRadius}
borderWidth={borderWidth}
isInteractive={isInteractive}
setActiveCell={setActiveCell}
onMouseEnter={onMouseEnter}
onMouseMove={onMouseMove}
onMouseLeave={onMouseLeave}
Expand All @@ -145,7 +151,7 @@ const InnerHeatMap = <Datum extends HeatMapDatum, ExtraProps extends object>({
)
}

if (layers.includes('legends')) {
if (layers.includes('legends') && colorScale !== null) {
layerById.legends = (
<Fragment key="legends">
{legends.map((legend, index) => (
Expand All @@ -171,6 +177,12 @@ const InnerHeatMap = <Datum extends HeatMapDatum, ExtraProps extends object>({
)
}

const customLayerProps: CustomLayerProps<Datum> = {
cells,
activeCell,
setActiveCell
}

return (
<SvgWrapper
width={outerWidth}
Expand All @@ -186,7 +198,7 @@ const InnerHeatMap = <Datum extends HeatMapDatum, ExtraProps extends object>({
>
{layers.map((layer, i) => {
if (typeof layer === 'function') {
return <Fragment key={i}>{createElement(layer, {})}</Fragment>
return <Fragment key={i}>{createElement(layer, customLayerProps)}</Fragment>
}

return layerById?.[layer] ?? null
Expand Down

0 comments on commit d0cd500

Please sign in to comment.