@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
90 lines (89 loc) • 3.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.TextInternal = TextInternal;
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _SpacingHelper = require("../space/SpacingHelper.js");
var _Typography = require("../../elements/typography/Typography.js");
var _componentHelper = require("../../shared/component-helper.js");
var _useStatSkeleton = _interopRequireDefault(require("./useStatSkeleton.js"));
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, {
...rest,
'aria-label': ariaLabel,
style,
className: (0, _classnames.default)(textClassName, (0, _SpacingHelper.createSpacingClasses)(props), 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 _react.default.createElement(Element, attributes, children);
}
TextInternal._supportsSpacingProps = true;
function Text(props) {
return _react.default.createElement(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