/
HeatMapCellRect.tsx
72 lines (68 loc) · 2.38 KB
/
HeatMapCellRect.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { memo, useMemo } from 'react'
import { animated, to } from '@react-spring/web'
import { useTheme } from '@nivo/core'
import { CellComponentProps, HeatMapDatum } from './types'
const NonMemoizedHeatMapCellRect = <Datum extends HeatMapDatum>({
cell,
borderWidth,
borderRadius,
animatedProps,
onMouseEnter,
onMouseMove,
onMouseLeave,
onClick,
enableLabels,
}: CellComponentProps<Datum>) => {
const theme = useTheme()
const handlers = useMemo(
() => ({
onMouseEnter: onMouseEnter ? onMouseEnter(cell) : undefined,
onMouseMove: onMouseMove ? onMouseMove(cell) : undefined,
onMouseLeave: onMouseLeave ? onMouseLeave(cell) : undefined,
onClick: onClick ? onClick(cell) : undefined,
}),
[cell, onMouseEnter, onMouseMove, onMouseLeave, onClick]
)
return (
<animated.g
data-testid={`cell.${cell.id}`}
style={{ cursor: 'pointer' }}
opacity={animatedProps.opacity}
{...handlers}
transform={to(
[animatedProps.x, animatedProps.y, animatedProps.scale],
(x, y, scale) => `translate(${x}, ${y}) scale(${scale})`
)}
>
<animated.rect
transform={to(
[animatedProps.width, animatedProps.height],
(width, height) => `translate(${width * -0.5}, ${height * -0.5})`
)}
key={cell.id}
fill={animatedProps.color}
width={animatedProps.width}
height={animatedProps.height}
stroke={animatedProps.borderColor}
strokeWidth={borderWidth}
rx={borderRadius}
ry={borderRadius}
/>
{enableLabels && (
<animated.text
textAnchor="middle"
dominantBaseline="central"
fill={animatedProps.labelTextColor}
style={{
...theme.labels.text,
fill: undefined,
userSelect: 'none',
}}
>
{cell.label}
</animated.text>
)}
</animated.g>
)
}
export const HeatMapCellRect = memo(NonMemoizedHeatMapCellRect) as typeof NonMemoizedHeatMapCellRect