foundation.ts 5.1 KB

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