UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

142 lines (141 loc) 4.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireDefault(require("react")); var _classnames = _interopRequireDefault(require("classnames")); var _componentHelper = require("../../shared/component-helper.js"); var _StatValueContext = _interopRequireDefault(require("./StatValueContext.js")); var _Text = require("./Text.js"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } 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 _react.default.createElement(_Text.TextInternal, _extends({}, rest, { id: id, element: Element, className: (0, _classnames.default)(`dnb-stat dnb-stat__trend dnb-stat__trend--${usedTone}`, className), style: style, skeleton: skeleton, textClassName: false }), _react.default.createElement(_StatValueContext.default.Provider, { value: trendContextValue }, _react.default.createElement("span", { className: "dnb-stat__trend-content", "aria-hidden": true }, !hasCustomChildren && sign ? _react.default.createElement("span", { className: "dnb-stat__trend-sign" }, sign) : null, _react.default.createElement("span", { className: "dnb-stat__trend-value" }, hasCustomChildren ? children : displayValue))), _react.default.createElement("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