跳到正文
简体中文

用法

import { ShopCart } from './ShopCart.jsx'

// Without callbacks the cart runs on its sample bag. Wire it to your shop:
<ShopCart
  locale="en"
  currency="USD"
  items={bag.lines}            // [{ id, name, variant, price, qty, image }]
  freeFrom={80}
  delivery={5}
  loading={bag.loading}
  error={bag.error}
  onRetry={bag.reload}
  onQuantityChange={(id, qty) => bag.setQuantity(id, qty)}
  onRemove={(id) => bag.remove(id)}
  onApplyCode={async (code) => (await checkCode(code)) ? 0.1 : false}
  onCheckout={async (lines, totals) => (await placeOrder(lines, totals)).number}
/>

属性

属性类型默认值作用
locale'en' | 'zh-CN' | 'es' | 'ja''en'内置文字和示例购物袋的语言。
itemsArray<{ id, name, variant, price, qty, image?, max? }>sample bag购物袋里的商品行。传入新数组会替换整个购物袋,之后数量和移除由区块自己维护。`image` 是商品图片,不传时画一个素色包裹。
currencystringby locale所有金额使用的 ISO 货币代码。
freeFromnumberby locale扣除优惠后达到此金额即包邮,进度条朝它填满。
deliverynumberby locale未达包邮时的运费。
loadingbooleanfalse加载时显示购物袋的骨架。
errorboolean | stringfalse显示带重试按钮的错误提示;传字符串可替换说明。
onQuantityChange(id, qty) => void—数量改变时调用。
onRemove(id) => void—移除了一行,6 秒内仍可撤销。
onApplyCode(code) => Promise<number | false>—核对优惠码:返回折扣比例(0.1 即 10%)或 false。
onCheckout(lines, totals) => Promise<string | void>—提交订单;返回订单号即可显示。
onContinue / onBrowse / onRetry() => void—分别在下单后、购物袋为空时和出错提示中调用。
labelsobject—替换任意内置文字(键名见 copy.js)。

装进你的项目

把指令粘贴给 Codex 或 Claude Code。Agent 会读取公开的源码,先告诉你要写入哪些文件,再把区块连同它用到的组件一起加进项目。

ShopCart.jsxshop-cart.csscopy.jskit.jsxkit.cssui/base-button/ui/empty-state/ui/rolling-number/ui/alert-banner/ui/success-check/ui/number-input/ui/text-input/ui/skeleton-shimmer/tokens.cssREADME.md

区块本身、样式和文案、共用的 kit、用到的组件和 tokens.css 会一起装进同一个文件夹。