11import { useConsolidatedRef } from '@ui5/webcomponents-react-base' ;
22import bestContrast from 'get-best-contrast-color' ;
3- import React , { forwardRef , Ref , RefObject , useRef , useMemo } from 'react' ;
3+ import React , { forwardRef , Ref , useMemo } from 'react' ;
44import { HorizontalBar } from 'react-chartjs-2' ;
55import { useTheme } from 'react-jss' ;
66import { DEFAULT_OPTIONS } from '../../config' ;
77import { ChartBaseProps } from '../../interfaces/ChartBaseProps' ;
8- import { withChartContainer } from '../../internal/withChartContainer ' ;
8+ import { InternalProps } from '../../interfaces/InternalProps ' ;
99import { useLegend , useLegendItemClickHandler } from '../../internal/ChartLegend' ;
10+ import { withChartContainer } from '../../internal/withChartContainer' ;
1011import { ChartBaseDefaultProps } from '../../util/ChartBaseDefaultProps' ;
1112import { useChartData } from '../../util/populateData' ;
1213import { formatTooltipLabel , getTextWidth , useMergedConfig } from '../../util/utils' ;
@@ -26,17 +27,14 @@ const BarChartComponent = forwardRef((props: BarChartPropTypes, ref: Ref<any>) =
2627 colors,
2728 width,
2829 height,
29- noLegend
30- } = props as BarChartPropTypes ;
30+ noLegend,
31+ legendRef
32+ } = props as BarChartPropTypes & InternalProps ;
3133
3234 const theme : any = useTheme ( ) ;
3335 const data = useChartData ( labels , datasets , colors , theme . theme ) ;
3436
3537 const chartRef = useConsolidatedRef < any > ( ref ) ;
36- const legendRef : RefObject < HTMLDivElement > = useRef ( ) ;
37-
38- const handleLegendItemPress = useLegendItemClickHandler ( chartRef , legendRef ) ;
39- useLegend ( chartRef , legendRef , noLegend , handleLegendItemPress ) ;
4038
4139 const barChartDefaultConfig = useMemo ( ( ) => {
4240 return {
@@ -89,24 +87,24 @@ const BarChartComponent = forwardRef((props: BarChartPropTypes, ref: Ref<any>) =
8987 }
9088 } ;
9189 } , [ valueAxisFormatter , categoryAxisFormatter ] ) ;
92-
9390 const mergedOptions = useMergedConfig ( barChartDefaultConfig , options ) ;
9491
92+ const handleLegendItemPress = useLegendItemClickHandler ( chartRef , legendRef ) ;
93+ useLegend ( chartRef , legendRef , noLegend , handleLegendItemPress ) ;
94+
9595 return (
96- < >
97- < HorizontalBar
98- ref = { chartRef }
99- data = { data }
100- height = { height }
101- width = { width }
102- options = { mergedOptions }
103- getDatasetAtEvent = { getDatasetAtEvent }
104- getElementAtEvent = { getElementAtEvent }
105- />
106- < div ref = { legendRef } className = "legend" />
107- </ >
96+ < HorizontalBar
97+ ref = { chartRef }
98+ data = { data }
99+ height = { height }
100+ width = { width }
101+ options = { mergedOptions }
102+ getDatasetAtEvent = { getDatasetAtEvent }
103+ getElementAtEvent = { getElementAtEvent }
104+ />
108105 ) ;
109106} ) ;
107+
110108// @ts -ignore
111109BarChartComponent . LoadingPlaceholder = BarChartPlaceholder ;
112110const BarChart = withChartContainer ( BarChartComponent ) ;
0 commit comments