@aws-amplify/ui-react-native 0.0.0-in-app-messaging-20220826014803

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 (242) hide show
  1. package/.eslintrc.js +100 -0
  2. package/.lintstagedrc.js +6 -0
  3. package/.turbo/turbo-build.log +4 -0
  4. package/CHANGELOG.md +11 -0
  5. package/LICENSE +201 -0
  6. package/README.md +25 -0
  7. package/babel.config.js +3 -0
  8. package/dist/InAppMessaging/components/BannerMessage/BannerMessage.d.ts +3 -0
  9. package/dist/InAppMessaging/components/BannerMessage/BannerMessage.js +16 -0
  10. package/dist/InAppMessaging/components/BannerMessage/index.d.ts +2 -0
  11. package/dist/InAppMessaging/components/BannerMessage/index.js +1 -0
  12. package/dist/InAppMessaging/components/BannerMessage/styles.d.ts +7 -0
  13. package/dist/InAppMessaging/components/BannerMessage/styles.js +77 -0
  14. package/dist/InAppMessaging/components/BannerMessage/types.d.ts +12 -0
  15. package/dist/InAppMessaging/components/BannerMessage/types.js +1 -0
  16. package/dist/InAppMessaging/components/CarouselMessage/CarouselMessage.d.ts +3 -0
  17. package/dist/InAppMessaging/components/CarouselMessage/CarouselMessage.js +20 -0
  18. package/dist/InAppMessaging/components/CarouselMessage/CarouselMessageItem.d.ts +3 -0
  19. package/dist/InAppMessaging/components/CarouselMessage/CarouselMessageItem.js +18 -0
  20. package/dist/InAppMessaging/components/CarouselMessage/index.d.ts +2 -0
  21. package/dist/InAppMessaging/components/CarouselMessage/index.js +1 -0
  22. package/dist/InAppMessaging/components/CarouselMessage/styles.d.ts +5 -0
  23. package/dist/InAppMessaging/components/CarouselMessage/styles.js +98 -0
  24. package/dist/InAppMessaging/components/CarouselMessage/types.d.ts +18 -0
  25. package/dist/InAppMessaging/components/CarouselMessage/types.js +1 -0
  26. package/dist/InAppMessaging/components/FullScreenMessage/FullScreenMessage.d.ts +3 -0
  27. package/dist/InAppMessaging/components/FullScreenMessage/FullScreenMessage.js +18 -0
  28. package/dist/InAppMessaging/components/FullScreenMessage/index.d.ts +2 -0
  29. package/dist/InAppMessaging/components/FullScreenMessage/index.js +1 -0
  30. package/dist/InAppMessaging/components/FullScreenMessage/styles.d.ts +3 -0
  31. package/dist/InAppMessaging/components/FullScreenMessage/styles.js +86 -0
  32. package/dist/InAppMessaging/components/FullScreenMessage/types.d.ts +6 -0
  33. package/dist/InAppMessaging/components/FullScreenMessage/types.js +1 -0
  34. package/dist/InAppMessaging/components/InAppMessageDisplay/InAppMessageDisplay.d.ts +10 -0
  35. package/dist/InAppMessaging/components/InAppMessageDisplay/InAppMessageDisplay.js +29 -0
  36. package/dist/InAppMessaging/components/InAppMessageDisplay/handleMessageLinkAction.d.ts +3 -0
  37. package/dist/InAppMessaging/components/InAppMessageDisplay/handleMessageLinkAction.js +24 -0
  38. package/dist/InAppMessaging/components/InAppMessageDisplay/index.d.ts +2 -0
  39. package/dist/InAppMessaging/components/InAppMessageDisplay/index.js +1 -0
  40. package/dist/InAppMessaging/components/InAppMessageDisplay/types.d.ts +24 -0
  41. package/dist/InAppMessaging/components/InAppMessageDisplay/types.js +1 -0
  42. package/dist/InAppMessaging/components/MessageLayout/MessageLayout.d.ts +3 -0
  43. package/dist/InAppMessaging/components/MessageLayout/MessageLayout.js +34 -0
  44. package/dist/InAppMessaging/components/MessageLayout/index.d.ts +1 -0
  45. package/dist/InAppMessaging/components/MessageLayout/index.js +1 -0
  46. package/dist/InAppMessaging/components/MessageLayout/types.d.ts +7 -0
  47. package/dist/InAppMessaging/components/MessageLayout/types.js +1 -0
  48. package/dist/InAppMessaging/components/MessageWrapper/MessageWrapper.d.ts +3 -0
  49. package/dist/InAppMessaging/components/MessageWrapper/MessageWrapper.js +20 -0
  50. package/dist/InAppMessaging/components/MessageWrapper/index.d.ts +2 -0
  51. package/dist/InAppMessaging/components/MessageWrapper/index.js +1 -0
  52. package/dist/InAppMessaging/components/MessageWrapper/styles.d.ts +2 -0
  53. package/dist/InAppMessaging/components/MessageWrapper/styles.js +5 -0
  54. package/dist/InAppMessaging/components/MessageWrapper/types.d.ts +10 -0
  55. package/dist/InAppMessaging/components/MessageWrapper/types.js +1 -0
  56. package/dist/InAppMessaging/components/ModalMessage/ModalMessage.d.ts +3 -0
  57. package/dist/InAppMessaging/components/ModalMessage/ModalMessage.js +18 -0
  58. package/dist/InAppMessaging/components/ModalMessage/index.d.ts +2 -0
  59. package/dist/InAppMessaging/components/ModalMessage/index.js +1 -0
  60. package/dist/InAppMessaging/components/ModalMessage/styles.d.ts +4 -0
  61. package/dist/InAppMessaging/components/ModalMessage/styles.js +99 -0
  62. package/dist/InAppMessaging/components/ModalMessage/types.d.ts +6 -0
  63. package/dist/InAppMessaging/components/ModalMessage/types.js +1 -0
  64. package/dist/InAppMessaging/components/index.d.ts +6 -0
  65. package/dist/InAppMessaging/components/index.js +2 -0
  66. package/dist/InAppMessaging/components/withInAppMessaging/index.d.ts +1 -0
  67. package/dist/InAppMessaging/components/withInAppMessaging/index.js +1 -0
  68. package/dist/InAppMessaging/components/withInAppMessaging/withInAppMessaging.d.ts +5 -0
  69. package/dist/InAppMessaging/components/withInAppMessaging/withInAppMessaging.js +11 -0
  70. package/dist/InAppMessaging/constants.d.ts +31 -0
  71. package/dist/InAppMessaging/constants.js +43 -0
  72. package/dist/InAppMessaging/hooks/index.d.ts +2 -0
  73. package/dist/InAppMessaging/hooks/index.js +2 -0
  74. package/dist/InAppMessaging/hooks/useMessageImage/constants.d.ts +8 -0
  75. package/dist/InAppMessaging/hooks/useMessageImage/constants.js +18 -0
  76. package/dist/InAppMessaging/hooks/useMessageImage/index.d.ts +2 -0
  77. package/dist/InAppMessaging/hooks/useMessageImage/index.js +1 -0
  78. package/dist/InAppMessaging/hooks/useMessageImage/types.d.ts +15 -0
  79. package/dist/InAppMessaging/hooks/useMessageImage/types.js +6 -0
  80. package/dist/InAppMessaging/hooks/useMessageImage/useMessageImage.d.ts +10 -0
  81. package/dist/InAppMessaging/hooks/useMessageImage/useMessageImage.js +50 -0
  82. package/dist/InAppMessaging/hooks/useMessageImage/utils.d.ts +4 -0
  83. package/dist/InAppMessaging/hooks/useMessageImage/utils.js +56 -0
  84. package/dist/InAppMessaging/hooks/useMessageProps/index.d.ts +2 -0
  85. package/dist/InAppMessaging/hooks/useMessageProps/index.js +1 -0
  86. package/dist/InAppMessaging/hooks/useMessageProps/types.d.ts +109 -0
  87. package/dist/InAppMessaging/hooks/useMessageProps/types.js +1 -0
  88. package/dist/InAppMessaging/hooks/useMessageProps/useMessageProps.d.ts +12 -0
  89. package/dist/InAppMessaging/hooks/useMessageProps/useMessageProps.js +49 -0
  90. package/dist/InAppMessaging/hooks/useMessageProps/utils.d.ts +43 -0
  91. package/dist/InAppMessaging/hooks/useMessageProps/utils.js +192 -0
  92. package/dist/InAppMessaging/index.d.ts +2 -0
  93. package/dist/InAppMessaging/index.js +2 -0
  94. package/dist/__mocks__/react-native-safe-area-context.d.ts +3 -0
  95. package/dist/__mocks__/react-native-safe-area-context.js +4 -0
  96. package/dist/assets/close.png +0 -0
  97. package/dist/assets/index.d.ts +5 -0
  98. package/dist/assets/index.js +4 -0
  99. package/dist/assets/index.ts +7 -0
  100. package/dist/hooks/index.d.ts +1 -0
  101. package/dist/hooks/index.js +1 -0
  102. package/dist/hooks/useDeviceOrientation/index.d.ts +1 -0
  103. package/dist/hooks/useDeviceOrientation/index.js +1 -0
  104. package/dist/hooks/useDeviceOrientation/useDeviceOrientation.d.ts +6 -0
  105. package/dist/hooks/useDeviceOrientation/useDeviceOrientation.js +33 -0
  106. package/dist/index.d.ts +1 -0
  107. package/dist/index.js +1 -0
  108. package/dist/primitives/Button/Button.d.ts +3 -0
  109. package/dist/primitives/Button/Button.js +8 -0
  110. package/dist/primitives/Button/index.d.ts +2 -0
  111. package/dist/primitives/Button/index.js +1 -0
  112. package/dist/primitives/Button/styles.d.ts +2 -0
  113. package/dist/primitives/Button/styles.js +4 -0
  114. package/dist/primitives/Button/types.d.ts +7 -0
  115. package/dist/primitives/Button/types.js +1 -0
  116. package/dist/primitives/Carousel/Carousel.d.ts +3 -0
  117. package/dist/primitives/Carousel/Carousel.js +58 -0
  118. package/dist/primitives/Carousel/CarouselPageIndicator.d.ts +3 -0
  119. package/dist/primitives/Carousel/CarouselPageIndicator.js +10 -0
  120. package/dist/primitives/Carousel/constants.d.ts +9 -0
  121. package/dist/primitives/Carousel/constants.js +18 -0
  122. package/dist/primitives/Carousel/index.d.ts +3 -0
  123. package/dist/primitives/Carousel/index.js +2 -0
  124. package/dist/primitives/Carousel/styles.d.ts +2 -0
  125. package/dist/primitives/Carousel/styles.js +11 -0
  126. package/dist/primitives/Carousel/types.d.ts +19 -0
  127. package/dist/primitives/Carousel/types.js +1 -0
  128. package/dist/primitives/IconButton/IconButton.d.ts +3 -0
  129. package/dist/primitives/IconButton/IconButton.js +9 -0
  130. package/dist/primitives/IconButton/index.d.ts +2 -0
  131. package/dist/primitives/IconButton/index.js +1 -0
  132. package/dist/primitives/IconButton/styles.d.ts +2 -0
  133. package/dist/primitives/IconButton/styles.js +9 -0
  134. package/dist/primitives/IconButton/types.d.ts +18 -0
  135. package/dist/primitives/IconButton/types.js +1 -0
  136. package/dist/primitives/index.d.ts +3 -0
  137. package/dist/primitives/index.js +3 -0
  138. package/dist/utils/index.d.ts +1 -0
  139. package/dist/utils/index.js +4 -0
  140. package/jest.config.js +20 -0
  141. package/package.json +55 -0
  142. package/src/InAppMessaging/components/BannerMessage/BannerMessage.tsx +37 -0
  143. package/src/InAppMessaging/components/BannerMessage/__tests__/BannerMessage.spec.tsx +138 -0
  144. package/src/InAppMessaging/components/BannerMessage/__tests__/__snapshots__/BannerMessage.spec.tsx.snap +224 -0
  145. package/src/InAppMessaging/components/BannerMessage/index.ts +2 -0
  146. package/src/InAppMessaging/components/BannerMessage/styles.ts +105 -0
  147. package/src/InAppMessaging/components/BannerMessage/types.ts +15 -0
  148. package/src/InAppMessaging/components/CarouselMessage/CarouselMessage.tsx +42 -0
  149. package/src/InAppMessaging/components/CarouselMessage/CarouselMessageItem.tsx +37 -0
  150. package/src/InAppMessaging/components/CarouselMessage/__tests__/CarouselMessage.spec.tsx +55 -0
  151. package/src/InAppMessaging/components/CarouselMessage/__tests__/CarouselMessageItem.spec.tsx +56 -0
  152. package/src/InAppMessaging/components/CarouselMessage/__tests__/__snapshots__/CarouselMessage.spec.tsx.snap +86 -0
  153. package/src/InAppMessaging/components/CarouselMessage/__tests__/__snapshots__/CarouselMessageItem.spec.tsx.snap +248 -0
  154. package/src/InAppMessaging/components/CarouselMessage/index.ts +2 -0
  155. package/src/InAppMessaging/components/CarouselMessage/styles.ts +125 -0
  156. package/src/InAppMessaging/components/CarouselMessage/types.ts +28 -0
  157. package/src/InAppMessaging/components/FullScreenMessage/FullScreenMessage.tsx +38 -0
  158. package/src/InAppMessaging/components/FullScreenMessage/__tests__/FullScreenMessage.spec.tsx +53 -0
  159. package/src/InAppMessaging/components/FullScreenMessage/__tests__/__snapshots__/FullScreenMessage.spec.tsx.snap +236 -0
  160. package/src/InAppMessaging/components/FullScreenMessage/index.ts +2 -0
  161. package/src/InAppMessaging/components/FullScreenMessage/styles.ts +106 -0
  162. package/src/InAppMessaging/components/FullScreenMessage/types.ts +7 -0
  163. package/src/InAppMessaging/components/InAppMessageDisplay/InAppMessageDisplay.tsx +48 -0
  164. package/src/InAppMessaging/components/InAppMessageDisplay/__tests__/InAppMessageDisplay.spec.tsx +33 -0
  165. package/src/InAppMessaging/components/InAppMessageDisplay/__tests__/__snapshots__/InAppMessageDisplay.spec.tsx.snap +9 -0
  166. package/src/InAppMessaging/components/InAppMessageDisplay/__tests__/handleMessageLinkAction.spec.ts +51 -0
  167. package/src/InAppMessaging/components/InAppMessageDisplay/handleMessageLinkAction.ts +30 -0
  168. package/src/InAppMessaging/components/InAppMessageDisplay/index.ts +2 -0
  169. package/src/InAppMessaging/components/InAppMessageDisplay/types.ts +32 -0
  170. package/src/InAppMessaging/components/MessageLayout/MessageLayout.tsx +102 -0
  171. package/src/InAppMessaging/components/MessageLayout/index.ts +1 -0
  172. package/src/InAppMessaging/components/MessageLayout/types.ts +15 -0
  173. package/src/InAppMessaging/components/MessageWrapper/MessageWrapper.tsx +34 -0
  174. package/src/InAppMessaging/components/MessageWrapper/__tests__/MessageWrapper.spec.tsx +19 -0
  175. package/src/InAppMessaging/components/MessageWrapper/__tests__/__snapshots__/MessageWrapper.spec.tsx.snap +35 -0
  176. package/src/InAppMessaging/components/MessageWrapper/index.ts +2 -0
  177. package/src/InAppMessaging/components/MessageWrapper/styles.ts +7 -0
  178. package/src/InAppMessaging/components/MessageWrapper/types.ts +12 -0
  179. package/src/InAppMessaging/components/ModalMessage/ModalMessage.tsx +37 -0
  180. package/src/InAppMessaging/components/ModalMessage/__tests__/ModalMessage.spec.tsx +155 -0
  181. package/src/InAppMessaging/components/ModalMessage/__tests__/__snapshots__/ModalMessage.spec.tsx.snap +323 -0
  182. package/src/InAppMessaging/components/ModalMessage/index.ts +2 -0
  183. package/src/InAppMessaging/components/ModalMessage/styles.ts +132 -0
  184. package/src/InAppMessaging/components/ModalMessage/types.ts +8 -0
  185. package/src/InAppMessaging/components/index.ts +6 -0
  186. package/src/InAppMessaging/components/withInAppMessaging/__tests__/__snapshots__/withInAppMessaging.spec.tsx.snap +3 -0
  187. package/src/InAppMessaging/components/withInAppMessaging/__tests__/withInAppMessaging.spec.tsx +15 -0
  188. package/src/InAppMessaging/components/withInAppMessaging/index.ts +1 -0
  189. package/src/InAppMessaging/components/withInAppMessaging/withInAppMessaging.tsx +18 -0
  190. package/src/InAppMessaging/constants.ts +56 -0
  191. package/src/InAppMessaging/hooks/index.ts +9 -0
  192. package/src/InAppMessaging/hooks/useMessageImage/__tests__/useMessageImage.spec.ts +141 -0
  193. package/src/InAppMessaging/hooks/useMessageImage/__tests__/utils.spec.ts +107 -0
  194. package/src/InAppMessaging/hooks/useMessageImage/constants.ts +27 -0
  195. package/src/InAppMessaging/hooks/useMessageImage/index.ts +2 -0
  196. package/src/InAppMessaging/hooks/useMessageImage/types.ts +18 -0
  197. package/src/InAppMessaging/hooks/useMessageImage/useMessageImage.ts +74 -0
  198. package/src/InAppMessaging/hooks/useMessageImage/utils.ts +80 -0
  199. package/src/InAppMessaging/hooks/useMessageProps/__tests__/__snapshots__/utils.spec.ts.snap +223 -0
  200. package/src/InAppMessaging/hooks/useMessageProps/__tests__/useMessageProps.spec.ts +162 -0
  201. package/src/InAppMessaging/hooks/useMessageProps/__tests__/utils.spec.ts +354 -0
  202. package/src/InAppMessaging/hooks/useMessageProps/index.ts +8 -0
  203. package/src/InAppMessaging/hooks/useMessageProps/types.ts +131 -0
  204. package/src/InAppMessaging/hooks/useMessageProps/useMessageProps.ts +70 -0
  205. package/src/InAppMessaging/hooks/useMessageProps/utils.ts +274 -0
  206. package/src/InAppMessaging/index.ts +14 -0
  207. package/src/__mocks__/react-native-safe-area-context.ts +6 -0
  208. package/src/__tests__/__snapshots__/index.spec.ts.snap +15 -0
  209. package/src/__tests__/index.spec.ts +8 -0
  210. package/src/assets/close.png +0 -0
  211. package/src/assets/index.ts +7 -0
  212. package/src/hooks/index.ts +4 -0
  213. package/src/hooks/useDeviceOrientation/__tests__/useDeviceOrientation.spec.ts +87 -0
  214. package/src/hooks/useDeviceOrientation/index.ts +4 -0
  215. package/src/hooks/useDeviceOrientation/useDeviceOrientation.ts +47 -0
  216. package/src/index.ts +1 -0
  217. package/src/primitives/Button/Button.tsx +21 -0
  218. package/src/primitives/Button/__tests__/Button.spec.tsx +24 -0
  219. package/src/primitives/Button/__tests__/__snapshots__/Button.spec.tsx.snap +52 -0
  220. package/src/primitives/Button/index.ts +2 -0
  221. package/src/primitives/Button/styles.ts +6 -0
  222. package/src/primitives/Button/types.ts +9 -0
  223. package/src/primitives/Carousel/Carousel.tsx +115 -0
  224. package/src/primitives/Carousel/CarouselPageIndicator.tsx +39 -0
  225. package/src/primitives/Carousel/__tests__/Carousel.spec.tsx +185 -0
  226. package/src/primitives/Carousel/__tests__/CarouselPageIndicator.spec.tsx +71 -0
  227. package/src/primitives/Carousel/__tests__/__snapshots__/Carousel.spec.tsx.snap +216 -0
  228. package/src/primitives/Carousel/__tests__/__snapshots__/CarouselPageIndicator.spec.tsx.snap +191 -0
  229. package/src/primitives/Carousel/constants.ts +25 -0
  230. package/src/primitives/Carousel/index.ts +3 -0
  231. package/src/primitives/Carousel/styles.ts +14 -0
  232. package/src/primitives/Carousel/types.ts +22 -0
  233. package/src/primitives/IconButton/IconButton.tsx +20 -0
  234. package/src/primitives/IconButton/__tests__/IconButton.spec.tsx +13 -0
  235. package/src/primitives/IconButton/__tests__/__snapshots__/IconButton.spec.tsx.snap +34 -0
  236. package/src/primitives/IconButton/index.ts +2 -0
  237. package/src/primitives/IconButton/styles.ts +16 -0
  238. package/src/primitives/IconButton/types.ts +27 -0
  239. package/src/primitives/index.ts +3 -0
  240. package/src/utils/index.ts +6 -0
  241. package/tsconfig.dist.json +4 -0
  242. package/tsconfig.json +21 -0
@@ -0,0 +1,115 @@
1
+ import React, { useCallback, useEffect, useRef, useState } from 'react';
2
+ import {
3
+ Dimensions,
4
+ EventSubscription,
5
+ FlatList,
6
+ ListRenderItemInfo,
7
+ ScaledSize,
8
+ View,
9
+ ViewToken,
10
+ } from 'react-native';
11
+
12
+ import CarouselPageIndicator from './CarouselPageIndicator';
13
+ import { VIEWABILITY_CONFIG } from './constants';
14
+ import { styles } from './styles';
15
+ import { CarouselProps } from './types';
16
+
17
+ export default function Carousel<T>(
18
+ props: CarouselProps<T>
19
+ ): JSX.Element | null {
20
+ const {
21
+ data,
22
+ indicatorActiveStyle,
23
+ indicatorInactiveStyle,
24
+ renderItem,
25
+ style,
26
+ } = props;
27
+ const flatListRef = useRef<FlatList>(null);
28
+ const indexRef = useRef<ViewToken['index']>(0);
29
+ const [currentIndex, setCurrentIndex] = useState<ViewToken['index']>(0);
30
+ const windowWidthRef = useRef(Dimensions.get('window').width);
31
+ const [width, setWidth] = useState(windowWidthRef.current);
32
+
33
+ const onViewableItemsChanged = useRef(
34
+ ({ viewableItems }: { viewableItems: ViewToken[] }) => {
35
+ if (viewableItems.length !== 1) {
36
+ return;
37
+ }
38
+ const [item] = viewableItems;
39
+ indexRef.current = item.index;
40
+ setCurrentIndex(indexRef.current);
41
+ }
42
+ );
43
+
44
+ const updateOrientation = useCallback((updatedWidth: number) => {
45
+ if (windowWidthRef.current !== updatedWidth) {
46
+ windowWidthRef.current = updatedWidth;
47
+ setWidth(updatedWidth);
48
+ }
49
+ }, []);
50
+
51
+ useEffect(() => {
52
+ // on width change (due to orientation change), jump to the new index offset
53
+ flatListRef?.current?.scrollToOffset({
54
+ offset: width * indexRef.current!,
55
+ animated: false,
56
+ });
57
+ }, [width]);
58
+
59
+ useEffect(() => {
60
+ const orientationHandler = ({ window }: { window: ScaledSize }) => {
61
+ updateOrientation(window.width);
62
+ };
63
+ const subscription = Dimensions.addEventListener(
64
+ 'change',
65
+ orientationHandler
66
+ ) as unknown as EventSubscription;
67
+
68
+ // Clean up listener. Dimensions.removeEventListener is deprecated as of React Native 0.65 but it is technically
69
+ // available so try to remove via a `EmitterSubscription` first before falling back to `removeEventListener`
70
+ return () => {
71
+ if (typeof subscription?.remove === 'function') {
72
+ subscription?.remove();
73
+ } else {
74
+ Dimensions.removeEventListener('change', orientationHandler);
75
+ }
76
+ };
77
+ }, [updateOrientation]);
78
+
79
+ const carouselRenderItem = (renderInfo: ListRenderItemInfo<T>) => (
80
+ <View style={{ width }}>{renderItem(renderInfo)}</View>
81
+ );
82
+
83
+ if (!data?.length) {
84
+ return null;
85
+ }
86
+
87
+ return (
88
+ <>
89
+ <FlatList
90
+ bounces={false}
91
+ data={data}
92
+ decelerationRate="fast"
93
+ disableIntervalMomentum
94
+ horizontal
95
+ onViewableItemsChanged={onViewableItemsChanged.current}
96
+ ref={flatListRef}
97
+ renderItem={carouselRenderItem}
98
+ renderToHardwareTextureAndroid
99
+ showsHorizontalScrollIndicator={false}
100
+ showsVerticalScrollIndicator={false}
101
+ snapToAlignment="start"
102
+ snapToInterval={width}
103
+ style={style}
104
+ viewabilityConfig={VIEWABILITY_CONFIG}
105
+ />
106
+ <CarouselPageIndicator
107
+ activeStyle={indicatorActiveStyle}
108
+ currentIndex={currentIndex}
109
+ inactiveStyle={indicatorInactiveStyle}
110
+ numberOfItems={data.length}
111
+ style={styles.indicator}
112
+ />
113
+ </>
114
+ );
115
+ }
@@ -0,0 +1,39 @@
1
+ import React, { useMemo } from 'react';
2
+ import { View } from 'react-native';
3
+ import { SafeAreaView } from 'react-native-safe-area-context';
4
+
5
+ import { CarouselPageIndicatorProps } from './types';
6
+ import {
7
+ DEFAULT_CAROUSEL_INDICATOR_ACTIVE_STYLE,
8
+ DEFAULT_CAROUSEL_INDICATOR_INACTIVE_STYLE,
9
+ } from './constants';
10
+
11
+ export default function CarouselPageIndicator({
12
+ activeStyle,
13
+ currentIndex,
14
+ inactiveStyle,
15
+ numberOfItems,
16
+ style,
17
+ }: CarouselPageIndicatorProps): JSX.Element {
18
+ const items = useMemo(
19
+ () =>
20
+ new Array(numberOfItems ?? 0)
21
+ .fill(null)
22
+ .map((_, index) =>
23
+ (currentIndex ?? 0) === index ? (
24
+ <View
25
+ style={[DEFAULT_CAROUSEL_INDICATOR_ACTIVE_STYLE, activeStyle]}
26
+ key={`indicator-item-${index}`}
27
+ />
28
+ ) : (
29
+ <View
30
+ style={[DEFAULT_CAROUSEL_INDICATOR_INACTIVE_STYLE, inactiveStyle]}
31
+ key={`indicator-item-${index}`}
32
+ />
33
+ )
34
+ ),
35
+ [activeStyle, currentIndex, inactiveStyle, numberOfItems]
36
+ );
37
+
38
+ return <SafeAreaView style={style}>{items}</SafeAreaView>;
39
+ }
@@ -0,0 +1,185 @@
1
+ import React from 'react';
2
+ import {
3
+ Dimensions,
4
+ EmitterSubscription,
5
+ FlatList,
6
+ ListRenderItem,
7
+ Text,
8
+ ViewToken,
9
+ } from 'react-native';
10
+ import { act, create, ReactTestRenderer } from 'react-test-renderer';
11
+
12
+ import Carousel from '../Carousel';
13
+ import CarouselPageIndicator from '../CarouselPageIndicator';
14
+
15
+ jest.mock('../CarouselPageIndicator', () => 'CarouselPageIndicator');
16
+
17
+ type ItemProps = { str?: string };
18
+
19
+ const Item = ({ str }: ItemProps) => <Text>{str}</Text>;
20
+ const renderItem: ListRenderItem<ItemProps> = ({ item }) => (
21
+ <Item str={item.str} />
22
+ );
23
+
24
+ describe('Carousel', () => {
25
+ let carousel: ReactTestRenderer;
26
+
27
+ beforeEach(() => {
28
+ jest.clearAllMocks();
29
+ });
30
+
31
+ /* eslint-disable no-console */
32
+ // turn off console errors during tests, can be safely removed once React Native v0.64 is no longer supported
33
+ const consoleWarn = console.warn;
34
+ console.warn = jest.fn();
35
+ afterAll(() => {
36
+ console.warn = consoleWarn;
37
+ /* eslint-enable no-console */
38
+ });
39
+
40
+ it('renders with multiple items in the data', () => {
41
+ const data = [
42
+ { key: 1, str: 'foo' },
43
+ { key: 2, str: 'bar' },
44
+ ];
45
+ act(() => {
46
+ carousel = create(<Carousel data={data} renderItem={renderItem} />);
47
+ });
48
+ const instance = carousel.root;
49
+
50
+ expect(carousel.toJSON()).toMatchSnapshot();
51
+ expect(instance.findAllByType(Text)).toHaveLength(2);
52
+ expect(instance.findByType(CarouselPageIndicator).props.numberOfItems).toBe(
53
+ 2
54
+ );
55
+ });
56
+
57
+ it('renders with just one item in the data', () => {
58
+ const data = [{ key: 1, str: 'qux' }];
59
+ act(() => {
60
+ carousel = create(<Carousel data={data} renderItem={renderItem} />);
61
+ });
62
+ const instance = carousel.root;
63
+
64
+ expect(carousel.toJSON()).toMatchSnapshot();
65
+ expect(instance.findAllByType(Text)).toHaveLength(1);
66
+ expect(instance.findByType(CarouselPageIndicator).props.numberOfItems).toBe(
67
+ 1
68
+ );
69
+ });
70
+
71
+ it('returns null if data is null', () => {
72
+ // Ideally, this should not happen but, if it does, we should be able to handle gracefully
73
+ act(() => {
74
+ carousel = create(
75
+ <Carousel data={null as any} renderItem={renderItem} />
76
+ );
77
+ });
78
+
79
+ expect(carousel.toJSON()).toBeNull();
80
+ });
81
+
82
+ it('returns null if there are no items in the data', () => {
83
+ // Ideally, this should not happen but, if it does, we should be able to handle gracefully
84
+ act(() => {
85
+ carousel = create(<Carousel data={[]} renderItem={renderItem} />);
86
+ });
87
+
88
+ expect(carousel.toJSON()).toBeNull();
89
+ });
90
+
91
+ it('calls the orientation handler on orientation change', () => {
92
+ let orientationHandler: Function;
93
+ const data = [{ key: 1, str: 'qux' }];
94
+ const window = { width: 350 };
95
+ const addEventListenerSpy = jest.spyOn(Dimensions, 'addEventListener');
96
+ // Get a reference to the orientation handler by spying on the `addEventListener` call
97
+ addEventListenerSpy.mockImplementation(((_, handler) => {
98
+ orientationHandler = handler;
99
+ }) as Dimensions['addEventListener']);
100
+ act(() => {
101
+ carousel = create(<Carousel data={data} renderItem={renderItem} />);
102
+ });
103
+ const instance = carousel.root;
104
+
105
+ expect(
106
+ instance.findByType(Text).parent!.parent!.props.style
107
+ ).not.toStrictEqual(window);
108
+
109
+ // Call the orientation handler with an arbitrary `ScaledSize`
110
+ act(() => {
111
+ orientationHandler({ window });
112
+ });
113
+
114
+ expect(instance.findByType(Text).parent!.parent!.props.style).toStrictEqual(
115
+ window
116
+ );
117
+ });
118
+
119
+ it('cleans up the orientation change listener (React Native < v0.65)', () => {
120
+ // ensure that `addEventListener` returns `undefined`
121
+ jest
122
+ .spyOn(Dimensions, 'addEventListener')
123
+ .mockReturnValue(undefined as unknown as EmitterSubscription);
124
+
125
+ const removeEventListener = jest.spyOn(Dimensions, 'removeEventListener');
126
+
127
+ act(() => {
128
+ carousel = create(<Carousel data={[]} renderItem={renderItem} />);
129
+ });
130
+ act(() => {
131
+ carousel.unmount();
132
+ });
133
+
134
+ expect(removeEventListener).toBeCalled();
135
+ });
136
+
137
+ it('cleans up the orientation change listener (React Native v0.65+)', () => {
138
+ const mockSubscription = { remove: jest.fn() };
139
+ const addEventListenerSpy = jest.spyOn(Dimensions, 'addEventListener');
140
+ (addEventListenerSpy as jest.Mock).mockReturnValue(mockSubscription);
141
+ act(() => {
142
+ carousel = create(<Carousel data={[]} renderItem={renderItem} />);
143
+ });
144
+ act(() => {
145
+ carousel.unmount();
146
+ });
147
+ expect(mockSubscription.remove).toBeCalled();
148
+ });
149
+
150
+ it('sets the index when viewable items change', () => {
151
+ const data = [
152
+ { key: 1, str: 'foo' },
153
+ { key: 2, str: 'bar' },
154
+ ];
155
+ act(() => {
156
+ carousel = create(<Carousel data={data} renderItem={renderItem} />);
157
+ });
158
+ const instance = carousel.root;
159
+ const flatList = instance.findByType(FlatList);
160
+ const { onViewableItemsChanged } = flatList.props as FlatList['props'];
161
+
162
+ expect(instance.findByType(CarouselPageIndicator).props.currentIndex).toBe(
163
+ 0
164
+ );
165
+
166
+ act(() => {
167
+ const info = { viewableItems: [{ index: 1 } as ViewToken], changed: [] };
168
+ onViewableItemsChanged!(info);
169
+ });
170
+
171
+ expect(instance.findByType(CarouselPageIndicator).props.currentIndex).toBe(
172
+ 1
173
+ );
174
+
175
+ // should not update the index if `viewableItems` is empty
176
+ act(() => {
177
+ const info = { viewableItems: [], changed: [] };
178
+ onViewableItemsChanged!(info);
179
+ });
180
+
181
+ expect(instance.findByType(CarouselPageIndicator).props.currentIndex).toBe(
182
+ 1
183
+ );
184
+ });
185
+ });
@@ -0,0 +1,71 @@
1
+ import React from 'react';
2
+ import { View } from 'react-native';
3
+ import { act, create, ReactTestRenderer } from 'react-test-renderer';
4
+
5
+ import CarouselPageIndicator from '../CarouselPageIndicator';
6
+
7
+ describe('CarouselPageIndicator', () => {
8
+ let carouselPageIndicator: ReactTestRenderer;
9
+
10
+ it('renders with multiple items', () => {
11
+ act(() => {
12
+ carouselPageIndicator = create(
13
+ <CarouselPageIndicator currentIndex={0} numberOfItems={3} />
14
+ );
15
+ });
16
+ const instance = carouselPageIndicator.root;
17
+
18
+ expect(carouselPageIndicator.toJSON()).toMatchSnapshot();
19
+ expect(instance.findAllByType(View)).toHaveLength(3);
20
+ });
21
+
22
+ it('renders with just one item', () => {
23
+ act(() => {
24
+ carouselPageIndicator = create(
25
+ <CarouselPageIndicator currentIndex={0} numberOfItems={1} />
26
+ );
27
+ });
28
+ const instance = carouselPageIndicator.root;
29
+
30
+ expect(carouselPageIndicator.toJSON()).toMatchSnapshot();
31
+ expect(instance.findAllByType(View)).toHaveLength(1);
32
+ });
33
+
34
+ it('handles null numberOfItems value', () => {
35
+ // Ideally, this should not happen but, if it does, we should be able to handle gracefully
36
+ act(() => {
37
+ carouselPageIndicator = create(
38
+ <CarouselPageIndicator currentIndex={0} numberOfItems={null as any} />
39
+ );
40
+ });
41
+
42
+ expect(carouselPageIndicator.toJSON()).toMatchSnapshot();
43
+ });
44
+
45
+ it('renders indicator styles based on current index', () => {
46
+ act(() => {
47
+ carouselPageIndicator = create(
48
+ <CarouselPageIndicator currentIndex={1} numberOfItems={3} />
49
+ );
50
+ });
51
+ const instance = carouselPageIndicator.root;
52
+ const indicators = instance.findAllByType(View);
53
+ const activeIndicator = indicators[1];
54
+
55
+ expect(carouselPageIndicator.toJSON()).toMatchSnapshot();
56
+ expect(indicators[0].props).toStrictEqual(indicators[2].props);
57
+ expect(indicators[0].props).not.toStrictEqual(activeIndicator.props);
58
+ expect(indicators[2].props).not.toStrictEqual(activeIndicator.props);
59
+ });
60
+
61
+ it('handles null index value', () => {
62
+ // Ideally, this should not happen but, if it does, we should be able to handle gracefully
63
+ act(() => {
64
+ carouselPageIndicator = create(
65
+ <CarouselPageIndicator currentIndex={null} numberOfItems={5} />
66
+ );
67
+ });
68
+
69
+ expect(carouselPageIndicator.toJSON()).toMatchSnapshot();
70
+ });
71
+ });
@@ -0,0 +1,216 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`Carousel renders with just one item in the data 1`] = `
4
+ Array [
5
+ <RCTScrollView
6
+ bounces={false}
7
+ data={
8
+ Array [
9
+ Object {
10
+ "key": 1,
11
+ "str": "qux",
12
+ },
13
+ ]
14
+ }
15
+ decelerationRate="fast"
16
+ disableIntervalMomentum={true}
17
+ getItem={[Function]}
18
+ getItemCount={[Function]}
19
+ horizontal={true}
20
+ keyExtractor={[Function]}
21
+ onContentSizeChange={[Function]}
22
+ onLayout={[Function]}
23
+ onMomentumScrollBegin={[Function]}
24
+ onMomentumScrollEnd={[Function]}
25
+ onScroll={[Function]}
26
+ onScrollBeginDrag={[Function]}
27
+ onScrollEndDrag={[Function]}
28
+ onViewableItemsChanged={[Function]}
29
+ removeClippedSubviews={false}
30
+ renderItem={[Function]}
31
+ renderToHardwareTextureAndroid={true}
32
+ scrollEventThrottle={50}
33
+ showsHorizontalScrollIndicator={false}
34
+ showsVerticalScrollIndicator={false}
35
+ snapToAlignment="start"
36
+ snapToInterval={750}
37
+ stickyHeaderIndices={Array []}
38
+ viewabilityConfig={
39
+ Object {
40
+ "itemVisiblePercentThreshold": 60,
41
+ }
42
+ }
43
+ viewabilityConfigCallbackPairs={
44
+ Array [
45
+ Object {
46
+ "onViewableItemsChanged": [Function],
47
+ "viewabilityConfig": Object {
48
+ "itemVisiblePercentThreshold": 60,
49
+ },
50
+ },
51
+ ]
52
+ }
53
+ >
54
+ <View>
55
+ <View
56
+ onLayout={[Function]}
57
+ style={
58
+ Array [
59
+ Object {
60
+ "flexDirection": "row",
61
+ },
62
+ null,
63
+ ]
64
+ }
65
+ >
66
+ <View
67
+ style={
68
+ Object {
69
+ "width": 750,
70
+ }
71
+ }
72
+ >
73
+ <Text>
74
+ qux
75
+ </Text>
76
+ </View>
77
+ </View>
78
+ </View>
79
+ </RCTScrollView>,
80
+ <CarouselPageIndicator
81
+ currentIndex={0}
82
+ numberOfItems={1}
83
+ style={
84
+ Object {
85
+ "bottom": 0,
86
+ "flexDirection": "row",
87
+ "justifyContent": "center",
88
+ "left": 0,
89
+ "position": "absolute",
90
+ "right": 0,
91
+ }
92
+ }
93
+ />,
94
+ ]
95
+ `;
96
+
97
+ exports[`Carousel renders with multiple items in the data 1`] = `
98
+ Array [
99
+ <RCTScrollView
100
+ bounces={false}
101
+ data={
102
+ Array [
103
+ Object {
104
+ "key": 1,
105
+ "str": "foo",
106
+ },
107
+ Object {
108
+ "key": 2,
109
+ "str": "bar",
110
+ },
111
+ ]
112
+ }
113
+ decelerationRate="fast"
114
+ disableIntervalMomentum={true}
115
+ getItem={[Function]}
116
+ getItemCount={[Function]}
117
+ horizontal={true}
118
+ keyExtractor={[Function]}
119
+ onContentSizeChange={[Function]}
120
+ onLayout={[Function]}
121
+ onMomentumScrollBegin={[Function]}
122
+ onMomentumScrollEnd={[Function]}
123
+ onScroll={[Function]}
124
+ onScrollBeginDrag={[Function]}
125
+ onScrollEndDrag={[Function]}
126
+ onViewableItemsChanged={[Function]}
127
+ removeClippedSubviews={false}
128
+ renderItem={[Function]}
129
+ renderToHardwareTextureAndroid={true}
130
+ scrollEventThrottle={50}
131
+ showsHorizontalScrollIndicator={false}
132
+ showsVerticalScrollIndicator={false}
133
+ snapToAlignment="start"
134
+ snapToInterval={750}
135
+ stickyHeaderIndices={Array []}
136
+ viewabilityConfig={
137
+ Object {
138
+ "itemVisiblePercentThreshold": 60,
139
+ }
140
+ }
141
+ viewabilityConfigCallbackPairs={
142
+ Array [
143
+ Object {
144
+ "onViewableItemsChanged": [Function],
145
+ "viewabilityConfig": Object {
146
+ "itemVisiblePercentThreshold": 60,
147
+ },
148
+ },
149
+ ]
150
+ }
151
+ >
152
+ <View>
153
+ <View
154
+ onLayout={[Function]}
155
+ style={
156
+ Array [
157
+ Object {
158
+ "flexDirection": "row",
159
+ },
160
+ null,
161
+ ]
162
+ }
163
+ >
164
+ <View
165
+ style={
166
+ Object {
167
+ "width": 750,
168
+ }
169
+ }
170
+ >
171
+ <Text>
172
+ foo
173
+ </Text>
174
+ </View>
175
+ </View>
176
+ <View
177
+ onLayout={[Function]}
178
+ style={
179
+ Array [
180
+ Object {
181
+ "flexDirection": "row",
182
+ },
183
+ null,
184
+ ]
185
+ }
186
+ >
187
+ <View
188
+ style={
189
+ Object {
190
+ "width": 750,
191
+ }
192
+ }
193
+ >
194
+ <Text>
195
+ bar
196
+ </Text>
197
+ </View>
198
+ </View>
199
+ </View>
200
+ </RCTScrollView>,
201
+ <CarouselPageIndicator
202
+ currentIndex={0}
203
+ numberOfItems={2}
204
+ style={
205
+ Object {
206
+ "bottom": 0,
207
+ "flexDirection": "row",
208
+ "justifyContent": "center",
209
+ "left": 0,
210
+ "position": "absolute",
211
+ "right": 0,
212
+ }
213
+ }
214
+ />,
215
+ ]
216
+ `;