Add JSdoc, add underscores to variables

This commit is contained in:
Alex Holliday committed 2024-11-07 10:52:19 +08:00
1 parent 19c0632143
commit da5ecb2b83
1 file changed
+35 -12
+35 -12
View File
@@ -2,8 +2,8 @@ import { RadialBarChart, RadialBar, ResponsiveContainer } from "recharts";
import PropTypes from "prop-types"; import PropTypes from "prop-types";
import { useTheme } from "@emotion/react"; import { useTheme } from "@emotion/react";
const MINIMUM_VALUE = 0 ; const MINIMUM_VALUE = 0;
const MAXIMUM_VALUE = 100 ; const MAXIMUM_VALUE = 100;
const DATA = [{ value: MAXIMUM_VALUE }]; const DATA = [{ value: MAXIMUM_VALUE }];
const PROGRESS_THRESHOLD = 50; const PROGRESS_THRESHOLD = 50;
const DEFAULT_CONTAINER_HEIGHT = 160; const DEFAULT_CONTAINER_HEIGHT = 160;
@@ -11,14 +11,14 @@ const TEXT_POSITIONS = {
value: { x: "50%", y: "45%" }, value: { x: "50%", y: "45%" },
label: { x: "50%", y: "55%" }, label: { x: "50%", y: "55%" },
}; };
const COMMON_RADIALBAR_PROPS = { const COMMON_RADIAL_BAR_PROPS = {
minAngle: 15, minAngle: 15,
clockWise: true, clockWise: true,
dataKey: "value", dataKey: "value",
cornerRadius: 0, cornerRadius: 0,
}; };
const RADIALBARCHART_PROPS = { const RADIAL_BAR_CHART_PROPS = {
cx: "50%", cx: "50%",
cy: "50%", cy: "50%",
innerRadius: "45%", innerRadius: "45%",
@@ -27,7 +27,7 @@ const RADIALBARCHART_PROPS = {
endAngle: -270, endAngle: -270,
}; };
const RADIALBAR_OVERLAY_PROPS = { const RADIAL_BAR_OVERLAY_PROPS = {
transform: "rotate(180deg)", transform: "rotate(180deg)",
position: "absolute", position: "absolute",
top: "0", top: "0",
@@ -39,6 +39,29 @@ const COMMON_HEADER_PROPS = {
dominantBaseline: "middle", dominantBaseline: "middle",
}; };
/**
* A circular gauge component that displays a progress value and text.
* The gauge fills based on the progress value and changes color at a 50% threshold.
*
* @component
* @param {Object} props - Component props
* @param {number} props.progressValue - The value to display in the gauge (0-100)
* @param {string} props.displayText - The text to display below the progress value
* @param {number} [props.containerHeight=160] - Height of the gauge container in pixels
* @param {Object} [props.gaugeHeader] - Custom styles to apply to the progress value text
* @param {Object} [props.gaugeSubheader] - Custom styles to apply to the display text
*
* @returns {JSX.Element} A circular gauge chart with progress value and text
*
* @example
* <Gauge
* progressValue={75}
* displayText="Completion"
* containerHeight={200}
* gaugeHeader={{ fontSize: '24px' }}
* gaugeSubheader={{ fill: 'gray' }}
* />
*/
const Gauge = ({ const Gauge = ({
progressValue, progressValue,
displayText, displayText,
@@ -46,17 +69,17 @@ const Gauge = ({
gaugeHeader, gaugeHeader,
gaugeSubheader, gaugeSubheader,
}) => { }) => {
const theme = useTheme(); const theme = useTheme();
const myProgressValue = Math.max(MINIMUM_VALUE, Math.min(progressValue, MAXIMUM_VALUE)); const myProgressValue = Math.max(MINIMUM_VALUE, Math.min(progressValue, MAXIMUM_VALUE));
return ( return (
<ResponsiveContainer height={containerHeight ?? DEFAULT_CONTAINER_HEIGHT}> <ResponsiveContainer height={containerHeight ?? DEFAULT_CONTAINER_HEIGHT}>
<RadialBarChart <RadialBarChart
{...RADIALBARCHART_PROPS} {...RADIAL_BAR_CHART_PROPS}
data={DATA} data={DATA}
> >
<RadialBar <RadialBar
{...COMMON_RADIALBAR_PROPS} {...COMMON_RADIAL_BAR_PROPS}
fill={ fill={
progressValue > PROGRESS_THRESHOLD progressValue > PROGRESS_THRESHOLD
? theme.palette.primary.main ? theme.palette.primary.main
@@ -66,15 +89,15 @@ const Gauge = ({
data={[{ value: myProgressValue }]} data={[{ value: myProgressValue }]}
/> />
<RadialBar <RadialBar
{...COMMON_RADIALBAR_PROPS} {...COMMON_RADIAL_BAR_PROPS}
data={DATA} data={DATA}
style={RADIALBAR_OVERLAY_PROPS} style={RADIAL_BAR_OVERLAY_PROPS}
/> />
<text <text
{...TEXT_POSITIONS.value} {...TEXT_POSITIONS.value}
role="text" role="text"
aria-label={`${myProgressValue}%`} aria-label={`${myProgressValue}%`}
style={{...COMMON_HEADER_PROPS, ...theme.chart.header, ...gaugeHeader }} style={{ ...COMMON_HEADER_PROPS, ...theme.chart.header, ...gaugeHeader }}
> >
{`${myProgressValue}%`} {`${myProgressValue}%`}
</text> </text>
@@ -82,7 +105,7 @@ const Gauge = ({
{...TEXT_POSITIONS.label} {...TEXT_POSITIONS.label}
role="text" role="text"
aria-label={`${displayText}`} aria-label={`${displayText}`}
style={{...COMMON_HEADER_PROPS, ...theme.chart.subheader, ...gaugeSubheader }} style={{ ...COMMON_HEADER_PROPS, ...theme.chart.subheader, ...gaugeSubheader }}
> >
{`${displayText}`} {`${displayText}`}
</text> </text>