index.tsx 10 KB

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