UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

120 lines (119 loc) 4.8 kB
"use client"; import React, { useCallback, useContext, useEffect, useRef, useState } from 'react'; import clsx from 'clsx'; import Context from "../../shared/Context.js"; import { validateDOMAttributes, dispatchCustomElementEvent, extendPropsWithContext } from "../../shared/component-helper.js"; import { applySpacing } from "../space/SpacingUtils.js"; import ProgressIndicatorCircular from "./ProgressIndicatorCircular.js"; import ProgressIndicatorLinear from "./ProgressIndicatorLinear.js"; import { formatPercent } from "../number-format/NumberUtils.js"; import { isValidSize } from "./types.js"; import withComponentMarkers from "../../shared/helpers/withComponentMarkers.js"; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; function ProgressIndicator(props) { const allProps = updatePropsWithContext(props, useContext(Context)); const { type = 'circular', size = 'default', noAnimation = false, onComplete, label, indicatorLabel, labelDirection = 'vertical', showDefaultLabel = false, className, title, progress, show = true, customColors, customCircleWidth, style, ...rest } = allProps; const remainingDOMProps = validateDOMAttributes(allProps, { ...rest }); const [sizeVariant, customSize] = isValidSize(size) ? [size, undefined] : ['custom-size', size]; const completeTimeout = useRef(undefined); const fadeOutTimeout = useRef(undefined); const [complete, setCompleteState] = useState(false); const progressNumber = typeof progress === 'string' ? parseFloat(progress) : typeof progress === 'number' ? progress : undefined; const usedIndicatorLabel = label || showDefaultLabel && indicatorLabel; const progressTitle = title || formatProgress(progressNumber); useEffect(() => { return () => { clearTimeout(completeTimeout.current); clearTimeout(fadeOutTimeout.current); }; }, []); useEffect(() => { if (show) { setCompleteState(false); } }, [show]); const callOnCompleteHandler = useCallback(() => { completeTimeout.current = setTimeout(() => { setCompleteState(true); if (onComplete) { fadeOutTimeout.current = setTimeout(() => { dispatchCustomElementEvent({ onComplete }, 'onComplete'); }, 600); } }, 200); }, [onComplete]); return _jsxs("span", { ...applySpacing(allProps, { className: clsx('dnb-progress-indicator', className, show && 'dnb-progress-indicator--show', complete && 'dnb-progress-indicator--complete', labelDirection && `dnb-progress-indicator--${labelDirection}`, sizeVariant && `dnb-progress-indicator--${sizeVariant}`, noAnimation && 'dnb-progress-indicator--no-animation', type === 'linear' && 'dnb-progress-indicator--full-width'), style: { ...style, '--progress-indicator-circular-size': customSize, '--progress-indicator-circular-stroke-width': customCircleWidth, '--progress-indicator-linear-size': customSize }, ...remainingDOMProps }), children: [(type === 'circular' || type === 'countdown') && _jsx(ProgressIndicatorCircular, { size: sizeVariant, progress: progressNumber, show: show, onComplete: onComplete, callOnCompleteHandler: callOnCompleteHandler, title: progressTitle === null || progressTitle === void 0 ? void 0 : progressTitle.toString(), customColors: customColors, customCircleWidth: customCircleWidth, counterClockwise: type === 'countdown' }), type === 'linear' && _jsx(ProgressIndicatorLinear, { size: sizeVariant, progress: progressNumber, show: show, onComplete: onComplete, callOnCompleteHandler: callOnCompleteHandler, title: progressTitle === null || progressTitle === void 0 ? void 0 : progressTitle.toString(), customColors: customColors }), usedIndicatorLabel && _jsx("span", { className: "dnb-progress-indicator__label dnb-p", children: usedIndicatorLabel })] }); } function updatePropsWithContext(props, context) { const localPropsFromContext = context === null || context === void 0 ? void 0 : context.getTranslation(props).ProgressIndicator; const componentPropsFromContext = context === null || context === void 0 ? void 0 : context.ProgressIndicator; return extendPropsWithContext(props, {}, localPropsFromContext, componentPropsFromContext); } function formatProgress(progress) { if (parseFloat(progress) > -1) { return formatPercent(progress, { decimals: 2 }); } return null; } ProgressIndicator.displayName = 'ProgressIndicator'; export default ProgressIndicator; withComponentMarkers(ProgressIndicator, { _supportsSpacingProps: true }); //# sourceMappingURL=ProgressIndicator.js.map