index.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { useState } from 'react'
  2. import { Swiper, SwiperItem, Image, View } from '@tarojs/components'
  3. import './index.css'
  4. interface SwiperItemType {
  5. value: string
  6. ariaLabel?: string
  7. }
  8. interface SwiperProps {
  9. images: string[] | SwiperItemType[]
  10. autoplay?: boolean
  11. current?: number
  12. interval?: number
  13. duration?: number
  14. loop?: boolean
  15. indicatorDots?: boolean
  16. indicatorColor?: string
  17. indicatorActiveColor?: string
  18. vertical?: boolean
  19. previousMargin?: string
  20. nextMargin?: string
  21. displayMultipleItems?: number
  22. height?: string
  23. onChange?: (current: number) => void
  24. onTap?: (index: number) => void
  25. }
  26. export default function TDesignSwiper({
  27. images = [],
  28. autoplay = true,
  29. current = 0,
  30. interval = 5000,
  31. duration = 300,
  32. loop = true,
  33. indicatorDots = true,
  34. indicatorColor = '#999',
  35. indicatorActiveColor = '#fa550f',
  36. vertical = false,
  37. previousMargin = '0',
  38. nextMargin = '0',
  39. displayMultipleItems = 1,
  40. height = '192rpx',
  41. onChange,
  42. onTap
  43. }: SwiperProps) {
  44. const [currentIndex, setCurrentIndex] = useState(current)
  45. const handleChange = (e: any) => {
  46. const newIndex = e.detail.current
  47. setCurrentIndex(newIndex)
  48. onChange?.(newIndex)
  49. }
  50. const handleTap = (index: number) => {
  51. onTap?.(index)
  52. }
  53. const getImageUrl = (item: string | SwiperItemType): string => {
  54. return typeof item === 'string' ? item : item.value
  55. }
  56. const getAriaLabel = (item: string | SwiperItemType, index: number): string => {
  57. if (typeof item === 'object' && item.ariaLabel) {
  58. return item.ariaLabel
  59. }
  60. return `轮播图 ${index + 1}`
  61. }
  62. return (
  63. <View className="tdesign-swiper">
  64. <Swiper
  65. className="tdesign-swiper__host"
  66. autoplay={autoplay}
  67. current={currentIndex}
  68. interval={interval}
  69. duration={duration}
  70. circular={loop}
  71. vertical={vertical}
  72. previousMargin={previousMargin}
  73. nextMargin={nextMargin}
  74. displayMultipleItems={displayMultipleItems}
  75. indicatorDots={indicatorDots}
  76. indicatorColor={indicatorColor}
  77. indicatorActiveColor={indicatorActiveColor}
  78. style={{ height }}
  79. onChange={handleChange}
  80. >
  81. {images.map((item: string | SwiperItemType, index: number) => (
  82. <SwiperItem key={index}>
  83. <View
  84. className="tdesign-swiper__item"
  85. onClick={() => handleTap(index)}
  86. aria-hidden={currentIndex !== index}
  87. aria-role="image"
  88. aria-label={getAriaLabel(item, index)}
  89. >
  90. <Image
  91. src={getImageUrl(item)}
  92. mode="aspectFill"
  93. className="tdesign-swiper__image"
  94. style={{ height }}
  95. />
  96. </View>
  97. </SwiperItem>
  98. ))}
  99. </Swiper>
  100. </View>
  101. )
  102. }