|
| 1 | +import type { SwiperProps, SwiperSlideProps } from 'swiper/react'; |
| 2 | + |
| 3 | +import { isUndefined } from 'lodash'; |
| 4 | +import React from 'react'; |
| 5 | +import { |
| 6 | + A11y, |
| 7 | + Autoplay, |
| 8 | + Controller, |
| 9 | + EffectCoverflow, |
| 10 | + EffectCube, |
| 11 | + EffectFade, |
| 12 | + EffectFlip, |
| 13 | + EffectCreative, |
| 14 | + EffectCards, |
| 15 | + HashNavigation, |
| 16 | + History, |
| 17 | + Keyboard, |
| 18 | + Lazy, |
| 19 | + Mousewheel, |
| 20 | + Navigation, |
| 21 | + Pagination, |
| 22 | + Parallax, |
| 23 | + Scrollbar, |
| 24 | + Thumbs, |
| 25 | + Virtual, |
| 26 | + Zoom, |
| 27 | + FreeMode, |
| 28 | + Grid, |
| 29 | + Manipulation, |
| 30 | +} from 'swiper'; |
| 31 | +import 'swiper/css/bundle'; |
| 32 | +import { Swiper, SwiperSlide } from 'swiper/react'; |
| 33 | + |
| 34 | +import { useComponentConfig, usePrefixConfig } from '../../hooks'; |
| 35 | +import { LeftOutlined, RightOutlined } from '../../icons'; |
| 36 | +import { getClassName, registerComponentMate } from '../../utils'; |
| 37 | + |
| 38 | +export type DSlidesProps = SwiperProps; |
| 39 | + |
| 40 | +const { COMPONENT_NAME } = registerComponentMate({ COMPONENT_NAME: 'DSlides' }); |
| 41 | +export function DSlides(props: DSlidesProps): JSX.Element | null { |
| 42 | + const { |
| 43 | + children, |
| 44 | + |
| 45 | + navigation: _navigation, |
| 46 | + pagination: _pagination, |
| 47 | + direction = 'horizontal', |
| 48 | + |
| 49 | + className, |
| 50 | + ...restProps |
| 51 | + } = useComponentConfig(COMPONENT_NAME, props); |
| 52 | + |
| 53 | + //#region Context |
| 54 | + const dPrefix = usePrefixConfig(); |
| 55 | + //#endregion |
| 56 | + |
| 57 | + const navigation = (() => { |
| 58 | + const configs = { |
| 59 | + nextEl: '.swiper-button-next', |
| 60 | + prevEl: '.swiper-button-prev', |
| 61 | + }; |
| 62 | + if (_navigation === true || isUndefined(_navigation)) { |
| 63 | + return configs; |
| 64 | + } |
| 65 | + if (_navigation !== false) { |
| 66 | + return Object.assign(configs, _navigation); |
| 67 | + } |
| 68 | + return _navigation; |
| 69 | + })(); |
| 70 | + |
| 71 | + const pagination = (() => { |
| 72 | + const configs = { |
| 73 | + clickable: true, |
| 74 | + }; |
| 75 | + if (_pagination === true || isUndefined(_pagination)) { |
| 76 | + return configs; |
| 77 | + } |
| 78 | + if (_pagination !== false) { |
| 79 | + return Object.assign(configs, _pagination); |
| 80 | + } |
| 81 | + return _pagination; |
| 82 | + })(); |
| 83 | + |
| 84 | + return ( |
| 85 | + <Swiper |
| 86 | + {...restProps} |
| 87 | + className={getClassName(className, `${dPrefix}slides`, { |
| 88 | + [`${dPrefix}slides--vertical`]: direction === 'vertical', |
| 89 | + })} |
| 90 | + direction={direction} |
| 91 | + modules={[ |
| 92 | + A11y, |
| 93 | + Autoplay, |
| 94 | + Controller, |
| 95 | + EffectCoverflow, |
| 96 | + EffectCube, |
| 97 | + EffectFade, |
| 98 | + EffectFlip, |
| 99 | + EffectCreative, |
| 100 | + EffectCards, |
| 101 | + HashNavigation, |
| 102 | + History, |
| 103 | + Keyboard, |
| 104 | + Lazy, |
| 105 | + Mousewheel, |
| 106 | + Navigation, |
| 107 | + Pagination, |
| 108 | + Parallax, |
| 109 | + Scrollbar, |
| 110 | + Thumbs, |
| 111 | + Virtual, |
| 112 | + Zoom, |
| 113 | + FreeMode, |
| 114 | + Grid, |
| 115 | + Manipulation, |
| 116 | + ]} |
| 117 | + navigation={navigation} |
| 118 | + pagination={pagination} |
| 119 | + > |
| 120 | + {React.Children.map(children, (child) => |
| 121 | + React.isValidElement(child) |
| 122 | + ? React.createElement(SwiperSlide, { |
| 123 | + ...(child.props as SwiperSlideProps), |
| 124 | + className: getClassName((child.props as SwiperSlideProps).className, `${dPrefix}slide`), |
| 125 | + }) |
| 126 | + : child |
| 127 | + )} |
| 128 | + <button className="swiper-button-prev"> |
| 129 | + <LeftOutlined /> |
| 130 | + </button> |
| 131 | + <button className="swiper-button-next"> |
| 132 | + <RightOutlined /> |
| 133 | + </button> |
| 134 | + </Swiper> |
| 135 | + ); |
| 136 | +} |
0 commit comments