UNPKG

@shopify/hydrogen-react

Version:

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

50 lines (49 loc) 1.88 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const jsxRuntime = require("react/jsx-runtime"); const React = require("react"); const CartProvider = require("./CartProvider.js"); const CartLineProvider = require("./CartLineProvider.js"); const BaseButton = require("./BaseButton.js"); function CartLineQuantityAdjustButton(props) { const { status, linesRemove, linesUpdate } = CartProvider.useCart(); const cartLine = CartLineProvider.useCartLine(); const { children, adjust, onClick, ...passthroughProps } = props; const handleAdjust = React.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__ */ jsxRuntime.jsx( BaseButton.BaseButton, { ...passthroughProps, onClick, defaultOnClick: handleAdjust, disabled: typeof disabledAttr !== "undefined" ? disabledAttr : status !== "idle", children } ); } exports.CartLineQuantityAdjustButton = CartLineQuantityAdjustButton; //# sourceMappingURL=CartLineQuantityAdjustButton.js.map