Skip to content

Commit 2c6129e

Browse files
committed
Use PublicScrollViewInstance for FlatList getNativeScrollRef return value
1 parent 805bc2b commit 2c6129e

3 files changed

Lines changed: 62 additions & 46 deletions

File tree

packages/react-native/Libraries/Components/ScrollView/ScrollView.d.ts

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -827,10 +827,29 @@ export interface ScrollViewProps
827827
StickyHeaderComponent?: React.ComponentType<any> | undefined;
828828
}
829829

830-
declare class ScrollViewComponent extends React.Component<ScrollViewProps> {}
831-
export declare const ScrollViewBase: Constructor<ScrollResponderMixin> &
832-
typeof ScrollViewComponent;
833-
export class ScrollView extends ScrollViewBase {
830+
export interface ScrollViewScrollToOptions {
831+
x?: number;
832+
y?: number;
833+
animated?: boolean;
834+
}
835+
836+
// Public methods for ScrollView
837+
export interface ScrollViewImperativeMethods {
838+
/**
839+
* Returns a reference to the underlying scroll responder, which supports
840+
* operations like `scrollTo`. All ScrollView-like components should
841+
* implement this method so that they can be composed while providing access
842+
* to the underlying scroll responder's methods.
843+
*/
844+
readonly getScrollResponder: () => ScrollResponderType;
845+
readonly getScrollableNode: () => number | undefined;
846+
readonly getInnerViewNode: () => number | undefined;
847+
readonly getInnerViewRef: () => React.ComponentRef<typeof View> | null;
848+
/**
849+
* Returns a reference to the underlying native scroll view, or null if the
850+
* native instance is not mounted.
851+
*/
852+
readonly getNativeScrollRef: () => HostInstance | null;
834853
/**
835854
* Scrolls to a given x, y offset, either immediately or with a smooth animation.
836855
* Syntax:
@@ -841,18 +860,11 @@ export class ScrollView extends ScrollViewBase {
841860
* the function also accepts separate arguments as an alternative to the options object.
842861
* This is deprecated due to ambiguity (y before x), and SHOULD NOT BE USED.
843862
*/
844-
scrollTo(
845-
y?:
846-
| number
847-
| {
848-
x?: number | undefined;
849-
y?: number | undefined;
850-
animated?: boolean | undefined;
851-
},
863+
readonly scrollTo: (
864+
options?: ScrollViewScrollToOptions | number,
852865
deprecatedX?: number,
853866
deprecatedAnimated?: boolean,
854-
): void;
855-
867+
) => void;
856868
/**
857869
* A helper function that scrolls to the end of the scrollview;
858870
* If this is a vertical ScrollView, it scrolls to the bottom.
@@ -861,32 +873,39 @@ export class ScrollView extends ScrollViewBase {
861873
* The options object has an animated prop, that enables the scrolling animation or not.
862874
* The animated prop defaults to true
863875
*/
864-
scrollToEnd(options?: {animated?: boolean | undefined}): void;
865-
876+
readonly scrollToEnd: (options?: ScrollViewScrollToOptions | null) => void;
866877
/**
867878
* Displays the scroll indicators momentarily.
868879
*/
869-
flashScrollIndicators(): void;
870-
871-
/**
872-
* Returns a reference to the underlying scroll responder, which supports
873-
* operations like `scrollTo`. All ScrollView-like components should
874-
* implement this method so that they can be composed while providing access
875-
* to the underlying scroll responder's methods.
876-
*/
877-
getScrollResponder(): ScrollResponderMixin;
878-
879-
getScrollableNode(): any;
880+
readonly flashScrollIndicators: () => void;
881+
readonly scrollResponderZoomTo: (
882+
rect: {
883+
x: number;
884+
y: number;
885+
width: number;
886+
height: number;
887+
animated?: boolean;
888+
},
889+
animated?: boolean, // deprecated, put this inside the rect argument instead
890+
) => void;
891+
readonly scrollResponderScrollNativeHandleToKeyboard: (
892+
nodeHandle: number | HostInstance,
893+
additionalOffset?: number,
894+
preventNegativeScrollOffset?: boolean,
895+
) => void;
896+
}
880897

881-
// Undocumented
882-
getInnerViewNode(): any;
898+
export type ScrollResponderType = ScrollViewImperativeMethods;
883899

884-
/**
885-
* Returns a reference to the underlying native scroll view, or null if the
886-
* native instance is not mounted.
887-
*/
888-
getNativeScrollRef: () => HostInstance | null;
900+
export interface PublicScrollViewInstance
901+
extends HostInstance,
902+
ScrollViewImperativeMethods {}
889903

904+
declare class ScrollViewComponent extends React.Component<ScrollViewProps> {}
905+
export declare const ScrollViewBase: Constructor<ScrollResponderMixin> &
906+
typeof ScrollViewComponent;
907+
export interface ScrollView extends ScrollViewImperativeMethods {}
908+
export class ScrollView extends ScrollViewBase {
890909
/**
891910
* @deprecated Use scrollTo instead
892911
*/

packages/react-native/Libraries/Lists/FlatList.d.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ import type {
1414
VirtualizedListProps,
1515
ViewabilityConfig,
1616
} from '@react-native/virtualized-lists';
17-
import type {ScrollViewComponent} from '../Components/ScrollView/ScrollView';
18-
import {HostInstance} from '../../types/public/ReactNativeTypes';
17+
import type {PublicScrollViewInstance} from '../Components/ScrollView/ScrollView';
1918
import type {StyleProp} from '../StyleSheet/StyleSheet';
2019
import type {ViewStyle} from '../StyleSheet/StyleSheetTypes';
21-
import type {View} from '../Components/View/View';
2220

2321
export interface FlatListProps<ItemT> extends VirtualizedListProps<ItemT> {
2422
/**
@@ -233,7 +231,7 @@ export abstract class FlatListComponent<
233231
* Returns a reference to the underlying native scroll view, or null if the
234232
* native instance is not mounted.
235233
*/
236-
getNativeScrollRef: () => HostInstance | null;
234+
getNativeScrollRef: () => PublicScrollViewInstance | null;
237235

238236
getScrollableNode: () => any;
239237

packages/react-native/Libraries/Lists/FlatList.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
* @format
99
*/
1010

11-
import typeof ScrollViewNativeComponent from '../Components/ScrollView/ScrollViewNativeComponent';
1211
import type {ViewStyleProp} from '../StyleSheet/StyleSheet';
1312
import type {
1413
ListRenderItem,
@@ -19,7 +18,10 @@ import type {
1918
} from '@react-native/virtualized-lists';
2019

2120
import * as ReactNativeFeatureFlags from '../../src/private/featureflags/ReactNativeFeatureFlags';
22-
import {type ScrollResponderType} from '../Components/ScrollView/ScrollView';
21+
import {
22+
type PublicScrollViewInstance,
23+
type ScrollResponderType,
24+
} from '../Components/ScrollView/ScrollView';
2325
import View from '../Components/View/View';
2426
import VirtualizedLists from '@react-native/virtualized-lists';
2527
import memoizeOne from 'memoize-one';
@@ -395,14 +397,11 @@ class FlatList<ItemT = any> extends React.PureComponent<FlatListProps<ItemT>> {
395397
}
396398

397399
/**
398-
* Provides a reference to the underlying host component
400+
* Returns a reference to the underlying native scroll view, or null if the
401+
* native instance is not mounted.
399402
*/
400-
getNativeScrollRef():
401-
| ?React.ElementRef<typeof View>
402-
| ?React.ElementRef<ScrollViewNativeComponent> {
403+
getNativeScrollRef(): ?PublicScrollViewInstance {
403404
if (this._listRef) {
404-
/* $FlowFixMe[incompatible-return] Suppresses errors found when fixing
405-
* TextInput typing */
406405
return this._listRef.getScrollRef();
407406
}
408407
}

0 commit comments

Comments
 (0)