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 c5e9ecb commit 3e0f7a9
Show file tree
Hide file tree
Showing 5 changed files with 21 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(rest);
useScrollableProps(rest, ref);
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(rest);
useScrollableProps(rest, ref);
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(rest);
useScrollableProps(rest, ref);
const WW = WrappedComponent as any;
return (
<AutocompleteContext.Provider value={{ scrollableRef, scrollX, scrollY }}>
Expand Down
15 changes: 15 additions & 0 deletions src/mergeRefs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type * as React from 'react';

export function mergeRefs<T = any>(
refs: Array<React.MutableRefObject<T> | React.LegacyRef<T>>
): React.RefCallback<T> {
return (value) => {
refs.forEach((ref) => {
if (typeof ref === 'function') {
ref(value);
} else if (ref != null) {
(ref as React.MutableRefObject<T | null>).current = value;
}
});
};
}
5 changes: 3 additions & 2 deletions src/shared.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
NativeSyntheticEvent,
ScrollViewProps,
} from 'react-native';
import { mergeRefs } from './mergeRefs';

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

export function useScrollableProps(props: ScrollViewProps) {
export function useScrollableProps(props: ScrollViewProps, ref: any) {
const scrollableRef = useAnimatedRef<any>();
const scrollX = useSharedValue(0);
const scrollY = useSharedValue(0);
Expand All @@ -34,7 +35,7 @@ export function useScrollableProps(props: ScrollViewProps) {
props?.onScroll?.(e as any);
});
const scrollableProps: AutocompleteScrollableProps = {
ref: scrollableRef,
ref: mergeRefs([scrollableRef, ref]),
scrollEventThrottle: 16,
keyboardShouldPersistTaps: 'handled',
onScroll: scrollHandler,
Expand Down

0 comments on commit 3e0f7a9

Please sign in to comment.