Skip to content

Commit 5f7ec9c

Browse files
committed
Add ref types
1 parent 110d333 commit 5f7ec9c

4 files changed

Lines changed: 46 additions & 27 deletions

File tree

apps/common-app/src/release_tests/combo/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111

1212
import {
1313
NativeViewGestureHandler,
14-
ScrollView as GHScroll,
14+
LegacyScrollView as GHScroll,
1515
State,
1616
TapGestureHandler,
1717
TextInput,

packages/react-native-gesture-handler/src/v3/components/GestureComponents.tsx

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
FlatList as RNFlatList,
1717
FlatListProps as RNFlatListProps,
1818
RefreshControl as RNRefreshControl,
19+
RefreshControlProps as RNRefreshControlProps,
1920
} from 'react-native';
2021

2122
import createNativeWrapper, {
@@ -27,6 +28,11 @@ import { NativeWrapperProps } from '../hooks/utils';
2728
import { DetectorType } from '../detectors';
2829
import { NativeGesture } from '../hooks/gestures/native/useNativeGesture';
2930

31+
export type ImperativeRefreshControlRef = ComponentWrapperRef<
32+
RNRefreshControlProps,
33+
RNRefreshControl
34+
> | null;
35+
3036
export const RefreshControl = createNativeWrapper(
3137
RNRefreshControl,
3238
{
@@ -37,7 +43,12 @@ export const RefreshControl = createNativeWrapper(
3743
);
3844

3945
// eslint-disable-next-line @typescript-eslint/no-redeclare
40-
export type RefreshControl = typeof RefreshControl & RNRefreshControl;
46+
export type RefreshControl = RNRefreshControlProps;
47+
48+
export type ImperativeScrollViewRef = ComponentWrapperRef<
49+
RNScrollViewProps,
50+
RNScrollView
51+
> | null;
4152

4253
const GHScrollView = createNativeWrapper<PropsWithChildren<RNScrollViewProps>>(
4354
RNScrollView,
@@ -48,9 +59,6 @@ const GHScrollView = createNativeWrapper<PropsWithChildren<RNScrollViewProps>>(
4859
DetectorType.Intercepting
4960
);
5061

51-
export type ImperativeScrollViewRef =
52-
ComponentWrapperRef<RNScrollViewProps> | null;
53-
5462
export const ScrollView = (
5563
props: RNScrollViewProps &
5664
NativeWrapperProperties & {
@@ -65,7 +73,7 @@ export const ScrollView = (
6573
null
6674
);
6775

68-
const wrapperRef = useRef<ComponentWrapperRef<RNScrollViewProps>>(null);
76+
const wrapperRef = useRef<ImperativeScrollViewRef>(null);
6977

7078
useImperativeHandle<ImperativeScrollViewRef, ImperativeScrollViewRef>(
7179
ref,
@@ -94,29 +102,40 @@ export const ScrollView = (
94102
/>
95103
);
96104
};
97-
// Backward type compatibility with https://github.com/software-mansion/react-native-gesture-handler/blob/db78d3ca7d48e8ba57482d3fe9b0a15aa79d9932/react-native-gesture-handler.d.ts#L440-L457
98-
// include methods of wrapped components by creating an intersection type with the RN component instead of duplicating them.
105+
99106
// eslint-disable-next-line @typescript-eslint/no-redeclare
100-
export type ScrollView = typeof GHScrollView & RNScrollView;
107+
export type ScrollView = typeof ScrollView;
108+
109+
export type ImperativeSwitchRef = ComponentWrapperRef<
110+
RNSwitchProps,
111+
RNSwitch
112+
> | null;
101113

102114
export const Switch = createNativeWrapper<RNSwitchProps>(RNSwitch, {
103115
shouldCancelWhenOutside: false,
104116
shouldActivateOnStart: true,
105117
disallowInterruption: true,
106118
});
119+
107120
// eslint-disable-next-line @typescript-eslint/no-redeclare
108-
export type Switch = typeof Switch & RNSwitch;
121+
export type Switch = typeof Switch;
122+
123+
export type ImperativeTextInputRef = ComponentWrapperRef<
124+
RNTextInputProps,
125+
RNTextInput
126+
> | null;
109127

110128
export const TextInput = createNativeWrapper<RNTextInputProps>(RNTextInput);
111-
// eslint-disable-next-line @typescript-eslint/no-redeclare
112-
export type TextInput = typeof TextInput & RNTextInput;
113129

114130
export type ImperativeFlatListRef<T = any> =
115-
| (ComponentWrapperRef<RNScrollViewProps> & {
116-
flatListRef: FlatList<T> | null;
131+
| (ComponentWrapperRef<RNScrollViewProps, RNScrollView> & {
132+
flatListRef: RNFlatList<T> | null;
117133
})
118134
| null;
119135

136+
// eslint-disable-next-line @typescript-eslint/no-redeclare
137+
export type TextInput = typeof TextInput;
138+
120139
export const FlatList = ((props) => {
121140
const { refreshControl, ref, ...rest } = props;
122141

@@ -125,7 +144,7 @@ export const FlatList = ((props) => {
125144
);
126145

127146
const wrapperRef = useRef<ImperativeScrollViewRef>(null);
128-
const flatListRef = useRef<FlatList<any>>(null);
147+
const flatListRef = useRef<RNFlatList<any>>(null);
129148

130149
const flatListProps = {};
131150
const scrollViewProps = {};
@@ -191,5 +210,6 @@ export const FlatList = ((props) => {
191210
}
192211
>
193212
) => ReactElement | null;
213+
194214
// eslint-disable-next-line @typescript-eslint/no-redeclare
195-
export type FlatList<ItemT = any> = typeof FlatList & RNFlatList<ItemT>;
215+
export type FlatList = typeof FlatList;

packages/react-native-gesture-handler/src/v3/components/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ export {
2222
} from './GestureComponents';
2323

2424
export type {
25-
ImperativeScrollViewRef as GHScrollViewRef,
2625
ImperativeFlatListRef as GHFlatListRef,
26+
ImperativeRefreshControlRef as GHRefreshControlRef,
27+
ImperativeScrollViewRef as GHScrollViewRef,
28+
ImperativeSwitchRef as GHSwitchRef,
29+
ImperativeTextInputRef as GHTextInputRef,
2730
} from './GestureComponents';

packages/react-native-gesture-handler/src/v3/createNativeWrapper.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { NativeGesture } from './hooks/gestures/native/useNativeGesture';
88
import { DetectorType, InterceptingGestureDetector } from './detectors';
99
import { VirtualDetector } from './detectors/VirtualDetector/VirtualDetector';
1010

11-
export type ComponentWrapperRef<P> = {
12-
componentRef?: React.ComponentType<P>;
11+
export type ComponentWrapperRef<P, T> = {
12+
componentRef?: React.ComponentType<P> & T;
1313
gestureRef?: NativeGesture;
1414
};
1515

@@ -52,14 +52,10 @@ export default function createNativeWrapper<P>(
5252
const componentRef = useRef<React.ComponentType<P>>(null);
5353
const gestureRef = useRef<NativeGesture>(native);
5454

55-
useImperativeHandle(
56-
props.ref,
57-
() =>
58-
({
59-
componentRef: componentRef.current,
60-
gestureRef: gestureRef.current,
61-
}) as ComponentWrapperRef<P>
62-
);
55+
useImperativeHandle(props.ref, () => ({
56+
componentRef: componentRef.current,
57+
gestureRef: gestureRef.current,
58+
}));
6359

6460
const DetectorComponent =
6561
detectorType === DetectorType.Intercepting

0 commit comments

Comments
 (0)