UNPKG

@shopify/hydrogen-react

Version:

React components, hooks, and utilities for creating custom Shopify storefronts

44 lines (43 loc) 1.44 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const jsxRuntime = require("react/jsx-runtime"); const useMoney = require("./useMoney.js"); function Money({ data, as, withoutCurrency, withoutTrailingZeros, measurement, measurementSeparator = "/", ...passthroughProps }) { if (!isMoney(data)) { throw new Error( `<Money/> needs a valid 'data' prop that has 'amount' and 'currencyCode'` ); } const moneyObject = useMoney.useMoney(data); const Wrapper = as ?? "div"; let output = moneyObject.localizedString; if (withoutCurrency || withoutTrailingZeros) { if (withoutCurrency && !withoutTrailingZeros) { output = moneyObject.amount; } else if (!withoutCurrency && withoutTrailingZeros) { output = moneyObject.withoutTrailingZeros; } else { output = moneyObject.withoutTrailingZerosAndCurrency; } } return /* @__PURE__ */ jsxRuntime.jsxs(Wrapper, { ...passthroughProps, children: [ output, measurement && measurement.referenceUnit && /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [ measurementSeparator, measurement.referenceUnit ] }) ] }); } function isMoney(maybeMoney) { return typeof maybeMoney.amount === "string" && !!maybeMoney.amount && typeof maybeMoney.currencyCode === "string" && !!maybeMoney.currencyCode; } exports.Money = Money; //# sourceMappingURL=Money.js.map