UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

83 lines 2.99 kB
import React from 'react'; import classnames from 'classnames'; import { createSpacingClasses } from "../space/SpacingHelper.js"; import { getHeadingLineHeightSize } from "../../elements/typography/Typography.js"; import { convertJsxToString, validateDOMAttributes } from "../../shared/component-helper.js"; import useStatSkeleton from "./useStatSkeleton.js"; 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 } = useStatSkeleton(skeleton, skeletonMethod); const resolvedSignTone = resolveSignTone(colorizeBySign, children); const textValue = convertJsxToString(children); const srText = srLabel ? `${convertJsxToString(srLabel)}${' '}${textValue}` : textValue; const ariaLabel = srLabel ? srText : rest['aria-label']; const attributes = validateDOMAttributes(props, { ...rest, 'aria-label': ariaLabel, style, className: classnames(textClassName, createSpacingClasses(props), skeletonClass, className, textClassName && [fontSize && `dnb-t__size--${fontSize} dnb-t__line-height--${getHeadingLineHeightSize(fontSize)}`, fontWeight && `dnb-t__weight--${fontWeight}`], resolvedSignTone && `dnb-stat--tone-${resolvedSignTone}`) }); applySkeletonAttributes(attributes); return React.createElement(Element, attributes, children); } TextInternal._supportsSpacingProps = true; function Text(props) { return React.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 convertJsxToString(children); } export { TextInternal }; export default Text; //# sourceMappingURL=Text.js.map