1
- import { useTransition } from '@ react-spring/web '
2
- import { useMotionConfig } from '@nivo/core '
3
- import { ComputedBar , RadialBarArcGenerator , RadialBarCommonProps } from './types '
4
- import { RadialBarArc } from './RadialBarArc '
1
+ import { createElement , MouseEvent , useCallback } from 'react'
2
+ import { ArcsLayer , ArcGenerator } from '@nivo/arcs '
3
+ import { useTooltip } from '@nivo/tooltip '
4
+ import { ComputedBar , RadialBarCommonProps } from './types '
5
5
6
6
interface RadialBarArcsProps {
7
+ center : [ number , number ]
7
8
bars : ComputedBar [ ]
8
- arcGenerator : RadialBarArcGenerator
9
+ arcGenerator : ArcGenerator
9
10
isInteractive : RadialBarCommonProps [ 'isInteractive' ]
10
11
tooltip : RadialBarCommonProps [ 'tooltip' ]
11
12
onClick ?: RadialBarCommonProps [ 'onClick' ]
12
13
onMouseEnter ?: RadialBarCommonProps [ 'onMouseEnter' ]
13
14
onMouseMove ?: RadialBarCommonProps [ 'onMouseMove' ]
14
15
onMouseLeave ?: RadialBarCommonProps [ 'onMouseLeave' ]
16
+ transitionMode : RadialBarCommonProps [ 'transitionMode' ]
15
17
}
16
18
17
19
export const RadialBarArcs = ( {
20
+ center,
18
21
bars,
19
22
arcGenerator,
20
23
isInteractive,
@@ -23,71 +26,53 @@ export const RadialBarArcs = ({
23
26
onMouseEnter,
24
27
onMouseMove,
25
28
onMouseLeave,
29
+ transitionMode,
26
30
} : RadialBarArcsProps ) => {
27
- const { animate , config : springConfig } = useMotionConfig ( )
31
+ const { showTooltipFromEvent , hideTooltip } = useTooltip ( )
28
32
29
- const transition = useTransition <
30
- ComputedBar ,
31
- {
32
- startAngle : number
33
- endAngle : number
34
- innerRadius : number
35
- outerRadius : number
36
- color : string
37
- }
38
- > ( bars , {
39
- keys : bar => bar . id ,
40
- initial : bar => ( {
41
- startAngle : bar . startAngle ,
42
- endAngle : bar . endAngle ,
43
- innerRadius : bar . innerRadius ,
44
- outerRadius : bar . outerRadius ,
45
- color : bar . color ,
46
- } ) ,
47
- from : bar => ( {
48
- startAngle : bar . startAngle ,
49
- endAngle : bar . endAngle ,
50
- innerRadius : bar . innerRadius ,
51
- outerRadius : bar . outerRadius ,
52
- color : bar . color ,
53
- } ) ,
54
- update : bar => ( {
55
- startAngle : bar . startAngle ,
56
- endAngle : bar . endAngle ,
57
- innerRadius : bar . innerRadius ,
58
- outerRadius : bar . outerRadius ,
59
- color : bar . color ,
60
- } ) ,
61
- leave : bar => ( {
62
- startAngle : bar . startAngle + ( bar . endAngle - bar . startAngle ) / 2 ,
63
- endAngle : bar . startAngle + ( bar . endAngle - bar . startAngle ) / 2 ,
64
- innerRadius : bar . innerRadius ,
65
- outerRadius : bar . outerRadius ,
66
- color : bar . color ,
67
- } ) ,
68
- expires : true ,
69
- config : springConfig ,
70
- immediate : ! animate ,
71
- } )
33
+ const handleClick = useCallback (
34
+ ( bar : ComputedBar , event : MouseEvent ) => {
35
+ onClick ?.( bar , event )
36
+ } ,
37
+ [ onClick ]
38
+ )
39
+
40
+ const handleMouseEnter = useCallback (
41
+ ( bar : ComputedBar , event : MouseEvent ) => {
42
+ showTooltipFromEvent ( createElement ( tooltip , { bar } ) , event )
43
+ onMouseEnter ?.( bar , event )
44
+ } ,
45
+ [ showTooltipFromEvent , tooltip , onMouseEnter ]
46
+ )
47
+
48
+ const handleMouseMove = useCallback (
49
+ ( bar : ComputedBar , event : MouseEvent ) => {
50
+ showTooltipFromEvent ( createElement ( tooltip , { bar } ) , event )
51
+ onMouseMove ?.( bar , event )
52
+ } ,
53
+ [ showTooltipFromEvent , tooltip , onMouseMove ]
54
+ )
55
+
56
+ const handleMouseLeave = useCallback (
57
+ ( bar : ComputedBar , event : MouseEvent ) => {
58
+ hideTooltip ( )
59
+ onMouseLeave ?.( bar , event )
60
+ } ,
61
+ [ hideTooltip , onMouseLeave ]
62
+ )
72
63
73
64
return (
74
- < >
75
- { transition ( ( style , bar ) => {
76
- return (
77
- < RadialBarArc
78
- key = { bar . id }
79
- bar = { bar }
80
- animated = { style }
81
- arcGenerator = { arcGenerator }
82
- isInteractive = { isInteractive }
83
- tooltip = { tooltip }
84
- onClick = { onClick }
85
- onMouseEnter = { onMouseEnter }
86
- onMouseMove = { onMouseMove }
87
- onMouseLeave = { onMouseLeave }
88
- />
89
- )
90
- } ) }
91
- </ >
65
+ < ArcsLayer < ComputedBar >
66
+ center = { center }
67
+ data = { bars }
68
+ arcGenerator = { arcGenerator }
69
+ borderWidth = { 0 }
70
+ borderColor = { '#000000' }
71
+ transitionMode = { transitionMode }
72
+ onClick = { isInteractive ? handleClick : undefined }
73
+ onMouseEnter = { isInteractive ? handleMouseEnter : undefined }
74
+ onMouseMove = { isInteractive ? handleMouseMove : undefined }
75
+ onMouseLeave = { isInteractive ? handleMouseLeave : undefined }
76
+ />
92
77
)
93
78
}
0 commit comments