@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
96 lines (95 loc) • 3.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.TextInternal = TextInternal;
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _clsx = _interopRequireDefault(require("clsx"));
var _SpacingUtils = require("../space/SpacingUtils.js");
var _Typography = require("../../elements/typography/Typography.js");
var _componentHelper = require("../../shared/component-helper.js");
var _useStatSkeleton = _interopRequireDefault(require("./useStatSkeleton.js"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function TextInternal(props) {
const {
children,
element: Element = 'span',
className = null,
style = null,
srLabel = null,
fontSize = null,
fontWeight,
colorizeBySign = false,
skeleton = null,
skeletonMethod = 'font',
textClassName = 'dnb-stat__text',
...rest
} = props;
const {
skeletonClass,
applySkeletonAttributes
} = (0, _useStatSkeleton.default)(skeleton, skeletonMethod);
const resolvedSignTone = resolveSignTone(colorizeBySign, children);
const textValue = (0, _componentHelper.convertJsxToString)(children);
const srText = srLabel ? `${(0, _componentHelper.convertJsxToString)(srLabel)}${' '}${textValue}` : textValue;
const ariaLabel = srLabel ? srText : rest['aria-label'];
const attributes = (0, _componentHelper.validateDOMAttributes)(props, (0, _SpacingUtils.applySpacing)(props, {
...rest,
'aria-label': ariaLabel,
style,
className: (0, _clsx.default)(textClassName, skeletonClass, className, textClassName && [fontSize && `dnb-t__size--${fontSize} dnb-t__line-height--${(0, _Typography.getHeadingLineHeightSize)(fontSize)}`, fontWeight && `dnb-t__weight--${fontWeight}`], resolvedSignTone && `dnb-stat--tone-${resolvedSignTone}`)
}));
applySkeletonAttributes(attributes);
return (0, _jsxRuntime.jsx)(Element, {
...attributes,
children: children
});
}
TextInternal._supportsSpacingProps = true;
function Text(props) {
return (0, _jsxRuntime.jsx)(TextInternal, {
...props
});
}
Text._supportsSpacingProps = true;
function resolveSignTone(colorizeBySign, children) {
if (colorizeBySign === false || colorizeBySign === null) {
return null;
}
const value = typeof colorizeBySign === 'number' ? colorizeBySign : getValueFromChildren(children);
if (typeof value === 'number') {
if (value > 0) {
return 'positive';
}
if (value < 0 || Object.is(value, -0)) {
return 'negative';
}
return null;
}
const normalized = String(value || '').trim();
const match = normalized.match(/^([+\-−])/);
if ((match === null || match === void 0 ? void 0 : match[1]) === '+') {
return 'positive';
}
if ((match === null || match === void 0 ? void 0 : match[1]) === '-' || (match === null || match === void 0 ? void 0 : match[1]) === '−') {
return 'negative';
}
const numericValue = Number(normalized);
if (numericValue > 0) {
return 'positive';
}
if (numericValue < 0 || Object.is(numericValue, -0)) {
return 'negative';
}
return null;
}
function getValueFromChildren(children) {
if (typeof children === 'string' || typeof children === 'number') {
return children;
}
return (0, _componentHelper.convertJsxToString)(children);
}
var _default = exports.default = Text;
//# sourceMappingURL=Text.js.map