1
0

index.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /* eslint-disable jsx-a11y/no-static-element-interactions */
  2. import React, { ReactNode, Children, ReactChild, ReactFragment, ReactPortal } from 'react';
  3. import cls from 'classnames';
  4. import PropTypes from 'prop-types';
  5. import BaseComponent from "../_base/baseComponent";
  6. import { CarouselProps } from './interface';
  7. import { cssClasses, numbers, strings } from '@douyinfe/semi-foundation/carousel/constants';
  8. import CarouselFoundation, { CarouselAdapter } from '@douyinfe/semi-foundation/carousel/foundation';
  9. import CarouselIndicator from './CarouselIndicator';
  10. import CarouselArrow from './CarouselArrow';
  11. import '@douyinfe/semi-foundation/carousel/carousel.scss';
  12. import { debounce } from 'lodash';
  13. import isNullOrUndefined from '@douyinfe/semi-foundation/utils/isNullOrUndefined';
  14. export interface CarouselState {
  15. activeIndex: number;
  16. children: (ReactChild | ReactFragment | ReactPortal)[];
  17. preIndex: number;
  18. isReverse: boolean;
  19. isInit: boolean;
  20. }
  21. class Carousel extends BaseComponent<CarouselProps, CarouselState> {
  22. static propTypes = {
  23. activeIndex: PropTypes.number,
  24. animation:PropTypes.oneOf(strings.ANIMATION_MAP),
  25. arrowProps: PropTypes.object,
  26. autoPlay: PropTypes.oneOfType([PropTypes.bool, PropTypes.object]),
  27. className: PropTypes.string,
  28. defaultActiveIndex: PropTypes.number,
  29. indicatorPosition: PropTypes.oneOf(strings.POSITION_MAP),
  30. indicatorSize: PropTypes.oneOf(strings.SIZE),
  31. indicatorType: PropTypes.oneOf(strings.TYPE_MAP),
  32. theme: PropTypes.oneOf(strings.THEME_MAP),
  33. onChange: PropTypes.func,
  34. arrowType: PropTypes.oneOf(strings.ARROW_MAP),
  35. showArrow: PropTypes.bool,
  36. showIndicator: PropTypes.bool,
  37. slideDirection: PropTypes.oneOf(strings.DIRECTION),
  38. speed: PropTypes.number,
  39. style: PropTypes.object,
  40. trigger: PropTypes.oneOf(strings.TRIGGER)
  41. };
  42. static defaultProps: CarouselProps = {
  43. children: [],
  44. animation: 'slide',
  45. autoPlay: true,
  46. arrowType: 'always',
  47. defaultActiveIndex: numbers.DEFAULT_ACTIVE_INDEX,
  48. indicatorPosition: 'center',
  49. indicatorSize: 'small',
  50. indicatorType: 'dot',
  51. theme: 'light',
  52. onChange: () => undefined,
  53. showArrow: true,
  54. showIndicator: true,
  55. slideDirection: 'left',
  56. speed: numbers.DEFAULT_SPEED,
  57. trigger: 'click'
  58. };
  59. foundation: CarouselFoundation;
  60. constructor(props: CarouselProps) {
  61. super(props);
  62. this.foundation = new CarouselFoundation(this.adapter);
  63. const defaultActiveIndex = this.foundation.getDefaultActiveIndex();
  64. this.state = {
  65. activeIndex: defaultActiveIndex,
  66. children: this.getChildren(),
  67. preIndex: defaultActiveIndex,
  68. isReverse: false,
  69. isInit: true
  70. };
  71. }
  72. get adapter(): CarouselAdapter<CarouselProps, CarouselState> {
  73. return {
  74. ...super.adapter,
  75. notifyChange: (activeIndex: number, preIndex: number): void => {
  76. this.props.onChange(activeIndex, preIndex);
  77. },
  78. setNewActiveIndex: (activeIndex: number): void => {
  79. this.setState({ activeIndex });
  80. },
  81. setPreActiveIndex: (preIndex: number): void => {
  82. this.setState({ preIndex });
  83. },
  84. setIsReverse: (isReverse: boolean): void => {
  85. this.setState({ isReverse });
  86. },
  87. setIsInit: (isInit: boolean): void => {
  88. this.setState({ isInit });
  89. }
  90. };
  91. }
  92. static getDerivedStateFromProps(props: CarouselProps, state: CarouselState): Partial<CarouselState> {
  93. const states: Partial<CarouselState> = {};
  94. if (!isNullOrUndefined(props.activeIndex) && props.activeIndex !== state.activeIndex) {
  95. states.activeIndex = props.activeIndex;
  96. }
  97. return states;
  98. }
  99. componentDidMount(): void {
  100. this.handleAutoPlay();
  101. }
  102. componentWillUnmount(): void {
  103. this.foundation.destroy();
  104. }
  105. play = (): void => {
  106. return this.foundation.handleAutoPlay();
  107. }
  108. stop = (): void => {
  109. return this.foundation.stop();
  110. };
  111. goTo = ( targetIndex: number): void => {
  112. return this.foundation.goTo(targetIndex);
  113. };
  114. prev = (): void => {
  115. return this.foundation.prev();
  116. };
  117. next = (): void => {
  118. return this.foundation.next();
  119. };
  120. handleAutoPlay = (): void => {
  121. if (!this.foundation.getIsControledComponent()){
  122. this.foundation.handleAutoPlay();
  123. }
  124. }
  125. handleMouseEnter = (): void => {
  126. const { autoPlay } = this.props;
  127. if (typeof autoPlay !== 'object' || autoPlay.hoverToPause){
  128. this.foundation.stop();
  129. }
  130. }
  131. handleMouseLeave = (): void => {
  132. const { autoPlay } = this.props;
  133. if ((typeof autoPlay !== 'object' || autoPlay.hoverToPause) && !this.foundation.getIsControledComponent()){
  134. this.foundation.handleAutoPlay();
  135. }
  136. }
  137. onIndicatorChange = (activeIndex: number): void => {
  138. return this.foundation.onIndicatorChange(activeIndex);
  139. };
  140. getChildren = (): (ReactChild | ReactFragment | ReactPortal)[] => {
  141. const { children: originChildren } = this.props;
  142. return Children.toArray(originChildren).filter(child=>{
  143. return React.isValidElement(child);
  144. });
  145. }
  146. getValidIndex = (activeIndex: number): number => {
  147. return this.foundation.getValidIndex(activeIndex);
  148. };
  149. renderChildren = () => {
  150. const { speed, animation } = this.props;
  151. const { activeIndex, children, preIndex, isInit } = this.state;
  152. return (
  153. <>
  154. {children.map((child: any, index: number) => {
  155. const isCurrent = index === activeIndex;
  156. const isPrev = index === this.getValidIndex(activeIndex - 1);
  157. const isNext = index === this.getValidIndex(activeIndex + 1);
  158. const animateStyle = {
  159. transitionTimingFunction: 'ease',
  160. transitionDuration: `${speed}ms`,
  161. animationTimingFunction: 'ease',
  162. animationDuration: `${speed}ms`,
  163. };
  164. return React.cloneElement(child, {
  165. style: {
  166. ...child.props.style,
  167. ...animateStyle,
  168. },
  169. className: cls(child.props.className, {
  170. [`${cssClasses.CAROUSEL_CONTENT}-item-prev`]: isPrev,
  171. [`${cssClasses.CAROUSEL_CONTENT}-item-next`]: isNext,
  172. [`${cssClasses.CAROUSEL_CONTENT}-item-current`]: isCurrent,
  173. [`${cssClasses.CAROUSEL_CONTENT}-item`]: true,
  174. [`${cssClasses.CAROUSEL_CONTENT}-item-active`]: isCurrent,
  175. [`${cssClasses.CAROUSEL_CONTENT}-item-slide-in`]:animation === 'slide' && !isInit && isCurrent,
  176. [`${cssClasses.CAROUSEL_CONTENT}-item-slide-out`]:animation === 'slide' && !isInit && index === preIndex,
  177. })
  178. });
  179. })}
  180. </>
  181. );
  182. }
  183. renderIndicator = () => {
  184. const { children, activeIndex } = this.state;
  185. const { showIndicator, indicatorType, theme, indicatorPosition, indicatorSize, trigger } = this.props;
  186. const carouselIndicatorCls = cls({
  187. [cssClasses.CAROUSEL_INDICATOR]: true
  188. });
  189. if (showIndicator && children.length > 1){
  190. return (
  191. <div className={carouselIndicatorCls}>
  192. <CarouselIndicator
  193. type={indicatorType}
  194. total={children.length}
  195. activeIndex={activeIndex}
  196. position={indicatorPosition}
  197. trigger={trigger}
  198. size={indicatorSize}
  199. theme={theme}
  200. onIndicatorChange={this.onIndicatorChange}
  201. />
  202. </div>
  203. );
  204. }
  205. return null;
  206. }
  207. renderArrow = () => {
  208. const { children } = this.state;
  209. const { showArrow, arrowType, theme, arrowProps } = this.props;
  210. if (showArrow && children.length > 1){
  211. return (
  212. <CarouselArrow
  213. type={arrowType}
  214. theme={theme}
  215. prev={this.prev}
  216. next={this.next}
  217. arrowProps={arrowProps}
  218. />
  219. );
  220. }
  221. return null;
  222. };
  223. render(): ReactNode {
  224. const { animation, className, style, slideDirection } = this.props;
  225. const { isReverse } = this.state;
  226. const carouselWrapperCls = cls(className, {
  227. [cssClasses.CAROUSEL]: true
  228. });
  229. return (
  230. <div
  231. // role='listbox'
  232. // tabIndex={0}
  233. className={carouselWrapperCls}
  234. style={style}
  235. onMouseEnter={debounce(this.handleMouseEnter, 400)}
  236. onMouseLeave={debounce(this.handleMouseLeave, 400)}
  237. // onMouseEnter={this.handleMouseEnter}
  238. // onMouseLeave={this.handleMouseLeave}
  239. // onKeyDown={e => this.foundation.handleKeyDown(e)}
  240. >
  241. <div
  242. className={cls([`${cssClasses.CAROUSEL_CONTENT}-${animation}`], {
  243. [`${cssClasses.CAROUSEL_CONTENT}`]: true,
  244. [`${cssClasses.CAROUSEL_CONTENT}-reverse`]: slideDirection === 'left' ? isReverse : !isReverse,
  245. })}
  246. x-semi-prop="children"
  247. >
  248. {this.renderChildren()}
  249. </div>
  250. {this.renderIndicator()}
  251. {this.renderArrow()}
  252. </div>
  253. );
  254. }
  255. }
  256. export default Carousel;