Skip to content

Commit a653308

Browse files
committed
fix: TS errors
1 parent 28198bf commit a653308

4 files changed

Lines changed: 35 additions & 25 deletions

File tree

src/animated.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ import {
1818

1919
import type { KeyboardAnimationContext } from "./context";
2020
import type {
21-
FocusedInputHandler,
2221
FocusedInputLayoutChangedEvent,
2322
KeyboardControllerProps,
24-
KeyboardHandler,
2523
NativeEvent,
2624
} from "./types";
2725
import type { ViewStyle } from "react-native";
@@ -129,10 +127,8 @@ export const KeyboardProvider = (props: KeyboardProviderProps) => {
129127
const progressSV = useSharedValue(0);
130128
const heightSV = useSharedValue(0);
131129
const layout = useSharedValue<FocusedInputLayoutChangedEvent | null>(null);
132-
const setKeyboardHandlers =
133-
useEventHandlerRegistration<KeyboardHandler>(viewTagRef);
134-
const setInputHandlers =
135-
useEventHandlerRegistration<FocusedInputHandler>(viewTagRef);
130+
const setKeyboardHandlers = useEventHandlerRegistration(viewTagRef);
131+
const setInputHandlers = useEventHandlerRegistration(viewTagRef);
136132
// memo
137133
const context = useMemo<KeyboardAnimationContext>(
138134
() => ({

src/context.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { createContext, useContext } from "react";
22
import { Animated } from "react-native";
33

4-
import type {
5-
FocusedInputHandler,
6-
FocusedInputLayoutChangedEvent,
7-
KeyboardHandler,
8-
} from "./types";
4+
import type { FocusedInputLayoutChangedEvent } from "./types";
95
import type React from "react";
10-
import type { SharedValue } from "react-native-reanimated";
6+
import type {
7+
EventHandlerProcessed,
8+
SharedValue,
9+
} from "react-native-reanimated";
1110

1211
export type AnimatedContext = {
1312
/**
@@ -37,9 +36,13 @@ export type KeyboardAnimationContext = {
3736
/** Layout of the focused `TextInput` represented as `SharedValue`. */
3837
layout: SharedValue<FocusedInputLayoutChangedEvent | null>;
3938
/** Method for setting workletized keyboard handlers. */
40-
setKeyboardHandlers: (handlers: KeyboardHandler) => () => void;
39+
setKeyboardHandlers: (
40+
handlers: EventHandlerProcessed<never, never>,
41+
) => () => void;
4142
/** Method for setting workletized handlers for tracking focused input events. */
42-
setInputHandlers: (handlers: FocusedInputHandler) => () => void;
43+
setInputHandlers: (
44+
handlers: EventHandlerProcessed<never, never>,
45+
) => () => void;
4346
/** Method to enable/disable KeyboardController library. */
4447
setEnabled: React.Dispatch<React.SetStateAction<boolean>>;
4548
};

src/hooks/index.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ import { useKeyboardContext } from "../context";
66
import { KeyboardController } from "../module";
77

88
import type { AnimatedContext, ReanimatedContext } from "../context";
9-
import type { FocusedInputHandler, KeyboardHandler } from "../types";
9+
import type {
10+
FocusedInputHandler,
11+
FocusedInputSelectionChangedEvent,
12+
FocusedInputTextChangedEvent,
13+
KeyboardHandler,
14+
NativeEvent,
15+
} from "../types";
1016
import type { DependencyList } from "react";
1117

1218
/**
@@ -112,7 +118,7 @@ export function useGenericKeyboardHandler(
112118
) {
113119
const context = useKeyboardContext();
114120

115-
const eventHandler = useEvent(
121+
const eventHandler = useEvent<NativeEvent>(
116122
(event) => {
117123
"worklet";
118124

@@ -257,16 +263,18 @@ export function useFocusedInputHandler(
257263
) {
258264
const context = useKeyboardContext();
259265

260-
const eventHandler = useEvent(
266+
const eventHandler = useEvent<
267+
FocusedInputSelectionChangedEvent | FocusedInputTextChangedEvent
268+
>(
261269
(event) => {
262270
"worklet";
263271

264272
if (event.eventName.endsWith("onFocusedInputTextChanged")) {
265-
handler.onChangeText?.(event);
273+
handler.onChangeText?.(event as FocusedInputTextChangedEvent);
266274
}
267275

268276
if (event.eventName.endsWith("onFocusedInputSelectionChanged")) {
269-
handler.onSelectionChange?.(event);
277+
handler.onSelectionChange?.(event as FocusedInputSelectionChangedEvent);
270278
}
271279
},
272280
["onFocusedInputTextChanged", "onFocusedInputSelectionChanged"],

src/internal.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { Animated } from "react-native";
33

44
import { findNodeHandle } from "./utils/findNodeHandle";
55

6-
type EventHandler = (event: never) => void;
6+
import type { EventHandlerProcessed } from "react-native-reanimated";
7+
78
type ComponentOrHandle = Parameters<typeof findNodeHandle>[0];
89

910
/**
1011
* An internal hook that helps to register workletized event handlers.
1112
*
12-
* @param map - Map of event handlers and their names.
1313
* @param viewTagRef - Ref to the view that produces events.
1414
* @returns A function that registers supplied event handlers.
1515
* @example
@@ -20,10 +20,10 @@ type ComponentOrHandle = Parameters<typeof findNodeHandle>[0];
2020
* );
2121
* ```
2222
*/
23-
export function useEventHandlerRegistration<
24-
H extends Partial<Record<string, EventHandler>>,
25-
>(viewTagRef: React.MutableRefObject<ComponentOrHandle>) {
26-
const onRegisterHandler = (handler: H) => {
23+
export function useEventHandlerRegistration(
24+
viewTagRef: React.MutableRefObject<ComponentOrHandle>,
25+
) {
26+
const onRegisterHandler = (handler: EventHandlerProcessed<never, never>) => {
2727
const attachWorkletHandlers = () => {
2828
const viewTag = findNodeHandle(viewTagRef.current);
2929

@@ -34,6 +34,7 @@ export function useEventHandlerRegistration<
3434
}
3535

3636
// TODO: must be a compat layer? Property `workletEventHandler` from `ref` may be missing. Should be on useEvent layer?
37+
// @ts-expect-error this handler is not exposed publicly
3738
handler.workletEventHandler.registerForEvents(viewTag);
3839
};
3940

@@ -47,6 +48,8 @@ export function useEventHandlerRegistration<
4748
return () => {
4849
const viewTag = findNodeHandle(viewTagRef.current);
4950

51+
// TODO: must be a compat layer? Property `workletEventHandler` from `ref` may be missing. Should be on useEvent layer?
52+
// @ts-expect-error this handler is not exposed publicly
5053
handler.workletEventHandler.unregisterFromEvents(viewTag);
5154
};
5255
};

0 commit comments

Comments
 (0)