Skip to content

Commit 8b25dfe

Browse files
Responsive design - git comments
1 parent 0390cf2 commit 8b25dfe

File tree

9 files changed

+87
-66
lines changed

9 files changed

+87
-66
lines changed

.eslintrc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
"max-len":["warn", 150],
3131
"space-before-blocks":"error",
3232
"prefer-spread": ["off"],
33-
"react-hooks/exhaustive-deps": "off"
33+
"react-hooks/exhaustive-deps": "off",
34+
"arrow-parens": "error",
35+
"arrow-spacing": "error"
3436
},
3537
"settings": {
3638
"react": {

.github/workflows/deploy.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ jobs:
2424
uses: actions/setup-node@v2
2525
with:
2626
node-version: 16.x
27+
scope: '@keyvaluesystems'
2728

2829
- name: Install dependencies
2930
run: npm install
@@ -81,11 +82,7 @@ jobs:
8182
run: npm run build
8283

8384
- name: Publish package
84-
uses: JS-DevTools/npm-publish@v1
85-
with:
86-
access: 'public'
87-
check-version: true
88-
token: ${{ secrets.NPM_AUTH_TOKEN }}
85+
run: npm publish --access public --//registry.npmjs.org/:_authToken=${{ secrets.NPM_AUTH_TOKEN }}
8986
if: success()
9087

9188
- name: Revert package.json and package-lock.json

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "react-dot-matrix-chart",
2+
"name": "@keyvaluesystems/react-dot-matrix-chart",
33
"version": "0.2.2",
44
"description": "A Dot Matrix Chart component",
55
"main": "build/index.js",

src/lib/dot-matrix/Chart.tsx

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useMemo } from 'react';
22
import { v4 } from 'uuid';
33
import {
44
getNumberOfDots,
@@ -8,8 +8,10 @@ import {
88
import {
99
Elements,
1010
DEFAULT_COLUMNS,
11-
DEFAULT_ROWS
12-
} from "./constants";
11+
DEFAULT_ROWS,
12+
DEFAULT_ROW_WIDTH,
13+
DEFAULT_ROW_GAP
14+
} from './constants';
1315
import { ChartProps, DataPointType } from './types';
1416
import classes from './styles.module.scss';
1517

@@ -27,13 +29,10 @@ const Chart = (props: ChartProps) : JSX.Element => {
2729
columns = DEFAULT_COLUMNS
2830
} = dimensions;
2931

30-
const getDotWidth = (): number => {
31-
let dotWidth = 35;
32-
if (width) {
33-
dotWidth = width / columns - 19;
34-
}
35-
return dotWidth;
36-
}
32+
const dotWidth = useMemo(
33+
() => (width ? width / columns - DEFAULT_ROW_GAP : DEFAULT_ROW_WIDTH),
34+
[width]
35+
);
3736
return (
3837
<div
3938
className={classes.dotsContainer}
@@ -49,20 +48,22 @@ const Chart = (props: ChartProps) : JSX.Element => {
4948
<div
5049
className={classes.eachDot}
5150
style={{
52-
backgroundImage: `linear-gradient(to right, ${data[rowIndex - 1].color} 20%, ${dataItem?.color} 50%)`,
53-
width: `${getDotWidth()}px`,
54-
height: `${getDotWidth()}px`,
55-
...(getStyles(Elements.Dot, styles))
51+
backgroundImage: `linear-gradient(to right, ${
52+
data[rowIndex - 1].color
53+
} 20%, ${dataItem?.color} 50%)`,
54+
width: `${dotWidth}px`,
55+
height: `${dotWidth}px`,
56+
...getStyles(Elements.Dot, styles)
5657
}}
5758
/>
5859
)) || (
5960
<div
6061
className={classes.eachDot}
6162
style={{
6263
backgroundColor: dataItem?.color,
63-
width: `${getDotWidth()}px`,
64-
height: `${getDotWidth()}px`,
65-
...(getStyles(Elements.Dot, styles))
64+
width: `${dotWidth}px`,
65+
height: `${dotWidth}px`,
66+
...getStyles(Elements.Dot, styles)
6667
}}
6768
key={v4()}
6869
id={`each-category-${rowIndex}-${columnIndex}`}
@@ -73,6 +74,6 @@ const Chart = (props: ChartProps) : JSX.Element => {
7374
</React.Fragment>
7475
))}
7576
</div>
76-
)
77+
);
7778
};
78-
export default Chart;
79+
export default Chart;

src/lib/dot-matrix/DotMatrix.tsx

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
import React, { useEffect, useState } from "react";
1+
import React from 'react';
22
import classes from './styles.module.scss';
3-
import { DotMatrixPropType } from "./types";
3+
import { DotMatrixPropType } from './types';
44
import { useDotMatrix } from './custom-hooks/useDotMatrix';
5+
import useChartContainerWidth from './custom-hooks/useChartContainerWidth';
56
import Chart from './Chart';
67
import Legend from './Legend';
7-
import { getLegendPosition, getStyles, findContainerWidth } from "./utils/utils";
8+
import { getLegendPosition, getStyles } from './utils/utils';
89
import {
910
Elements,
1011
DEFAULT_COLUMNS,
1112
DEFAULT_ROWS,
1213
DEFAULT_LEGEND_POSITION
13-
} from "./constants";
14+
} from './constants';
1415
const DotMatrix = (props: DotMatrixPropType): JSX.Element => {
1516
const {
1617
dataPoints,
@@ -19,38 +20,26 @@ const DotMatrix = (props: DotMatrixPropType): JSX.Element => {
1920
columns: DEFAULT_COLUMNS
2021
},
2122
styles = {},
22-
showLegend,
23+
showLegend = false,
2324
legendPosition = DEFAULT_LEGEND_POSITION
2425
} = props;
2526

26-
const [width, setWidth] = useState<number>(0);
27+
const width = useChartContainerWidth('dots-container', [
28+
showLegend,
29+
legendPosition
30+
]);
2731
const [data, total, overlappingValues] = useDotMatrix(dataPoints, dimensions);
2832

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-
};
4233
return (
43-
<div
44-
className={classes.container}
45-
>
34+
<div className={classes.container}>
4635
<div
4736
className={classes.dotsWithLegend}
4837
style={{
4938
...getStyles(Elements.Container, styles),
5039
...(getLegendPosition(legendPosition) as React.CSSProperties)
5140
}}
5241
>
53-
<div id="dots-container">
42+
<div className={classes.chartContainer} id="dots-container">
5443
<Chart
5544
styles={styles}
5645
dimensions={dimensions}
@@ -62,15 +51,12 @@ const DotMatrix = (props: DotMatrixPropType): JSX.Element => {
6251
</div>
6352
{showLegend && (
6453
<div>
65-
<Legend
66-
styles={styles}
67-
data={data}
68-
/>
54+
<Legend styles={styles} data={data} />
6955
</div>
7056
)}
7157
</div>
7258
</div>
73-
)
59+
);
7460
};
7561

76-
export default DotMatrix;
62+
export default DotMatrix;

src/lib/dot-matrix/constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ export const DEFAULT_ROWS = 5;
2424
export const DEFAULT_COLUMNS = 12;
2525

2626
export const DEFAULT_LEGEND_POSITION = 'right';
27+
28+
export const DEFAULT_ROW_WIDTH = 35;
29+
30+
export const DEFAULT_ROW_GAP = 6;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { useState, useEffect } from 'react';
2+
import { findContainerWidth } from '../utils/utils';
3+
4+
const useChartContainerWidth = (
5+
id: string,
6+
dependencyArray: Array<boolean | string>
7+
): number => {
8+
const [width, setWidth] = useState<number>(0);
9+
10+
useEffect(() => {
11+
updateContainerWidth();
12+
}, []);
13+
14+
useEffect(() => {
15+
updateContainerWidth();
16+
}, [...dependencyArray]);
17+
18+
window.onresize = (): void => {
19+
updateContainerWidth();
20+
};
21+
22+
const updateContainerWidth = (): void => {
23+
const widthValue = findContainerWidth(id);
24+
if (widthValue) setWidth(widthValue);
25+
};
26+
return width;
27+
};
28+
29+
export default useChartContainerWidth;

src/lib/dot-matrix/styles.module.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
display: flex;
1212
flex-wrap: wrap;
1313
gap: 6px;
14-
width: calc(100% - 100px);
14+
width: 100%;
1515
.eachDot {
1616
border-radius: 50%;
1717
width: 2.2rem;

transform.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
/* eslint-disable no-undef */
2+
/* eslint-disable @typescript-eslint/explicit-function-return-type */
13
module.exports = {
2-
process() {
3-
return {
4-
code: `module.exports = {};`,
5-
};
6-
},
7-
getCacheKey() {
8-
// The output is always the same.
9-
return "transform";
10-
},
11-
};
4+
process () {
5+
return {
6+
code: `module.exports = {};`
7+
};
8+
},
9+
getCacheKey () {
10+
// The output is always the same.
11+
return 'transform';
12+
}
13+
};

0 commit comments

Comments
 (0)