@shopify/hydrogen-react
Version:
React components, hooks, and utilities for creating custom Shopify storefronts
44 lines (43 loc) • 1.31 kB
JavaScript
import { jsxs, Fragment } from "react/jsx-runtime";
import { useMoney } from "./useMoney.mjs";
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 ?? "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