Skip to content

Commit fce6630

Browse files
authored
Add Carousel to extras (#259)
* update name * update useCarousel ref type * export Carousel and useCarousel * Make accessible on iOS * fix type for useCarousel * fix to work a Carousel * remove onLayout * remove unused import * Added changesets
1 parent b30a396 commit fce6630

File tree

5 files changed

+64
-31
lines changed

5 files changed

+64
-31
lines changed

.changeset/breezy-suns-compare.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
'@react-native-ama/internal': minor
3+
'@react-native-ama/extras': minor
4+
'@react-native-ama/forms': minor
5+
'@react-native-ama/animations': minor
6+
'@react-native-ama/core': minor
7+
'@react-native-ama/lists': minor
8+
'@react-native-ama/react-native': minor
9+
---
10+
11+
Added Carousel and useCarousel to extras package
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import * as React from 'react';
2+
import { FlatListProps } from 'react-native';
3+
import { FlatList } from 'react-native-gesture-handler';
4+
5+
import { useCarousel } from '../hooks/useCarousel';
6+
7+
function fixedForwardRef<T, P = {}>(
8+
render: (props: P, ref: React.Ref<T>) => React.ReactNode,
9+
): (props: P & React.RefAttributes<T>) => React.ReactNode {
10+
return React.forwardRef(render) as any;
11+
}
12+
13+
export type CarouselProps<T = any> = Omit<
14+
FlatListProps<T>,
15+
| 'accessibilityLabel'
16+
| 'accessibilityRole'
17+
| 'accessibilityActions'
18+
| 'onAccessibilityAction'
19+
> & {
20+
accessibilityLabel: string;
21+
};
22+
23+
const CarouselInner = <T,>(
24+
props: CarouselProps<T>,
25+
forwardedRef: React.ForwardedRef<FlatList<T>>,
26+
) => {
27+
const a11yProps = useCarousel({
28+
data: props.data,
29+
flatListRef: forwardedRef,
30+
});
31+
32+
return (
33+
<FlatList
34+
ref={forwardedRef}
35+
showsHorizontalScrollIndicator={false}
36+
showsVerticalScrollIndicator={false}
37+
decelerationRate="fast"
38+
horizontal
39+
{...props}
40+
{...a11yProps}
41+
/>
42+
);
43+
};
44+
45+
export const Carousel = fixedForwardRef(CarouselInner);

packages/extras/src/components/CarouselWrapper.tsx

-27
This file was deleted.

packages/extras/src/hooks/useCarousel.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { useRef } from 'react';
2+
// import { FlatList } from 'react-native';
23
import { FlatList } from 'react-native-gesture-handler';
34
import {
45
AccessibilityActionEvent,
56
AccessibilityRole,
67
} from 'react-native/types';
78

8-
type UseCarousel = {
9-
data: ArrayLike<any> | null | undefined;
10-
flatListRef: React.ForwardedRef<FlatList<any> | null>;
9+
type UseCarousel<T> = {
10+
data: ArrayLike<T> | null | undefined;
11+
flatListRef: React.Ref<FlatList<T> | null>;
1112
};
1213

13-
export const useCarousel = ({ data, flatListRef }: UseCarousel) => {
14+
export const useCarousel = <T = any>({ data, flatListRef }: UseCarousel<T>) => {
1415
const carouselIndexForScreenReader = useRef(0);
1516
const totalItems = data?.length || 0;
1617

@@ -29,6 +30,7 @@ export const useCarousel = ({ data, flatListRef }: UseCarousel) => {
2930
};
3031

3132
return {
33+
accessible: true,
3234
accessibilityRole: 'adjustable' as AccessibilityRole,
3335
accessibilityActions,
3436
onAccessibilityAction,

packages/extras/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Components
22
export { BottomSheet } from './components/BottomSheet';
3+
export { Carousel } from './components/Carousel';
34

45
// Hooks
56
export { useBottomSheetGestureHandler } from './hooks/useBottomSheetGestureHandler';
67
export { useKeyboard } from './hooks/useKeyboard';
8+
export { useCarousel } from './hooks/useCarousel';

0 commit comments

Comments
 (0)