import { useTheme } from '@emotion/react';
import type { ReactElement } from 'react';
import React from 'react';
import Typography from '../../Typography';
import Box from '../../Box';
import { MAX_PROGRESS_VALUE, MIN_PROGRESS_VALUE } from './constants';
import {
useValidateSegmentedBarData,
useValidateSegmentedBarLegendDisplayValues,
useValidateSegmentedBarStyleConfig,
} from './hooks/validation';
import { useSegmentedBarColors } from './hooks/colors';
import LegendItem from './Legend';
import { LegendBox } from './StyledLegend';
import {
StyledEmptyText,
StyledHeader,
StyledSegment,
StyledTrack,
} from './StyledSegmentedBar';
import type { ProgressSegmentedBarProps } from './types';
import { getTextComponent } from './utils';
const ProgressSegmentedBar = ({
data,
headerConfig,
emptyText,
legendConfig,
styleConfig,
style,
testID,
collapsable,
...nativeProps
}: ProgressSegmentedBarProps): ReactElement => {
const values = React.useMemo(() => data.map((s) => s.data), [data]);
useValidateSegmentedBarData({ dataLength: data.length, values });
const legendDisplayValues = useValidateSegmentedBarLegendDisplayValues({
dataLength: data.length,
legendConfig,
fallbackValues: values,
});
useValidateSegmentedBarStyleConfig({
dataSeries: data,
customSeries: styleConfig?.series,
});
const theme = useTheme();
const totalPercent = React.useMemo(
() => values.reduce((acc, value) => acc + value, 0),
[values]
);
const ariaValueMax =
totalPercent > MAX_PROGRESS_VALUE ? totalPercent : MAX_PROGRESS_VALUE;
const remainderPercent = React.useMemo(() => {
return totalPercent >= MAX_PROGRESS_VALUE
? 0
: MAX_PROGRESS_VALUE - totalPercent;
}, [totalPercent]);
const segmentColors = useSegmentedBarColors({
dataSeries: data,
customSeries: styleConfig?.series,
});
const headerRight = React.useMemo(
() =>
getTextComponent(
headerConfig?.right,
{headerConfig?.right}
),
[headerConfig?.right]
);
const showHeader = Boolean(headerConfig?.left || headerRight);
const showLegend = Boolean(
legendConfig && Object.keys(legendConfig).length > 0
);
const showEmptyText = Boolean(!data.length && emptyText);
return (
{showHeader ? (
{headerConfig?.left ? (
{headerConfig.left}
) : null}
{headerRight ? (
{headerRight}
) : null}
) : null}
{data.map((s, index) => {
const segmentColor = segmentColors[index % segmentColors.length];
return (
);
})}
{remainderPercent > 0 ? (
) : null}
{showEmptyText ? (
{emptyText}
) : null}
{showLegend ? (
{data.map((item, index) => (
))}
) : null}
);
};
export default ProgressSegmentedBar;