1- import React from "react" ;
1+ import React , { useEffect , useState } from "react" ;
22import classes from './styles.module.scss' ;
33import { DotMatrixPropType } from "./types" ;
44import { useDotMatrix } from './custom-hooks/useDotMatrix' ;
55import Chart from './Chart' ;
66import Legend from './Legend' ;
7- import { getLegendPosition , getStyles } from "./utils/utils" ;
7+ import { getLegendPosition , getStyles , findContainerWidth } from "./utils/utils" ;
88import {
99 Elements ,
1010 DEFAULT_COLUMNS ,
@@ -23,28 +23,50 @@ const DotMatrix = (props: DotMatrixPropType): JSX.Element => {
2323 legendPosition = DEFAULT_LEGEND_POSITION
2424 } = props ;
2525
26+ const [ width , setWidth ] = useState < number > ( 0 ) ;
2627 const [ data , total , overlappingValues ] = useDotMatrix ( dataPoints , dimensions ) ;
28+
29+ useEffect ( ( ) => {
30+ findChartContainerWidth ( ) ;
31+ } , [ ] ) ;
32+ useEffect ( ( ) => {
33+ findChartContainerWidth ( ) ;
34+ } , [ showLegend , legendPosition ] ) ;
35+ const findChartContainerWidth = ( ) : void => {
36+ const widthValue = findContainerWidth ( 'dots-container' ) ;
37+ if ( widthValue ) setWidth ( widthValue ) ;
38+ }
39+ window . onresize = ( ) : void => {
40+ findChartContainerWidth ( ) ;
41+ } ;
2742 return (
28- < div className = { classes . container } >
43+ < div
44+ className = { classes . container }
45+ >
2946 < div
3047 className = { classes . dotsWithLegend }
3148 style = { {
3249 ...getStyles ( Elements . Container , styles ) ,
3350 ...( getLegendPosition ( legendPosition ) as React . CSSProperties )
3451 } }
3552 >
36- < Chart
37- styles = { styles }
38- dimensions = { dimensions }
39- data = { data }
40- total = { total }
41- overlappingValues = { overlappingValues }
42- />
43- { showLegend && (
44- < Legend
53+ < div id = "dots-container" >
54+ < Chart
4555 styles = { styles }
56+ dimensions = { dimensions }
4657 data = { data }
58+ total = { total }
59+ width = { width }
60+ overlappingValues = { overlappingValues }
4761 />
62+ </ div >
63+ { showLegend && (
64+ < div >
65+ < Legend
66+ styles = { styles }
67+ data = { data }
68+ />
69+ </ div >
4870 ) }
4971 </ div >
5072 </ div >
0 commit comments