UNPKG

@carbon/ibm-products

Version:
143 lines (141 loc) 6.11 kB
/** * Copyright IBM Corp. 2020, 2026 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ import { __toESM } from "../../_virtual/_rolldown/runtime.js"; import { require_classnames } from "../../node_modules/classnames/index.js"; import { pkg } from "../../settings.js"; import { getDevtoolsProps } from "../../global/js/utils/devtools.js"; import { TooltipTrigger } from "../TooltipTrigger/TooltipTrigger.js"; import { getSupportedLocale } from "../../global/js/utils/getSupportedLocale.js"; import { DefaultLocale, formatValue, getIconSize } from "./constants.js"; import { BigNumberSkeleton } from "./BigNumberSkeleton.js"; import React, { forwardRef } from "react"; import PropTypes from "prop-types"; import { Tooltip } from "@carbon/react"; import { ArrowUp, Information } from "@carbon/react/icons"; //#region src/components/BigNumber/BigNumber.tsx /** * Copyright IBM Corp. 2024, 2025 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ var import_classnames = /* @__PURE__ */ __toESM(require_classnames()); const blockClass = `${pkg.prefix}--big-number`; const componentName = "BigNumber"; /** * BigNumber is used to display large values in a small area. The display of * values can be the value itself, or grouped in a `numerator/denominator` fashion. * Control over the total fraction decimals displayed as well as how the * values/totals are displayed are done via a locale prop. Other optional props * allow control over size, truncation, if the value is a percentage, the addition * of a button as well as tool tip functionality. * The default locale is English (`en-US`) if one is not provided or if the provided one is not supported. */ const BigNumber = forwardRef(({ className, forceShowTotal = false, fractionDigits = 1, iconButton, loading = false, label, locale = DefaultLocale, percentage = false, size = "default", tooltipDescription, total, trending = false, truncate = true, value, ...rest }, ref) => { const bigNumberClasses = (0, import_classnames.default)(className, { [`${blockClass}--lg`]: size === "lg", [`${blockClass}--xl`]: size === "xl" }); const supportedLocale = getSupportedLocale(locale, DefaultLocale); const truncatedValue = formatValue(supportedLocale, value, fractionDigits, truncate) ?? "–"; const truncatedTotal = formatValue(supportedLocale, total, fractionDigits, truncate) ?? "Unknown"; const shouldDisplayDenominator = forceShowTotal || !percentage && total && value && total > value && truncatedValue !== truncatedTotal; if (loading) return /* @__PURE__ */ React.createElement(BigNumberSkeleton, { ...rest, ref, className, size, ...getDevtoolsProps(componentName) }); return /* @__PURE__ */ React.createElement("div", { ...rest, className: (0, import_classnames.default)(blockClass, bigNumberClasses, className), ref, ...getDevtoolsProps(componentName) }, /* @__PURE__ */ React.createElement("span", { className: `${blockClass}__row` }, /* @__PURE__ */ React.createElement("h4", { className: `${blockClass}__label` }, label), tooltipDescription && /* @__PURE__ */ React.createElement(Tooltip, { description: tooltipDescription, align: "right", className: `${blockClass}__info` }, /* @__PURE__ */ React.createElement(TooltipTrigger, { className: `${blockClass}__tooltip-trigger` }, /* @__PURE__ */ React.createElement(Information, { size: 16 })))), /* @__PURE__ */ React.createElement("span", { className: `${blockClass}__row`, role: "math" }, trending && /* @__PURE__ */ React.createElement(ArrowUp, { className: `${blockClass}__trend`, size: getIconSize(size) }), /* @__PURE__ */ React.createElement("span", { className: `${blockClass}__value` }, percentage ? /* @__PURE__ */ React.createElement("div", { className: `${blockClass}__percentage` }, truncatedValue, /* @__PURE__ */ React.createElement("span", { className: `${blockClass}__percentage-mark` }, "%")) : truncatedValue), shouldDisplayDenominator && /* @__PURE__ */ React.createElement("span", { className: `${blockClass}__total` }, /* @__PURE__ */ React.createElement("span", null, `/${truncatedTotal}`)), /* @__PURE__ */ React.createElement("span", { className: `${blockClass}__icon-button` }, iconButton))); }); BigNumber.displayName = componentName; BigNumber.propTypes = { /** * Provide an optional class to be applied to the containing node. */ className: PropTypes.string, /** * The default behavior will hide `total` if `undefined` or is the same as `value`. * * Set to `true` to ignore the default behavior and show the `total`. */ forceShowTotal: PropTypes.bool, /** * Specifies the number of fraction digits when truncating `value` and `total`. */ fractionDigits: PropTypes.number, /** * Displays an icon button next to `value`. */ iconButton: PropTypes.node, /** * Text label above the `value`. */ label: PropTypes.string.isRequired, /** * When `true`, will show the loading state. */ loading: PropTypes.bool, /** * Determines how `value` and `total` will be formatted. */ locale: PropTypes.string, /** * Appends a percent sign (_%_) after `value` and hides `total`. */ percentage: PropTypes.bool, /** * */ size: PropTypes.oneOf([ "default", "lg", "xl" ]), /** * When applied, an information icon will be rendered next to the * `label` and the description will be applied to its tooltip. */ tooltipDescription: PropTypes.string, /** * The number that will appear after the slash (i.e. the "denominator" of a fraction). * * This number will not be rendered if it's the same as `value` or * `percentage` is true. See also the **forceShowTotal** prop. */ total: PropTypes.number, /** * When `true`, will render a "trending up" icon. */ trending: PropTypes.bool, /** * Abbreviates the number when `true`. E.g. from _1,000_ to _1K_. */ truncate: PropTypes.bool, /** * The primary value to display (or the "numerator" of a fraction). */ value: PropTypes.number }; //#endregion export { BigNumber };