|
| 1 | +import React, { useEffect, useMemo, useState } from 'react'; |
| 2 | +import cx from 'classnames'; |
| 3 | + |
| 4 | +import type { ActionSheetProps } from './ActionSheet'; |
| 5 | +import type { ActionSheetItem } from './type'; |
| 6 | + |
| 7 | +import { Grid, GridItem } from '../grid'; |
| 8 | +import { Swiper, SwiperProps } from '../swiper'; |
| 9 | +import { usePrefixClass } from '../hooks/useClass'; |
| 10 | + |
| 11 | +type ActionSheetGridProps = Pick<ActionSheetProps, 'items' | 'align'> & { |
| 12 | + onSelected?: (idx: number) => void; |
| 13 | + count?: number; |
| 14 | +}; |
| 15 | + |
| 16 | +export function ActionSheetGrid(props: ActionSheetGridProps) { |
| 17 | + const { items = [], count = 8, onSelected } = props; |
| 18 | + |
| 19 | + const actionSheetClass = usePrefixClass('action-sheet'); |
| 20 | + |
| 21 | + const [direction, setDirection] = useState<SwiperProps['direction']>('vertical'); |
| 22 | + |
| 23 | + const gridColumn = Math.ceil(count / 2); |
| 24 | + const pageNum = Math.ceil(items.length / count); |
| 25 | + |
| 26 | + const actionItems = useMemo(() => { |
| 27 | + const res: ActionSheetProps['items'][] = []; |
| 28 | + for (let i = 0; i < pageNum; i++) { |
| 29 | + const temp = items.slice(i * count, (i + 1) * count); |
| 30 | + res.push(temp); |
| 31 | + } |
| 32 | + return res; |
| 33 | + }, [items, count, pageNum]); |
| 34 | + |
| 35 | + useEffect(() => { |
| 36 | + setDirection('horizontal'); |
| 37 | + }, []); |
| 38 | + |
| 39 | + return ( |
| 40 | + <div |
| 41 | + className={cx({ |
| 42 | + [`${actionSheetClass}__grid`]: true, |
| 43 | + [`${actionSheetClass}__grid--swiper`]: pageNum > 1, |
| 44 | + [`${actionSheetClass}__dots`]: pageNum > 1, |
| 45 | + })} |
| 46 | + > |
| 47 | + <Swiper |
| 48 | + autoplay={false} |
| 49 | + className={cx(`${actionSheetClass}__swiper-wrap--base`, pageNum > 1 && `${actionSheetClass}__swiper-wrap`)} |
| 50 | + loop={false} |
| 51 | + navigation={pageNum > 1 ? { type: 'dots' } : undefined} |
| 52 | + direction={direction} |
| 53 | + height={pageNum > 1 ? 208 : 196} |
| 54 | + > |
| 55 | + {actionItems.map((item, idx1) => ( |
| 56 | + <Swiper.SwiperItem key={idx1}> |
| 57 | + <Grid gutter={0} column={gridColumn} style={{ width: '100%' }}> |
| 58 | + {item.map((it, idx2) => { |
| 59 | + let label: string; |
| 60 | + let image: React.ReactNode; |
| 61 | + let badge: ActionSheetItem['badge']; |
| 62 | + if (typeof it === 'string') { |
| 63 | + label = it; |
| 64 | + } else { |
| 65 | + label = it.label; |
| 66 | + image = it.icon; |
| 67 | + badge = it.badge; |
| 68 | + } |
| 69 | + return ( |
| 70 | + <GridItem |
| 71 | + key={`${idx1}-${idx2}`} |
| 72 | + image={image} |
| 73 | + text={label} |
| 74 | + badge={badge} |
| 75 | + // @ts-ignore |
| 76 | + onClick={() => { |
| 77 | + onSelected?.(idx1 * count + idx2); |
| 78 | + }} |
| 79 | + /> |
| 80 | + ); |
| 81 | + })} |
| 82 | + </Grid> |
| 83 | + </Swiper.SwiperItem> |
| 84 | + ))} |
| 85 | + </Swiper> |
| 86 | + </div> |
| 87 | + ); |
| 88 | +} |
0 commit comments