UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

148 lines (147 loc) 4.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireDefault(require("react")); var _clsx = _interopRequireDefault(require("clsx")); var _componentHelper = require("../../shared/component-helper.js"); var _StatValueContext = _interopRequireDefault(require("./StatValueContext.js")); var _Text = require("./Text.js"); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } 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 ? (0, _componentHelper.convertJsxToString)(children) : ''; const visibleText = childText || `${sign || ''}${displayValue}`; const usedTone = tone || resolvedTone; const srText = srLabel ? `${(0, _componentHelper.convertJsxToString)(srLabel)}${' '}${visibleText}` : visibleText; return (0, _jsxRuntime.jsxs)(_Text.TextInternal, { ...rest, id: id, element: Element, className: (0, _clsx.default)(`dnb-stat dnb-stat__trend dnb-stat__trend--${usedTone}`, className), style: style, skeleton: skeleton, textClassName: false, children: [(0, _jsxRuntime.jsx)(_StatValueContext.default, { value: trendContextValue, children: (0, _jsxRuntime.jsxs)("span", { className: "dnb-stat__trend-content", "aria-hidden": true, children: [!hasCustomChildren && sign ? (0, _jsxRuntime.jsx)("span", { className: "dnb-stat__trend-sign", children: sign }) : null, (0, _jsxRuntime.jsx)("span", { className: "dnb-stat__trend-value", children: hasCustomChildren ? children : displayValue })] }) }), (0, _jsxRuntime.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.default.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 = (0, _componentHelper.convertJsxToString)(children); if (!text) { (0, _componentHelper.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] }; } var _default = exports.default = Trend; //# sourceMappingURL=Trend.js.map