Skip to content

Commit

Permalink
fix: forward refs
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardLindhout committed Jul 23, 2022
1 parent 982ec51 commit 4212891
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/AutocompleteFlatList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useScrollableProps } from './shared';

function AutocompleteFlatList<T>(rest: FlatListProps<T>, ref: any) {
const { scrollableRef, scrollX, scrollY, scrollableProps } =
useScrollableProps();
useScrollableProps(rest);
const Flat = Animated.FlatList as any;
return (
<AutocompleteContext.Provider value={{ scrollableRef, scrollX, scrollY }}>
Expand Down
2 changes: 1 addition & 1 deletion src/AutocompleteScrollView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useScrollableProps } from './shared';

function AutocompleteScrollView(rest: ScrollViewProps, ref: any) {
const { scrollableRef, scrollX, scrollY, scrollableProps } =
useScrollableProps();
useScrollableProps(rest);
return (
<AutocompleteContext.Provider value={{ scrollableRef, scrollX, scrollY }}>
<Animated.ScrollView ref={ref} {...rest} {...scrollableProps} />
Expand Down
2 changes: 1 addition & 1 deletion src/createAutocompleteScrollable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default function createAutocompleteScrollable<
>(WrappedComponent: T): React.ComponentType<T> {
return React.forwardRef(function (rest, ref) {
const { scrollableRef, scrollX, scrollY, scrollableProps } =
useScrollableProps();
useScrollableProps(rest);
const WW = WrappedComponent as any;
return (
<AutocompleteContext.Provider value={{ scrollableRef, scrollX, scrollY }}>
Expand Down
9 changes: 7 additions & 2 deletions src/shared.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import {
useAnimatedScrollHandler,
useSharedValue,
} from 'react-native-reanimated';
import type { NativeScrollEvent, NativeSyntheticEvent } from 'react-native';
import type {
NativeScrollEvent,
NativeSyntheticEvent,
ScrollViewProps,
} from 'react-native';

export type AutocompleteScrollableProps = {
ref?: any;
Expand All @@ -19,14 +23,15 @@ export type AutocompleteScrollableProps = {
| undefined;
};

export function useScrollableProps() {
export function useScrollableProps(props: ScrollViewProps) {
const scrollableRef = useAnimatedRef<any>();
const scrollX = useSharedValue(0);
const scrollY = useSharedValue(0);
const scrollHandler = useAnimatedScrollHandler((e) => {
const { x, y } = e.contentOffset;
scrollX.value = x;
scrollY.value = y;
props?.onScroll?.(e as any);
});
const scrollableProps: AutocompleteScrollableProps = {
ref: scrollableRef,
Expand Down

0 comments on commit 4212891

Please sign in to comment.