@momo-kits/carousel 0.125.4-rc.7 → 0.150.1-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/index.tsx +77 -67
  2. package/package.json +20 -21
  3. package/publish.sh +3 -12
  4. package/types.ts +1 -1
package/index.tsx CHANGED
@@ -10,9 +10,9 @@ import {
10
10
  View,
11
11
  ViewStyle,
12
12
  } from 'react-native';
13
- import {defaultAnimatedStyles, defaultScrollInterpolator} from './animation';
14
- import {CarouselProps, CarouselRef, CarouselState, Position} from './types';
15
- import {Spacing} from '@momo-kits/foundation';
13
+ import { defaultAnimatedStyles, defaultScrollInterpolator } from './animation';
14
+ import { CarouselProps, CarouselRef, CarouselState, Position } from './types';
15
+ import { Spacing } from '@momo-kits/foundation';
16
16
 
17
17
  const IS_ANDROID = Platform.OS === 'android';
18
18
  const IS_IOS = Platform.OS === 'ios';
@@ -53,17 +53,17 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
53
53
  _positions: Position[];
54
54
  _currentScrollOffset;
55
55
  _scrollEnabled;
56
- _initTimeout?: NodeJS.Timeout;
57
- _apparitionTimeout?: NodeJS.Timeout;
58
- _enableAutoplayTimeout?: NodeJS.Timeout;
59
- _autoplayTimeout?: NodeJS.Timeout;
60
- _snapNoMomentumTimeout?: NodeJS.Timeout;
61
- _androidRepositioningTimeout?: NodeJS.Timeout;
56
+ _initTimeout?: ReturnType<typeof setTimeout>;
57
+ _apparitionTimeout?: ReturnType<typeof setTimeout>;
58
+ _enableAutoplayTimeout?: ReturnType<typeof setTimeout>;
59
+ _autoplayTimeout?: ReturnType<typeof setTimeout>;
60
+ _snapNoMomentumTimeout?: ReturnType<typeof setTimeout>;
61
+ _androidRepositioningTimeout?: ReturnType<typeof setTimeout>;
62
62
  _scrollPos?: Animated.Value;
63
63
  _onScrollHandler?: (...args: any[]) => void;
64
64
  _autoplay?: boolean;
65
65
  _autoplaying?: boolean;
66
- _autoplayInterval?: NodeJS.Timeout;
66
+ _autoplayInterval?: ReturnType<typeof setInterval>;
67
67
  _carouselRef: any;
68
68
  _onLayoutInitDone?: boolean;
69
69
 
@@ -100,7 +100,7 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
100
100
  }
101
101
 
102
102
  componentDidMount() {
103
- const {apparitionDelay, autoplay} = this.props;
103
+ const { apparitionDelay, autoplay } = this.props;
104
104
 
105
105
  this._mounted = true;
106
106
  this._initPositionsAndInterpolators();
@@ -113,7 +113,7 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
113
113
 
114
114
  const apparitionCallback = () => {
115
115
  if (apparitionDelay) {
116
- this.setState({hideCarousel: false});
116
+ this.setState({ hideCarousel: false });
117
117
  }
118
118
  if (autoplay) {
119
119
  this.startAutoplay();
@@ -131,8 +131,8 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
131
131
  }
132
132
 
133
133
  componentDidUpdate(prevProps: CarouselProps) {
134
- const {interpolators} = this.state;
135
- const {firstItem = 0, scrollEnabled} = this.props;
134
+ const { interpolators } = this.state;
135
+ const { firstItem = 0, scrollEnabled } = this.props;
136
136
  const itemsLength = this._getCustomDataLength(this.props);
137
137
 
138
138
  if (!itemsLength) {
@@ -175,12 +175,15 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
175
175
  componentWillUnmount() {
176
176
  this._mounted = false;
177
177
  this.stopAutoplay();
178
- clearTimeout(this._initTimeout);
179
- clearTimeout(this._apparitionTimeout);
180
- clearTimeout(this._enableAutoplayTimeout);
181
- clearTimeout(this._autoplayTimeout);
182
- clearTimeout(this._snapNoMomentumTimeout);
183
- clearTimeout(this._androidRepositioningTimeout);
178
+ if (this._initTimeout != null) clearTimeout(this._initTimeout);
179
+ if (this._apparitionTimeout != null) clearTimeout(this._apparitionTimeout);
180
+ if (this._enableAutoplayTimeout != null)
181
+ clearTimeout(this._enableAutoplayTimeout);
182
+ if (this._autoplayTimeout != null) clearTimeout(this._autoplayTimeout);
183
+ if (this._snapNoMomentumTimeout != null)
184
+ clearTimeout(this._snapNoMomentumTimeout);
185
+ if (this._androidRepositioningTimeout != null)
186
+ clearTimeout(this._androidRepositioningTimeout);
184
187
  }
185
188
 
186
189
  _setScrollHandler(props: CarouselProps) {
@@ -189,7 +192,9 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
189
192
  useNativeDriver: true,
190
193
  };
191
194
  this._scrollPos = new Animated.Value(0);
192
- const argMapping = [{nativeEvent: {contentOffset: {x: this._scrollPos}}}];
195
+ const argMapping = [
196
+ { nativeEvent: { contentOffset: { x: this._scrollPos } } },
197
+ ];
193
198
 
194
199
  if (props.onScroll && Array.isArray(props.onScroll._argMapping)) {
195
200
  argMapping.pop();
@@ -206,17 +211,17 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
206
211
  }
207
212
 
208
213
  _enableLoop() {
209
- const {data, enableSnap, loop} = this.props;
214
+ const { data, enableSnap, loop } = this.props;
210
215
  return enableSnap && loop && data && data.length && data.length > 1;
211
216
  }
212
217
 
213
218
  _shouldAnimateSlides(props = this.props) {
214
- const {inactiveSlideOpacity = 1, inactiveSlideScale = 1} = props;
219
+ const { inactiveSlideOpacity = 1, inactiveSlideScale = 1 } = props;
215
220
  return inactiveSlideOpacity < 1 || inactiveSlideScale < 1;
216
221
  }
217
222
 
218
223
  _shouldRepositionScroll(index: number) {
219
- const {data, enableSnap, loopClonesPerSide = 3} = this.props;
224
+ const { data, enableSnap, loopClonesPerSide = 3 } = this.props;
220
225
  const dataLength = data && data.length;
221
226
  return !(
222
227
  !enableSnap ||
@@ -231,7 +236,7 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
231
236
  }
232
237
 
233
238
  _getCustomData(props = this.props) {
234
- const {data, loopClonesPerSide = 3} = props;
239
+ const { data, loopClonesPerSide = 3 } = props;
235
240
  const dataLength = data && data.length;
236
241
 
237
242
  if (!dataLength) {
@@ -265,7 +270,7 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
265
270
  }
266
271
 
267
272
  _getCustomDataLength(props = this.props) {
268
- const {data, loopClonesPerSide = 3} = props;
273
+ const { data, loopClonesPerSide = 3 } = props;
269
274
  const dataLength = data && data.length;
270
275
 
271
276
  if (!dataLength) {
@@ -286,7 +291,7 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
286
291
  }
287
292
 
288
293
  _getDataIndex(index: number) {
289
- const {data, loopClonesPerSide = 3} = this.props;
294
+ const { data, loopClonesPerSide = 3 } = this.props;
290
295
  const dataLength = data && data.length;
291
296
  if (!this._enableLoop() || !dataLength) {
292
297
  return index;
@@ -322,7 +327,7 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
322
327
  }
323
328
 
324
329
  _getFirstItem(index: number, props = this.props) {
325
- const {loopClonesPerSide = 3} = props;
330
+ const { loopClonesPerSide = 3 } = props;
326
331
  const itemsLength = this._getCustomDataLength(props);
327
332
 
328
333
  if (!itemsLength || index > itemsLength - 1 || index < 0) {
@@ -345,8 +350,8 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
345
350
  }
346
351
 
347
352
  _getItemMainDimension() {
348
- const {itemWidth} = this.state;
349
- const {full} = this.props;
353
+ const { itemWidth } = this.state;
354
+ const { full } = this.props;
350
355
  return full ? itemWidth : itemWidth + Spacing.S;
351
356
  }
352
357
 
@@ -374,7 +379,7 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
374
379
  }
375
380
 
376
381
  _getActiveSlideOffset() {
377
- const {activeSlideOffset = 0} = this.props;
382
+ const { activeSlideOffset = 0 } = this.props;
378
383
  const itemMainDimension = this._getItemMainDimension();
379
384
  const minOffset = 10;
380
385
  return itemMainDimension / 2 - activeSlideOffset >= minOffset
@@ -401,7 +406,7 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
401
406
  }
402
407
 
403
408
  for (let i = 0; i < this._positions.length; i++) {
404
- const {start, end} = this._positions[i];
409
+ const { start, end } = this._positions[i];
405
410
  if (
406
411
  center + activeSlideOffset >= start &&
407
412
  center - activeSlideOffset <= end
@@ -415,7 +420,7 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
415
420
  }
416
421
 
417
422
  _initPositionsAndInterpolators(props = this.props) {
418
- const {data} = props;
423
+ const { data } = props;
419
424
  const itemMainDimension = this._getItemMainDimension();
420
425
 
421
426
  if (!data || !data.length) {
@@ -439,7 +444,7 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
439
444
  } else {
440
445
  let interpolator = defaultScrollInterpolator(
441
446
  _index,
442
- this.state.itemWidth
447
+ this.state.itemWidth,
443
448
  );
444
449
 
445
450
  animatedValue = this._scrollPos.interpolate({
@@ -451,11 +456,11 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
451
456
  interpolators.push(animatedValue);
452
457
  });
453
458
 
454
- this.setState({interpolators});
459
+ this.setState({ interpolators });
455
460
  }
456
461
 
457
462
  _repositionScroll(index: number, animated = false) {
458
- const {data, loopClonesPerSide = 3} = this.props;
463
+ const { data, loopClonesPerSide = 3 } = this.props;
459
464
  const dataLength = data && data.length;
460
465
 
461
466
  if (typeof index === 'undefined' || !this._shouldRepositionScroll(index)) {
@@ -474,7 +479,7 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
474
479
  }
475
480
 
476
481
  _onTouchStart(event: any) {
477
- const {onTouchStart} = this.props;
482
+ const { onTouchStart } = this.props;
478
483
 
479
484
  if (this._getScrollEnabled() !== false && this._autoplaying) {
480
485
  this.pauseAutoPlay();
@@ -484,7 +489,7 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
484
489
  }
485
490
 
486
491
  _onTouchEnd(event: GestureResponderEvent) {
487
- const {onTouchEnd} = this.props;
492
+ const { onTouchEnd } = this.props;
488
493
 
489
494
  if (
490
495
  this._getScrollEnabled() !== false &&
@@ -498,7 +503,7 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
498
503
  }
499
504
 
500
505
  _onScroll(event: NativeSyntheticEvent<NativeScrollEvent>) {
501
- const {onScroll, onScrollIndexChanged, onSnapToItem} = this.props;
506
+ const { onScroll, onScrollIndexChanged, onSnapToItem } = this.props;
502
507
  const scrollOffset = event
503
508
  ? this._getScrollOffset(event)
504
509
  : this._currentScrollOffset;
@@ -531,8 +536,8 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
531
536
  }
532
537
 
533
538
  _onMomentumScrollEnd(event: NativeSyntheticEvent<NativeScrollEvent>) {
534
- const {autoplayDelay, onMomentumScrollEnd, onSnapToItem} = this.props;
535
- const {itemWidth} = this.state;
539
+ const { autoplayDelay, onMomentumScrollEnd, onSnapToItem } = this.props;
540
+ const { itemWidth } = this.state;
536
541
  const scrollOffset = event
537
542
  ? this._getScrollOffset(event)
538
543
  : this._currentScrollOffset;
@@ -553,7 +558,8 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
553
558
  onMomentumScrollEnd && onMomentumScrollEnd(event);
554
559
 
555
560
  if (IS_ANDROID && this._autoplay && !this._autoplaying) {
556
- clearTimeout(this._enableAutoplayTimeout);
561
+ if (this._enableAutoplayTimeout != null)
562
+ clearTimeout(this._enableAutoplayTimeout);
557
563
  this._enableAutoplayTimeout = setTimeout(() => {
558
564
  this.startAutoplay();
559
565
  }, autoplayDelay);
@@ -561,7 +567,7 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
561
567
  }
562
568
 
563
569
  _onLayout(event: LayoutChangeEvent) {
564
- const {onLayout, visibleItem = 1} = this.props;
570
+ const { onLayout, visibleItem = 1 } = this.props;
565
571
 
566
572
  if (this._onLayoutInitDone) {
567
573
  this._initPositionsAndInterpolators();
@@ -574,7 +580,7 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
574
580
  this.props.visibleItem === 1
575
581
  ? screenWidth - Spacing.M * 2
576
582
  : Math.ceil(
577
- (containerWidth * 0.9 - visibleItem * Spacing.S) / visibleItem
583
+ (containerWidth * 0.9 - visibleItem * Spacing.S) / visibleItem,
578
584
  );
579
585
  if (this.props.itemWidth) {
580
586
  itemWidth = this.props.itemWidth;
@@ -583,13 +589,13 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
583
589
  itemWidth = containerWidth;
584
590
  }
585
591
 
586
- this.setState({containerWidth, itemWidth});
592
+ this.setState({ containerWidth, itemWidth });
587
593
 
588
594
  onLayout && onLayout(event);
589
595
  }
590
596
 
591
597
  _getPositionIndex(index: number) {
592
- const {loop, loopClonesPerSide = 3} = this.props;
598
+ const { loop, loopClonesPerSide = 3 } = this.props;
593
599
  return loop ? index + loopClonesPerSide : index;
594
600
  }
595
601
 
@@ -597,9 +603,9 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
597
603
  index: number,
598
604
  animated = true,
599
605
  fireCallback = true,
600
- forceScrollTo = false
606
+ forceScrollTo = false,
601
607
  ) {
602
- const {onSnapToItem} = this.props;
608
+ const { onSnapToItem } = this.props;
603
609
  const itemsLength = this._getCustomDataLength();
604
610
  const wrappedRef = this._getWrappedRef();
605
611
  if (!itemsLength || !wrappedRef) {
@@ -641,10 +647,10 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
641
647
  }
642
648
  }
643
649
 
644
- _renderItem(info: {item: any; index: number}) {
645
- const {item, index} = info;
646
- const {interpolators, itemWidth} = this.state;
647
- const {slideStyle, full} = this.props;
650
+ _renderItem(info: { item: any; index: number }) {
651
+ const { item, index } = info;
652
+ const { interpolators, itemWidth } = this.state;
653
+ const { slideStyle, full } = this.props;
648
654
  const animatedValue = interpolators && interpolators[index];
649
655
 
650
656
  if (typeof animatedValue === 'undefined') {
@@ -653,10 +659,10 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
653
659
 
654
660
  const animate = this._shouldAnimateSlides();
655
661
  const Component = animate ? Animated.View : View;
656
- const mainDimension = {width: itemWidth};
662
+ const mainDimension = { width: itemWidth };
657
663
 
658
664
  let spacingStyle: ViewStyle = this.props.loop
659
- ? {marginLeft: Spacing.S}
665
+ ? { marginLeft: Spacing.S }
660
666
  : {
661
667
  marginLeft: index === 0 ? Spacing.M : 0,
662
668
  marginRight:
@@ -672,11 +678,12 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
672
678
  style={[
673
679
  mainDimension,
674
680
  animatedStyle,
675
- {overflow: 'hidden'},
681
+ { overflow: 'hidden' },
676
682
  spacingStyle,
677
683
  slideStyle,
678
684
  ]}
679
- pointerEvents="box-none">
685
+ pointerEvents="box-none"
686
+ >
680
687
  {this.props.renderItem({
681
688
  item,
682
689
  index,
@@ -686,14 +693,14 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
686
693
  }
687
694
 
688
695
  startAutoplay() {
689
- const {autoplayInterval, autoplayDelay} = this.props;
696
+ const { autoplayInterval, autoplayDelay } = this.props;
690
697
  this._autoplay = true;
691
698
 
692
699
  if (this._autoplaying) {
693
700
  return;
694
701
  }
695
702
 
696
- clearTimeout(this._autoplayTimeout);
703
+ if (this._autoplayTimeout != null) clearTimeout(this._autoplayTimeout);
697
704
  this._autoplayTimeout = setTimeout(() => {
698
705
  this._autoplaying = true;
699
706
  this._autoplayInterval = setInterval(() => {
@@ -706,9 +713,10 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
706
713
 
707
714
  pauseAutoPlay() {
708
715
  this._autoplaying = false;
709
- clearTimeout(this._autoplayTimeout);
710
- clearTimeout(this._enableAutoplayTimeout);
711
- clearInterval(this._autoplayInterval);
716
+ if (this._autoplayTimeout != null) clearTimeout(this._autoplayTimeout);
717
+ if (this._enableAutoplayTimeout != null)
718
+ clearTimeout(this._enableAutoplayTimeout);
719
+ if (this._autoplayInterval != null) clearInterval(this._autoplayInterval);
712
720
  }
713
721
 
714
722
  stopAutoplay() {
@@ -762,7 +770,7 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
762
770
  enableSnap,
763
771
  contentContainerStyle,
764
772
  } = this.props;
765
- const {hideCarousel} = this.state;
773
+ const { hideCarousel } = this.state;
766
774
 
767
775
  const initialNumPerSide = this._enableLoop() ? loopClonesPerSide : 2;
768
776
  const initialNumToRender =
@@ -787,7 +795,9 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
787
795
  <Animated.FlatList
788
796
  {...this.props}
789
797
  {...specificProps}
790
- ref={c => (this._carouselRef = c)}
798
+ ref={c => {
799
+ this._carouselRef = c;
800
+ }}
791
801
  overScrollMode={'never'}
792
802
  snapToInterval={
793
803
  this.props.snapToInterval ? this.props.snapToInterval : snapToInterval
@@ -798,8 +808,8 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
798
808
  numColumns={1}
799
809
  style={[
800
810
  style,
801
- {width: '100%', flexDirection: 'row'},
802
- hideCarousel ? {opacity: 0} : {},
811
+ { width: '100%', flexDirection: 'row' },
812
+ hideCarousel ? { opacity: 0 } : {},
803
813
  ]}
804
814
  automaticallyAdjustContentInsets={false}
805
815
  directionalLockEnabled
@@ -826,5 +836,5 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
826
836
  }
827
837
  }
828
838
 
829
- export {Carousel};
830
- export type {CarouselProps, CarouselRef};
839
+ export { Carousel };
840
+ export type { CarouselProps, CarouselRef };
package/package.json CHANGED
@@ -1,22 +1,21 @@
1
1
  {
2
- "name": "@momo-kits/carousel",
3
- "version": "0.125.4-rc.7",
4
- "private": false,
5
- "main": "index.tsx",
6
- "peerDependencies": {
7
- "@momo-kits/foundation": "latest",
8
- "react": "16.9.0",
9
- "react-native": ">=0.55",
10
- "prop-types": "^15.7.2"
11
- },
12
- "devDependencies": {
13
- "@momo-platform/versions": "4.1.11"
14
- },
15
- "license": "MoMo",
16
- "publishConfig": {
17
- "registry": "https://registry.npmjs.org/"
18
- },
19
- "dependencies": {
20
- "moment": "^2.24.0"
21
- }
22
- }
2
+ "name": "@momo-kits/carousel",
3
+ "version": "0.150.1-beta.1",
4
+ "private": false,
5
+ "main": "index.tsx",
6
+ "peerDependencies": {
7
+ "@momo-kits/foundation": "latest",
8
+ "react": "*",
9
+ "react-native": "*"
10
+ },
11
+ "devDependencies": {
12
+ "@momo-platform/versions": "4.1.11"
13
+ },
14
+ "license": "MoMo",
15
+ "publishConfig": {
16
+ "registry": "https://registry.npmjs.org/"
17
+ },
18
+ "dependencies": {
19
+ "moment": "^2.24.0"
20
+ }
21
+ }
package/publish.sh CHANGED
@@ -1,12 +1,5 @@
1
1
  #!/bin/bash
2
2
 
3
- # Prepare dist files
4
- rm -rf dist
5
- mkdir dist
6
- rsync -r --exclude=/dist ./* dist
7
- cd dist
8
-
9
-
10
3
  if [ "$1" == "stable" ]; then
11
4
  npm version $(npm view @momo-kits/foundation@stable version)
12
5
  npm version patch
@@ -14,15 +7,13 @@ if [ "$1" == "stable" ]; then
14
7
  elif [ "$1" == "latest" ]; then
15
8
  npm version $(npm view @momo-kits/foundation@latest version)
16
9
  npm publish --tag latest --access=public
17
- else
10
+ elif [ "$1" == "beta" ]; then
18
11
  npm version $(npm view @momo-kits/carousel@beta version)
19
12
  npm version prerelease --preid=beta
20
13
  npm publish --tag beta --access=public
14
+ else
15
+ npm publish --tag alpha --access=public
21
16
  fi
22
17
 
23
18
  PACKAGE_NAME=$(npm pkg get name)
24
19
  NEW_PACKAGE_VERSION=$(npm pkg get version)
25
-
26
- # Clean up
27
- cd ..
28
- rm -rf dist
package/types.ts CHANGED
@@ -42,7 +42,7 @@ export type CarouselProps = {
42
42
  visibleItem?: number;
43
43
  contentContainerStyle?: StyleProp<ViewStyle>;
44
44
  full?: boolean;
45
- snapToInterval?: number | Animated.Value | Animated.AnimatedInterpolation | undefined
45
+ snapToInterval?: number | Animated.Value | Animated.AnimatedInterpolation<number | string> | undefined
46
46
  };
47
47
 
48
48
  export type CarouselRef = {