import {createContext, JSX, mergeProps, splitProps, useContext} from "solid-js"; import classNames from "./classnames"; import {useBootstrapPrefix} from "./ThemeProvider"; import {BsPrefixProps} from "./helpers"; export interface ProgressBarProps extends JSX.HTMLAttributes, BsPrefixProps { min?: number; now?: number; max?: number; label?: JSX.Element; visuallyHidden?: boolean; striped?: boolean; animated?: boolean; variant?: "success" | "danger" | "warning" | "info" | string; } const ProgressContext = createContext<{isStacked: boolean}>(); const ROUND_PRECISION = 1000; const defaultProps: Partial = { min: 0, max: 100, animated: false, visuallyHidden: false, striped: false, }; function getPercentage(now: number, min: number, max: number) { const percentage = ((now - min) / (max - min)) * 100; return Math.round(percentage * ROUND_PRECISION) / ROUND_PRECISION; } function renderProgressBar(p: ProgressBarProps) { const [local, props] = splitProps(p, [ "min", "now", "max", "label", "visuallyHidden", "striped", "animated", "class", "style", "variant", "bsPrefix", ]); const bsPrefix = useBootstrapPrefix(local.bsPrefix, "progress"); return (
{local.visuallyHidden ? {local.label} : local.label}
); } const ProgressBar = (p: ProgressBarProps) => { const [local, props] = splitProps(mergeProps(defaultProps, p), ["children", "class", "bsPrefix"]); const bsPrefix = useBootstrapPrefix(local.bsPrefix, "progress"); const context = useContext(ProgressContext); let barProps = props; let wrapperProps: Partial = {}; if (!context?.isStacked) { const [b, w] = splitProps(props, [ "min", "now", "max", "label", "visuallyHidden", "striped", "animated", "variant", ]); barProps = b; wrapperProps = w; } return context?.isStacked ? ( renderProgressBar(barProps) ) : (
{local.children ?? renderProgressBar(barProps)}
); }; export default ProgressBar;