UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

141 lines 4.14 kB
import React from 'react'; import clsx from 'clsx'; import { convertJsxToString, warn } from "../../shared/component-helper.js"; import StatValueContext from "./StatValueContext.js"; import { TextInternal as Text } from "./Text.js"; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; const trendContextValue = { useBasisSize: true, defaultMainWeight: null }; function Trend(props) { const { value, children, id = null, element: Element = 'span', className = null, style = null, srLabel = null, tone = null, skeleton = null, ...rest } = props; const rawValue = typeof value !== 'undefined' ? value : getValueFromChildren(children); const hasCustomChildren = typeof value === 'undefined' && children !== null && children !== undefined && typeof children !== 'string' && typeof children !== 'number'; const { tone: resolvedTone, sign, displayValue } = resolveTrendValue(rawValue); const childText = hasCustomChildren ? convertJsxToString(children) : ''; const visibleText = childText || `${sign || ''}${displayValue}`; const usedTone = tone || resolvedTone; const srText = srLabel ? `${convertJsxToString(srLabel)}${' '}${visibleText}` : visibleText; return _jsxs(Text, { ...rest, id: id, element: Element, className: clsx(`dnb-stat dnb-stat__trend dnb-stat__trend--${usedTone}`, className), style: style, skeleton: skeleton, textClassName: false, children: [_jsx(StatValueContext, { value: trendContextValue, children: _jsxs("span", { className: "dnb-stat__trend-content", "aria-hidden": true, children: [!hasCustomChildren && sign ? _jsx("span", { className: "dnb-stat__trend-sign", children: sign }) : null, _jsx("span", { className: "dnb-stat__trend-value", children: hasCustomChildren ? children : displayValue })] }) }), _jsx("span", { className: "dnb-sr-only", "data-text": srText })] }); } Trend._supportsSpacingProps = true; function getValueFromChildren(children) { if (typeof children === 'string' || typeof children === 'number') { return children; } if (React.isValidElement(children)) { const childProps = children.props; if (typeof (childProps === null || childProps === void 0 ? void 0 : childProps.value) === 'number' || typeof (childProps === null || childProps === void 0 ? void 0 : childProps.value) === 'string') { return childProps.value; } if (typeof (childProps === null || childProps === void 0 ? void 0 : childProps.children) === 'string' || typeof (childProps === null || childProps === void 0 ? void 0 : childProps.children) === 'number') { return childProps.children; } } const text = convertJsxToString(children); if (!text) { warn('Stat.Trend could not resolve a value from the provided children. Falling back to "0".'); } return text || '0'; } function resolveTrendValue(value) { if (typeof value === 'number') { if (!Number.isFinite(value)) { return { tone: 'neutral', sign: null, displayValue: '\u2013' }; } if (value > 0) { return { tone: 'positive', sign: '+', displayValue: String(Math.abs(value)) }; } if (value < 0) { return { tone: 'negative', sign: '-', displayValue: String(Math.abs(value)) }; } return { tone: 'neutral', sign: null, displayValue: '0' }; } const normalized = String(value || ''); const match = normalized.match(/^([+\-−])\s?(.*)$/); if (!match) { return { tone: 'neutral', sign: null, displayValue: normalized || '0' }; } if (!match[2]) { return { tone: 'neutral', sign: null, displayValue: '0' }; } if (match[1] === '+') { return { tone: 'positive', sign: '+', displayValue: match[2] }; } return { tone: 'negative', sign: '-', displayValue: match[2] }; } export default Trend; //# sourceMappingURL=Trend.js.map