index.tsx 2.8 KB

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