foundation.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import { isObject, get } from 'lodash';
  2. import BaseFoundation, { DefaultAdapter } from '../base/foundation';
  3. import { numbers } from './constants';
  4. export interface CarouselAdapter<P = Record<string, any>, S = Record<string, any>> extends DefaultAdapter<P, S> {
  5. notifyChange: (activeIndex: number, preIndex: number) => void;
  6. setNewActiveIndex: (activeIndex: number) => void;
  7. setPreActiveIndex: (activeIndex: number) => void;
  8. setIsReverse: (isReverse: boolean) => void;
  9. setIsInit: (isInit: boolean) => void;
  10. getChildren: () => any[]
  11. }
  12. class CarouselFoundation<P = Record<string, any>, S = Record<string, any>> extends BaseFoundation<CarouselAdapter<P, S>, P, S> {
  13. constructor(adapter: CarouselAdapter<P, S>) {
  14. super({ ...adapter });
  15. }
  16. _interval = null;
  17. _forcePlay = false;
  18. setForcePlay(forcePlay: boolean) {
  19. this._forcePlay = forcePlay;
  20. }
  21. play(interval: number): void {
  22. if (this._interval) {
  23. clearInterval(this._interval);
  24. }
  25. this._interval = setInterval(() => {
  26. this.next();
  27. }, interval);
  28. }
  29. stop(): void {
  30. if (this._interval) {
  31. clearInterval(this._interval);
  32. }
  33. }
  34. goTo(activeIndex: number): void {
  35. const { activeIndex: stateActiveIndex } = this.getStates();
  36. const targetIndex = this.getValidIndex(activeIndex);
  37. this._adapter.setIsReverse(stateActiveIndex > targetIndex);
  38. if (this.getIsControlledComponent()) {
  39. this._notifyChange(targetIndex);
  40. } else {
  41. this._notifyChange(targetIndex);
  42. this.handleNewActiveIndex(targetIndex);
  43. }
  44. }
  45. next(): void {
  46. this.stop();
  47. const { activeIndex: stateActiveIndex } = this.getStates();
  48. const targetIndex = this.getValidIndex(stateActiveIndex + 1);
  49. this._adapter.setIsReverse(false);
  50. if (this.getIsControlledComponent()) {
  51. this._notifyChange(targetIndex);
  52. } else {
  53. this._notifyChange(targetIndex);
  54. this.handleNewActiveIndex(targetIndex);
  55. }
  56. this.handleAutoPlay();
  57. }
  58. prev(): void {
  59. this.stop();
  60. const { activeIndex: stateActiveIndex } = this.getStates();
  61. const targetIndex = this.getValidIndex(stateActiveIndex - 1);
  62. this._adapter.setIsReverse(true);
  63. if (this.getIsControlledComponent()) {
  64. this._notifyChange(targetIndex);
  65. } else {
  66. this._notifyChange(targetIndex);
  67. this.handleNewActiveIndex(targetIndex);
  68. }
  69. this.handleAutoPlay();
  70. }
  71. destroy(): void {
  72. this._unregisterInterval();
  73. }
  74. _unregisterInterval() {
  75. if (this._interval) {
  76. clearInterval(this._interval);
  77. this._interval = null;
  78. }
  79. }
  80. _notifyChange(activeIndex: number): void {
  81. const { activeIndex: stateActiveIndex, isInit } = this.getStates();
  82. if (isInit) {
  83. this._adapter.setIsInit(false);
  84. }
  85. if (stateActiveIndex !== activeIndex) {
  86. this._adapter.setPreActiveIndex(stateActiveIndex);
  87. this._adapter.notifyChange(activeIndex, stateActiveIndex);
  88. }
  89. }
  90. getValidIndex(index: number): number {
  91. const children = this._adapter.getChildren();
  92. return (index + children.length) % children.length;
  93. }
  94. getSwitchingTime(): number {
  95. const { autoPlay, speed } = this.getProps();
  96. const autoPlayType = typeof autoPlay;
  97. if (autoPlayType === 'boolean') {
  98. return numbers.DEFAULT_INTERVAL + speed;
  99. }
  100. if (isObject(autoPlay)) {
  101. return get(autoPlay, 'interval', numbers.DEFAULT_INTERVAL) + speed;
  102. }
  103. return speed;
  104. }
  105. getIsControlledComponent(): boolean {
  106. return this._isInProps('activeIndex');
  107. }
  108. handleAutoPlay(): void {
  109. const { autoPlay } = this.getProps();
  110. const children = this._adapter.getChildren();
  111. const autoPlayType = typeof autoPlay;
  112. // when user manually call the play function, force play
  113. // only when carousel children length > 1 to start play
  114. if (children.length > 1 && ((autoPlay === true) || isObject(autoPlay) || this._forcePlay)) {
  115. this.play(this.getSwitchingTime());
  116. }
  117. }
  118. handleKeyDown(event: any): void {
  119. if (event.key === 'ArrowLeft') {
  120. this.prev();
  121. }
  122. if (event.key === 'ArrowRight') {
  123. this.next();
  124. }
  125. }
  126. onIndicatorChange(activeIndex: number): void {
  127. const { activeIndex: stateActiveIndex } = this.getStates();
  128. this._adapter.setIsReverse(stateActiveIndex > activeIndex);
  129. this._notifyChange(activeIndex);
  130. if (!this.getIsControlledComponent()) {
  131. this.handleNewActiveIndex(activeIndex);
  132. }
  133. }
  134. handleNewActiveIndex(activeIndex: number): void {
  135. const { activeIndex: stateActiveIndex } = this.getStates();
  136. if (stateActiveIndex !== activeIndex) {
  137. this._adapter.setNewActiveIndex(activeIndex);
  138. }
  139. }
  140. getDefaultActiveIndex(): number {
  141. let activeIndex;
  142. const props = this.getProps();
  143. if ('activeIndex' in props) {
  144. activeIndex = props.activeIndex;
  145. } else if ('defaultActiveIndex' in props) {
  146. activeIndex = props.defaultActiveIndex;
  147. }
  148. return activeIndex;
  149. }
  150. }
  151. export default CarouselFoundation;