@shopify/hydrogen-react
Version:
React components, hooks, and utilities for creating custom Shopify storefronts
50 lines (49 loc) • 1.69 kB
JavaScript
import { jsx } from "react/jsx-runtime";
import { useCallback } from "react";
import { useCart } from "./CartProvider.mjs";
import { useCartLine } from "./CartLineProvider.mjs";
import { BaseButton } from "./BaseButton.mjs";
function CartLineQuantityAdjustButton(props) {
const { status, linesRemove, linesUpdate } = useCart();
const cartLine = useCartLine();
const { children, adjust, onClick, ...passthroughProps } = props;
const handleAdjust = useCallback(() => {
if (adjust === "remove") {
linesRemove([(cartLine == null ? void 0 : cartLine.id) ?? ""]);
return;
}
const quantity = adjust === "decrease" ? ((cartLine == null ? void 0 : cartLine.quantity) ?? 0) - 1 : ((cartLine == null ? void 0 : cartLine.quantity) ?? 0) + 1;
if (quantity <= 0) {
linesRemove([(cartLine == null ? void 0 : cartLine.id) ?? ""]);
return;
}
const lineUpdate = {
id: (cartLine == null ? void 0 : cartLine.id) ?? "",
quantity,
attributes: (cartLine == null ? void 0 : cartLine.attributes) ?? []
};
linesUpdate([lineUpdate]);
}, [
adjust,
cartLine == null ? void 0 : cartLine.attributes,
cartLine == null ? void 0 : cartLine.id,
cartLine == null ? void 0 : cartLine.quantity,
linesRemove,
linesUpdate
]);
const disabledAttr = passthroughProps.disabled;
return /* @__PURE__ */ jsx(
BaseButton,
{
...passthroughProps,
onClick,
defaultOnClick: handleAdjust,
disabled: typeof disabledAttr !== "undefined" ? disabledAttr : status !== "idle",
children
}
);
}
export {
CartLineQuantityAdjustButton
};
//# sourceMappingURL=CartLineQuantityAdjustButton.mjs.map