Skip to content

Commit ea01eae

Browse files
committed
Use PublicScrollViewInstance for FlatList getNativeScrollRef return value
1 parent 8f95f63 commit ea01eae

3 files changed

Lines changed: 61 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
@@ -836,10 +836,29 @@ export interface ScrollViewProps
836836
StickyHeaderComponent?: React.ComponentType<any> | undefined;
837837
}
838838

839-
declare class ScrollViewComponent extends React.Component<ScrollViewProps> {}
840-
export declare const ScrollViewBase: Constructor<ScrollResponderMixin> &
841-
typeof ScrollViewComponent;
842-
export class ScrollView extends ScrollViewBase {
839+
export interface ScrollViewScrollToOptions {
840+
x?: number;
841+
y?: number;
842+
animated?: boolean;
843+
}
844+
845+
// Public methods for ScrollView
846+
export interface ScrollViewImperativeMethods {
847+
/**
848+
* Returns a reference to the underlying scroll responder, which supports
849+
* operations like `scrollTo`. All ScrollView-like components should
850+
* implement this method so that they can be composed while providing access
851+
* to the underlying scroll responder's methods.
852+
*/
853+
readonly getScrollResponder: () => ScrollResponderType;
854+
readonly getScrollableNode: () => number | undefined;
855+
readonly getInnerViewNode: () => number | undefined;
856+
readonly getInnerViewRef: () => React.ComponentRef<typeof View> | null;
857+
/**
858+
* Returns a reference to the underlying native scroll view, or null if the
859+
* native instance is not mounted.
860+
*/
861+
readonly getNativeScrollRef: () => HostInstance | null;
843862
/**
844863
* Scrolls to a given x, y offset, either immediately or with a smooth animation.
845864
* Syntax:
@@ -850,18 +869,11 @@ export class ScrollView extends ScrollViewBase {
850869
* the function also accepts separate arguments as an alternative to the options object.
851870
* This is deprecated due to ambiguity (y before x), and SHOULD NOT BE USED.
852871
*/
853-
scrollTo(
854-
y?:
855-
| number
856-
| {
857-
x?: number | undefined;
858-
y?: number | undefined;
859-
animated?: boolean | undefined;
860-
},
872+
readonly scrollTo: (
873+
options?: ScrollViewScrollToOptions | number,
861874
deprecatedX?: number,
862875
deprecatedAnimated?: boolean,
863-
): void;
864-
876+
) => void;
865877
/**
866878
* A helper function that scrolls to the end of the scrollview;
867879
* If this is a vertical ScrollView, it scrolls to the bottom.
@@ -870,32 +882,39 @@ export class ScrollView extends ScrollViewBase {
870882
* The options object has an animated prop, that enables the scrolling animation or not.
871883
* The animated prop defaults to true
872884
*/
873-
scrollToEnd(options?: {animated?: boolean | undefined}): void;
874-
885+
readonly scrollToEnd: (options?: ScrollViewScrollToOptions | null) => void;
875886
/**
876887
* Displays the scroll indicators momentarily.
877888
*/
878-
flashScrollIndicators(): void;
879-
880-
/**
881-
* Returns a reference to the underlying scroll responder, which supports
882-
* operations like `scrollTo`. All ScrollView-like components should
883-
* implement this method so that they can be composed while providing access
884-
* to the underlying scroll responder's methods.
885-
*/
886-
getScrollResponder(): ScrollResponderMixin;
887-
888-
getScrollableNode(): any;
889+
readonly flashScrollIndicators: () => void;
890+
readonly scrollResponderZoomTo: (
891+
rect: {
892+
x: number;
893+
y: number;
894+
width: number;
895+
height: number;
896+
animated?: boolean;
897+
},
898+
animated?: boolean, // deprecated, put this inside the rect argument instead
899+
) => void;
900+
readonly scrollResponderScrollNativeHandleToKeyboard: (
901+
nodeHandle: number | HostInstance,
902+
additionalOffset?: number,
903+
preventNegativeScrollOffset?: boolean,
904+
) => void;
905+
}
889906

890-
// Undocumented
891-
getInnerViewNode(): any;
907+
export type ScrollResponderType = ScrollViewImperativeMethods;
892908

893-
/**
894-
* Returns a reference to the underlying native scroll view, or null if the
895-
* native instance is not mounted.
896-
*/
897-
getNativeScrollRef: () => HostInstance | null;
909+
export interface PublicScrollViewInstance
910+
extends HostInstance,
911+
ScrollViewImperativeMethods {}
898912

913+
declare class ScrollViewComponent extends React.Component<ScrollViewProps> {}
914+
export declare const ScrollViewBase: Constructor<ScrollResponderMixin> &
915+
typeof ScrollViewComponent;
916+
export interface ScrollView extends ScrollViewImperativeMethods {}
917+
export class ScrollView extends ScrollViewBase {
899918
/**
900919
* @deprecated Use scrollTo instead
901920
*/

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: 6 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,10 @@ 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.
399401
*/
400-
getNativeScrollRef():
401-
| ?React.ElementRef<typeof View>
402-
| ?React.ElementRef<ScrollViewNativeComponent> {
402+
getNativeScrollRef(): ?PublicScrollViewInstance {
403403
if (this._listRef) {
404-
/* $FlowFixMe[incompatible-return] Suppresses errors found when fixing
405-
* TextInput typing */
406404
return this._listRef.getScrollRef();
407405
}
408406
}

0 commit comments

Comments
 (0)