@@ -21,58 +21,71 @@ import { FALLBACK_ENTITY } from './constants'
21
21
import './styles.scss'
22
22
23
23
const SegmentedBarChart : React . FC < SegmentedBarChartProps > = ( {
24
- entities = [ FALLBACK_ENTITY ] ,
24
+ entities : userEntities = [ FALLBACK_ENTITY ] ,
25
25
rootClassName,
26
26
countClassName,
27
27
labelClassName,
28
28
isProportional,
29
29
swapLegendAndBar = false ,
30
30
showAnimationOnBar = false ,
31
+ isLoading,
31
32
} ) => {
33
+ const entities = isLoading ? [ FALLBACK_ENTITY ] : userEntities
32
34
const total = entities . reduce ( ( sum , entity ) => entity . value + sum , 0 )
33
35
const filteredEntities = entities . filter ( ( entity ) => ! ! entity . value )
34
36
35
37
const calcSegmentWidth = ( entity : Entity ) => `${ ( entity . value / total ) * 100 } %`
36
38
37
- const renderLabel = ( label : string ) => (
38
- < span className = { labelClassName } data-testid = { `segmented-bar-chart-${ label } -label` } >
39
- { label }
40
- </ span >
41
- )
39
+ const renderLabel = ( label : Entity [ 'label' ] ) =>
40
+ isLoading ? (
41
+ < div className = "shimmer w-120" />
42
+ ) : (
43
+ < span className = { labelClassName } data-testid = { `segmented-bar-chart-${ label } -label` } >
44
+ { label }
45
+ </ span >
46
+ )
42
47
43
- const renderValue = ( value : string | number , label : string ) => (
44
- < span className = { countClassName } data-testid = { `segmented-bar-chart-${ label } -value` } >
45
- { isProportional ? `${ value } /${ total } ` : value }
46
- </ span >
47
- )
48
+ const renderValue = ( value : Entity [ 'value' ] , label : Entity [ 'label' ] ) =>
49
+ isLoading ? (
50
+ < div className = "shimmer w-64 lh-1-5 h-24" />
51
+ ) : (
52
+ < span className = { countClassName } data-testid = { `segmented-bar-chart-${ label } -value` } >
53
+ { isProportional ? `${ value } /${ total } ` : value }
54
+ </ span >
55
+ )
48
56
49
57
const renderContent = ( ) => {
50
58
if ( isProportional ) {
51
59
return filteredEntities . map ( ( entity , idx ) => (
52
60
// eslint-disable-next-line react/no-array-index-key
53
- < div key = { idx } className = " flexbox-col" >
61
+ < div key = { idx } className = { ` flexbox-col ${ isLoading ? 'dc__gap-10' : '' } ` } >
54
62
{ renderValue ( entity . value , entity . label ) }
63
+
55
64
< div className = "flex left dc__gap-6" >
56
- < span style = { { backgroundColor : entity . color } } className = "h-12 dc__border-radius-2 w-4" />
65
+ { ! isLoading && (
66
+ < span style = { { backgroundColor : entity . color } } className = "h-12 dc__border-radius-2 w-4" />
67
+ ) }
68
+
57
69
{ renderLabel ( entity . label ) }
58
70
</ div >
59
71
</ div >
60
72
) )
61
73
}
74
+
62
75
return entities . map ( ( entity , idx ) => (
63
76
// eslint-disable-next-line react/no-array-index-key
64
77
< div key = { idx } className = "flexbox dc__gap-4 dc__align-items-center" >
65
- < div className = "dot" style = { { backgroundColor : entity . color , width : '10px' , height : '10px' } } />
78
+ { ! isLoading && (
79
+ < div className = "dot" style = { { backgroundColor : entity . color , width : '10px' , height : '10px' } } />
80
+ ) }
81
+
66
82
{ renderValue ( entity . value , entity . label ) }
83
+
67
84
{ renderLabel ( entity . label ) }
68
85
</ div >
69
86
) )
70
87
}
71
88
72
- if ( ! entities . length ) {
73
- return null
74
- }
75
-
76
89
const renderLegend = ( ) => (
77
90
< div className = { `flexbox flex-wrap dc__row-gap-4 ${ isProportional ? 'dc__gap-24' : 'dc__gap-16' } ` } >
78
91
{ renderContent ( ) }
@@ -92,16 +105,21 @@ const SegmentedBarChart: React.FC<SegmentedBarChartProps> = ({
92
105
>
93
106
{ filteredEntities . map ( ( entity , index , map ) => (
94
107
< div
95
- key = { entity . label }
108
+ // eslint-disable-next-line react/no-array-index-key
109
+ key = { index }
96
110
className = { `h-8 ${ index === 0 ? 'dc__left-radius-4' : '' } ${
97
111
index === map . length - 1 ? 'dc__right-radius-4' : ''
98
- } `}
112
+ } ${ isLoading ? 'shimmer' : '' } `}
99
113
style = { { backgroundColor : entity . color , width : calcSegmentWidth ( entity ) } }
100
114
/>
101
115
) ) }
102
116
</ motion . div >
103
117
)
104
118
119
+ if ( ! entities . length ) {
120
+ return null
121
+ }
122
+
105
123
return (
106
124
< div className = { `flexbox-col w-100 dc__gap-12 ${ rootClassName } ` } >
107
125
{ swapLegendAndBar ? (
0 commit comments