跳到正文
简体中文

用法

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

// Without callbacks the page runs on its sample tea. Your own pictures and facts replace the drawings:
const product = {
  id: 'house-blend', name: 'House blend', kind: 'Coffee beans · medium roast', rating: 4.6, reviews: 88,
  images: [{ src: '/img/blend-bag.jpg', label: 'The bag' }, { src: '/img/blend-beans.jpg', label: 'The beans' }],
  specs: [{ icon: 'globe', label: 'Origin', value: 'Colombia' }, { icon: 'clock', label: 'Roasted', value: 'This week' }],
  sizes: [{ id: '250', label: '250 g', price: 12, note: 'about 16 cups' }, { id: '1k', label: '1 kg', price: 38, stock: 4 }],
}

<ShopProduct
  locale="en"
  currency="USD"
  freeFrom={80}
  product={product}
  soldOut={product.stock === 0}
  saved={wishlist.has(product.id)}
  bagCount={bag.count}
  loading={loading}
  error={error}
  onRetry={reload}
  onAdd={({ id, size, qty }) => bag.add(id, size, qty)}
  onSave={(id, saved) => wishlist.toggle(id, saved)}
  onNotify={(email, id) => restock.subscribe(email, id)}
  onViewBag={() => router.push('/bag')}
/>

属性

属性类型默认值作用
locale'en' | 'zh-CN' | 'es' | 'ja''en'内置文字和示例茶叶的语言。
product{ id, name, kind?, rating?, reviews?, tone?, notes?, notesLabel?, images?: Array<{ id?, src, label?, alt? }>, specs?: Array<{ icon?, label, value }>, specsLabel?, size?, sizes: Array<{ id, label, price, stock?, note?, noteIcon?, pack?: 'pouch' | 'tin' | 'big', cups? }>, brew? }sample tea商品信息。`images` 是商品图片(不传时显示示例插画);`specs` 是按钮下方的参数,每项可用 kit 图标名或你自己的元素;规格的 `note` 显示在价格旁。每个规格有自己的价格;`size` 是默认选中的规格;`stock` 不超过 5 时提示库存紧张。示例里的 `pack`、`cups`、`brew` 仍然可用。
currencystringby locale所有价格使用的 ISO 货币代码。
freeFromnumberby locale包邮门槛,显示在库存旁边。
soldOutbooleanfalse显示为售罄,加购按钮换成到货提醒。
saved / bagCountboolean / numberfalse / 0心形是否一开始就是实心,以及购物袋里已有的件数。
loadingbooleanfalse加载时显示页面骨架。
errorboolean | stringfalse显示带重试按钮的错误提示;传字符串可替换说明。
onAdd({ id, size, qty }) => Promise<void>—加入购物袋;完成前按钮转圈,完成后出现确认提示。
onSave(id, saved) => void—收藏或取消收藏时调用。
onNotify(email, id) => Promise<void>—保存售罄商品的到货提醒邮箱。
onViewBag / onRetry() => void—分别对应加购后的“去购物袋”和错误提示里的重试按钮。
labelsobject—替换任意内置文字(键名见 copy.js)。
classNamestring—加在根元素上;其他属性也会传给根元素。

装进你的项目

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

ShopProduct.jsxshop-product.csscopy.jskit.jsxkit.cssui/base-button/ui/like-button/ui/rolling-number/ui/status-tag/ui/alert-banner/ui/success-check/ui/number-input/ui/radio-group/ui/rating-stars/ui/text-input/ui/skeleton-shimmer/tokens.cssREADME.md

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