Files
mp-10550/src/components/GiftCardList.tsx
赵忠林 ecb5d9059a feat(components): 新增 GiftCard礼品卡组件
- 新增 GiftCard 组件,支持多种类型礼品卡的展示和交互
- 组件包含商品信息、价格、折扣、使用指南等丰富功能- 优化图像展示,支持单
2025-08-17 00:06:03 +08:00

176 lines
5.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import React from 'react'
import { View, ScrollView } from '@tarojs/components'
import GiftCard, { GiftCardProps } from './GiftCard'
export interface GiftCardListProps {
/** 礼品卡列表数据 */
gifts: GiftCardProps[]
/** 列表标题 */
title?: string
/** 布局方式vertical-垂直布局 horizontal-水平滚动 grid-网格布局 */
layout?: 'vertical' | 'horizontal' | 'grid'
/** 网格列数仅在grid布局时有效 */
columns?: number
/** 是否显示空状态 */
showEmpty?: boolean
/** 空状态文案 */
emptyText?: string
/** 礼品卡点击事件 */
onGiftClick?: (gift: GiftCardProps, index: number) => void
/** 礼品卡使用事件 */
onGiftUse?: (gift: GiftCardProps, index: number) => void
/** 礼品卡详情事件 */
onGiftDetail?: (gift: GiftCardProps, index: number) => void
}
const GiftCardList: React.FC<GiftCardListProps> = ({
gifts = [],
title,
layout = 'vertical',
columns = 2,
showEmpty = true,
emptyText = '暂无礼品卡',
onGiftClick,
onGiftUse,
onGiftDetail
}) => {
const handleGiftClick = (gift: GiftCardProps, index: number) => {
onGiftClick?.(gift, index)
}
const handleGiftUse = (gift: GiftCardProps, index: number) => {
onGiftUse?.(gift, index)
}
const handleGiftDetail = (gift: GiftCardProps, index: number) => {
onGiftDetail?.(gift, index)
}
// 垂直布局
if (layout === 'vertical') {
return (
<View className="p-4">
{title && (
<View className="font-semibold text-gray-800 mb-4 text-lg">{title}</View>
)}
{gifts.length === 0 ? (
showEmpty && (
<View className="text-center py-16 px-5">
<View className="text-gray-400 mb-4">
<View className="w-16 h-16 mx-auto bg-gray-100 rounded-full flex items-center justify-center">
<View className="text-2xl">🎁</View>
</View>
</View>
<View className="text-gray-500 text-base">{emptyText}</View>
</View>
)
) : (
gifts.map((gift, index) => (
<>
<GiftCard
key={gift.id || index}
{...gift}
onClick={() => handleGiftClick(gift, index)}
onUse={() => handleGiftUse(gift, index)}
onDetail={() => handleGiftDetail(gift, index)}
/>
</>
))
)}
</View>
)
}
// 网格布局
if (layout === 'grid') {
return (
<View className="p-4">
{title && (
<View className="font-semibold text-gray-800 mb-4 text-lg">{title}</View>
)}
{gifts.length === 0 ? (
showEmpty && (
<View className="text-center py-16 px-5">
<View className="text-gray-400 mb-4">
<View className="w-16 h-16 mx-auto bg-gray-100 rounded-full flex items-center justify-center">
<View className="text-2xl">🎁</View>
</View>
</View>
<View className="text-gray-500 text-base">{emptyText}</View>
</View>
)
) : (
<View
className="flex flex-wrap"
style={{gap: '12px'}}
>
{gifts.map((gift, index) => (
<View
key={gift.id || index}
className="w-full"
style={{width: `calc(${100/columns}% - ${12*(columns-1)/columns}px)`}}
>
<GiftCard
{...gift}
onClick={() => handleGiftClick(gift, index)}
onUse={() => handleGiftUse(gift, index)}
onDetail={() => handleGiftDetail(gift, index)}
/>
</View>
))}
</View>
)}
</View>
)
}
// 水平滚动布局
return (
<View>
{title && (
<View className="font-semibold text-gray-800 mb-4 pl-4 text-lg">
{title}
</View>
)}
{gifts.length === 0 ? (
showEmpty && (
<View className="text-center py-16 px-5">
<View className="text-gray-400 mb-4">
<View className="w-16 h-16 mx-auto bg-gray-100 rounded-full flex items-center justify-center">
<View className="text-2xl">🎁</View>
</View>
</View>
<View className="text-gray-500 text-base">{emptyText}</View>
</View>
)
) : (
<ScrollView
scrollX
className="flex p-4 gap-3 overflow-x-auto"
showScrollbar={false}
style={{}}
>
{gifts.map((gift, index) => (
<View
key={gift.id || index}
className="flex-shrink-0 w-80 mb-0"
>
<GiftCard
{...gift}
onClick={() => handleGiftClick(gift, index)}
onUse={() => handleGiftUse(gift, index)}
onDetail={() => handleGiftDetail(gift, index)}
/>
</View>
))}
</ScrollView>
)}
</View>
)
}
export default GiftCardList