@shopify/hydrogen-react
Version:
React components, hooks, and utilities for creating custom Shopify storefronts
41 lines (40 loc) • 1.3 kB
JavaScript
import { useMoney } from "./useMoney.mjs";
import { jsxs, Fragment } from "react/jsx-runtime";
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(data);
const Wrapper = as != null ? 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__ */ jsxs(Wrapper, {
...passthroughProps,
children: [output, measurement && measurement.referenceUnit && /* @__PURE__ */ jsxs(Fragment, {
children: [measurementSeparator, measurement.referenceUnit]
})]
});
}
function isMoney(maybeMoney) {
return typeof maybeMoney.amount === "string" && !!maybeMoney.amount && typeof maybeMoney.currencyCode === "string" && !!maybeMoney.currencyCode;
}
export {
Money
};
//# sourceMappingURL=Money.mjs.map