UNPKG

7.94 kBTypeScriptView Raw
1/**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @format
8 */
9
10import type * as React from 'react';
11import type {
12 ListRenderItem,
13 ViewToken,
14 VirtualizedListProps,
15 ViewabilityConfig,
16} from '@react-native/virtualized-lists';
17import type {ScrollViewComponent} from '../Components/ScrollView/ScrollView';
18import type {StyleProp} from '../StyleSheet/StyleSheet';
19import type {ViewStyle} from '../StyleSheet/StyleSheetTypes';
20import type {View} from '../Components/View/View';
21
22export interface FlatListProps<ItemT> extends VirtualizedListProps<ItemT> {
23 /**
24 * Optional custom style for multi-item rows generated when numColumns > 1
25 */
26 columnWrapperStyle?: StyleProp<ViewStyle> | undefined;
27
28 /**
29 * Determines when the keyboard should stay visible after a tap.
30 * - 'never' (the default), tapping outside of the focused text input when the keyboard is up dismisses the keyboard. When this happens, children won't receive the tap.
31 * - 'always', the keyboard will not dismiss automatically, and the scroll view will not catch taps, but children of the scroll view can catch taps.
32 * - 'handled', the keyboard will not dismiss automatically when the tap was handled by a children, (or captured by an ancestor).
33 * - false, deprecated, use 'never' instead
34 * - true, deprecated, use 'always' instead
35 */
36 keyboardShouldPersistTaps?:
37 | boolean
38 | 'always'
39 | 'never'
40 | 'handled'
41 | undefined;
42
43 /**
44 * An array (or array-like list) of items to render. Other data types can be
45 * used by targeting VirtualizedList directly.
46 */
47 data: ArrayLike<ItemT> | null | undefined;
48
49 /**
50 * A marker property for telling the list to re-render (since it implements PureComponent).
51 * If any of your `renderItem`, Header, Footer, etc. functions depend on anything outside of the `data` prop,
52 * stick it here and treat it immutably.
53 */
54 extraData?: any | undefined;
55
56 /**
57 * `getItemLayout` is an optional optimization that lets us skip measurement of dynamic
58 * content if you know the height of items a priori. getItemLayout is the most efficient,
59 * and is easy to use if you have fixed height items, for example:
60 * ```
61 * getItemLayout={(data, index) => (
62 * {length: ITEM_HEIGHT, offset: ITEM_HEIGHT * index, index}
63 * )}
64 * ```
65 * Remember to include separator length (height or width) in your offset calculation if you specify
66 * `ItemSeparatorComponent`.
67 */
68 getItemLayout?:
69 | ((
70 data: ArrayLike<ItemT> | null | undefined,
71 index: number,
72 ) => {length: number; offset: number; index: number})
73 | undefined;
74
75 /**
76 * If true, renders items next to each other horizontally instead of stacked vertically.
77 */
78 horizontal?: boolean | null | undefined;
79
80 /**
81 * How many items to render in the initial batch
82 */
83 initialNumToRender?: number | undefined;
84
85 /**
86 * Instead of starting at the top with the first item, start at initialScrollIndex
87 */
88 initialScrollIndex?: number | null | undefined;
89
90 /**
91 * Used to extract a unique key for a given item at the specified index. Key is used for caching
92 * and as the react key to track item re-ordering. The default extractor checks `item.key`, then
93 * falls back to using the index, like React does.
94 */
95 keyExtractor?: ((item: ItemT, index: number) => string) | undefined;
96
97 /**
98 * Uses legacy MetroListView instead of default VirtualizedSectionList
99 */
100 legacyImplementation?: boolean | undefined;
101
102 /**
103 * Multiple columns can only be rendered with `horizontal={false}` and will zig-zag like a `flexWrap` layout.
104 * Items should all be the same height - masonry layouts are not supported.
105 */
106 numColumns?: number | undefined;
107
108 /**
109 * If provided, a standard RefreshControl will be added for "Pull to Refresh" functionality.
110 * Make sure to also set the refreshing prop correctly.
111 */
112 onRefresh?: (() => void) | null | undefined;
113
114 /**
115 * Called when the viewability of rows changes, as defined by the `viewablePercentThreshold` prop.
116 */
117 onViewableItemsChanged?:
118 | ((info: {
119 viewableItems: Array<ViewToken<ItemT>>;
120 changed: Array<ViewToken<ItemT>>;
121 }) => void)
122 | null
123 | undefined;
124
125 /**
126 * Set this true while waiting for new data from a refresh.
127 */
128 refreshing?: boolean | null | undefined;
129
130 /**
131 * Takes an item from data and renders it into the list. Typical usage:
132 * ```
133 * _renderItem = ({item}) => (
134 * <TouchableOpacity onPress={() => this._onPress(item)}>
135 * <Text>{item.title}</Text>
136 * </TouchableOpacity>
137 * );
138 * ...
139 * <FlatList data={[{title: 'Title Text', key: 'item1'}]} renderItem={this._renderItem} />
140 * ```
141 * Provides additional metadata like `index` if you need it.
142 */
143 renderItem: ListRenderItem<ItemT> | null | undefined;
144
145 /**
146 * See `ViewabilityHelper` for flow type and further documentation.
147 */
148 viewabilityConfig?: ViewabilityConfig | undefined;
149
150 /**
151 * Note: may have bugs (missing content) in some circumstances - use at your own risk.
152 *
153 * This may improve scroll performance for large lists.
154 */
155 removeClippedSubviews?: boolean | undefined;
156
157 /**
158 * Fades out the edges of the scroll content.
159 *
160 * If the value is greater than 0, the fading edges will be set accordingly
161 * to the current scroll direction and position,
162 * indicating if there is more content to show.
163 *
164 * The default value is 0.
165 * @platform android
166 */
167 fadingEdgeLength?: number | undefined;
168}
169
170export abstract class FlatListComponent<
171 ItemT,
172 Props,
173> extends React.Component<Props> {
174 /**
175 * Scrolls to the end of the content. May be janky without `getItemLayout` prop.
176 */
177 scrollToEnd: (params?: {animated?: boolean | null | undefined}) => void;
178
179 /**
180 * Scrolls to the item at the specified index such that it is positioned in the viewable area
181 * such that viewPosition 0 places it at the top, 1 at the bottom, and 0.5 centered in the middle.
182 * Cannot scroll to locations outside the render window without specifying the getItemLayout prop.
183 */
184 scrollToIndex: (params: {
185 animated?: boolean | null | undefined;
186 index: number;
187 viewOffset?: number | undefined;
188 viewPosition?: number | undefined;
189 }) => void;
190
191 /**
192 * Requires linear scan through data - use `scrollToIndex` instead if possible.
193 * May be janky without `getItemLayout` prop.
194 */
195 scrollToItem: (params: {
196 animated?: boolean | null | undefined;
197 item: ItemT;
198 viewOffset?: number | undefined;
199 viewPosition?: number | undefined;
200 }) => void;
201
202 /**
203 * Scroll to a specific content pixel offset, like a normal `ScrollView`.
204 */
205 scrollToOffset: (params: {
206 animated?: boolean | null | undefined;
207 offset: number;
208 }) => void;
209
210 /**
211 * Tells the list an interaction has occurred, which should trigger viewability calculations,
212 * e.g. if waitForInteractions is true and the user has not scrolled. This is typically called
213 * by taps on items or by navigation actions.
214 */
215 recordInteraction: () => void;
216
217 /**
218 * Displays the scroll indicators momentarily.
219 */
220 flashScrollIndicators: () => void;
221
222 /**
223 * Provides a handle to the underlying scroll responder.
224 */
225 getScrollResponder: () => JSX.Element | null | undefined;
226
227 /**
228 * Provides a reference to the underlying host component
229 */
230 getNativeScrollRef: () =>
231 | React.ElementRef<typeof View>
232 | React.ElementRef<typeof ScrollViewComponent>
233 | null
234 | undefined;
235
236 getScrollableNode: () => any;
237
238 // TODO: use `unknown` instead of `any` for Typescript >= 3.0
239 setNativeProps: (props: {[key: string]: any}) => void;
240}
241
242export class FlatList<ItemT = any> extends FlatListComponent<
243 ItemT,
244 FlatListProps<ItemT>
245> {}
246
\No newline at end of file