Skip to content

Commit a1552c0

Browse files
j-piaseckifacebook-github-bot
authored andcommitted
Align SectionList types with OSS (#50074)
Summary: Changelog: [Internal] Reviewed By: huntie Differential Revision: D71318882
1 parent df30038 commit a1552c0

File tree

8 files changed

+184
-125
lines changed

8 files changed

+184
-125
lines changed

packages/react-native/Libraries/Animated/components/AnimatedSectionList.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ import * as React from 'react';
1818
export default (createAnimatedComponent(SectionList): AnimatedComponentType<
1919
React.ElementConfig<typeof SectionList>,
2020
// $FlowExpectedError[unclear-type]
21-
SectionList<SectionBase<any>>,
21+
SectionList<any, SectionBase<any>>,
2222
>);

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

+31-20
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {ScrollResponderType} from '../Components/ScrollView/ScrollView';
1414
import type {
1515
ScrollToLocationParamsType,
1616
SectionBase as _SectionBase,
17+
SectionData,
1718
VirtualizedSectionListProps,
1819
} from '@react-native/virtualized-lists';
1920

@@ -23,11 +24,18 @@ import * as React from 'react';
2324

2425
const VirtualizedSectionList = VirtualizedLists.VirtualizedSectionList;
2526

26-
type Item = any;
27+
type DefaultSectionT = {
28+
[key: string]: any,
29+
};
30+
31+
export type SectionBase<
32+
SectionItemT,
33+
SectionT = DefaultSectionT,
34+
> = _SectionBase<SectionItemT, SectionT>;
2735

28-
export type SectionBase<SectionItemT> = _SectionBase<SectionItemT>;
36+
export type {SectionData};
2937

30-
type RequiredProps<SectionT: SectionBase<any>> = {
38+
type RequiredProps<ItemT, SectionT = DefaultSectionT> = {
3139
/**
3240
* The actual data to render, akin to the `data` prop in [`<FlatList>`](https://reactnative.dev/docs/flatlist).
3341
*
@@ -39,17 +47,16 @@ type RequiredProps<SectionT: SectionBase<any>> = {
3947
* ItemSeparatorComponent?: ?ReactClass<{highlighted: boolean, ...}>,
4048
* }>
4149
*/
42-
sections: $ReadOnlyArray<SectionT>,
50+
sections: $ReadOnlyArray<SectionData<ItemT, SectionT>>,
4351
};
44-
45-
type OptionalProps<SectionT: SectionBase<any>> = {
52+
type OptionalProps<ItemT, SectionT = DefaultSectionT> = {
4653
/**
4754
* Default renderer for every item in every section. Can be over-ridden on a per-section basis.
4855
*/
4956
renderItem?: (info: {
50-
item: Item,
57+
item: ItemT,
5158
index: number,
52-
section: SectionT,
59+
section: SectionData<ItemT, SectionT>,
5360
separators: {
5461
highlight: () => void,
5562
unhighlight: () => void,
@@ -80,7 +87,7 @@ type OptionalProps<SectionT: SectionBase<any>> = {
8087
* falls back to using the index, like react does. Note that this sets keys for each item, but
8188
* each overall section still needs its own key.
8289
*/
83-
keyExtractor?: ?(item: Item, index: number) => string,
90+
keyExtractor?: ?(item: ItemT, index: number) => string,
8491
/**
8592
* Called once when the scroll position gets within `onEndReachedThreshold` of the rendered
8693
* content.
@@ -94,28 +101,31 @@ type OptionalProps<SectionT: SectionBase<any>> = {
94101
removeClippedSubviews?: boolean,
95102
};
96103

97-
export type Props<SectionT> = {
104+
export type SectionListProps<ItemT, SectionT = DefaultSectionT> = {
98105
...$Diff<
99-
VirtualizedSectionListProps<SectionT>,
106+
VirtualizedSectionListProps<ItemT, SectionT>,
100107
{
101-
getItem: $PropertyType<VirtualizedSectionListProps<SectionT>, 'getItem'>,
108+
getItem: $PropertyType<
109+
VirtualizedSectionListProps<ItemT, SectionT>,
110+
'getItem',
111+
>,
102112
getItemCount: $PropertyType<
103-
VirtualizedSectionListProps<SectionT>,
113+
VirtualizedSectionListProps<ItemT, SectionT>,
104114
'getItemCount',
105115
>,
106116
renderItem: $PropertyType<
107-
VirtualizedSectionListProps<SectionT>,
117+
VirtualizedSectionListProps<ItemT, SectionT>,
108118
'renderItem',
109119
>,
110120
keyExtractor: $PropertyType<
111-
VirtualizedSectionListProps<SectionT>,
121+
VirtualizedSectionListProps<ItemT, SectionT>,
112122
'keyExtractor',
113123
>,
114124
...
115125
},
116126
>,
117-
...RequiredProps<SectionT>,
118-
...OptionalProps<SectionT>,
127+
...RequiredProps<ItemT, SectionT>,
128+
...OptionalProps<ItemT, SectionT>,
119129
};
120130

121131
/**
@@ -174,9 +184,10 @@ export type Props<SectionT> = {
174184
*
175185
*/
176186
export default class SectionList<
177-
SectionT: SectionBase<any>,
178-
> extends React.PureComponent<Props<SectionT>, void> {
179-
props: Props<SectionT>;
187+
ItemT,
188+
SectionT = DefaultSectionT,
189+
> extends React.PureComponent<SectionListProps<ItemT, SectionT>> {
190+
props: SectionListProps<ItemT, SectionT>;
180191

181192
/**
182193
* Scrolls to the item at the specified `sectionIndex` and `itemIndex` (within the section)

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

+27-18
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {ScrollResponderType} from '../Components/ScrollView/ScrollView';
1414
import type {
1515
ScrollToLocationParamsType,
1616
SectionBase as _SectionBase,
17+
SectionData,
1718
VirtualizedSectionListProps,
1819
} from '@react-native/virtualized-lists';
1920
import type {ElementRef} from 'react';
@@ -24,11 +25,16 @@ import React, {forwardRef, useImperativeHandle, useRef} from 'react';
2425

2526
const VirtualizedSectionList = VirtualizedLists.VirtualizedSectionList;
2627

27-
type Item = any;
28+
type DefaultSectionT = {
29+
[key: string]: any,
30+
};
2831

29-
export type SectionBase<SectionItemT> = _SectionBase<SectionItemT>;
32+
export type SectionBase<
33+
SectionItemT,
34+
SectionT = DefaultSectionT,
35+
> = _SectionBase<SectionItemT, SectionT>;
3036

31-
type RequiredProps<SectionT: SectionBase<any>> = {
37+
type RequiredProps<ItemT, SectionT = DefaultSectionT> = {
3238
/**
3339
* The actual data to render, akin to the `data` prop in [`<FlatList>`](https://reactnative.dev/docs/flatlist).
3440
*
@@ -40,17 +46,17 @@ type RequiredProps<SectionT: SectionBase<any>> = {
4046
* ItemSeparatorComponent?: ?ReactClass<{highlighted: boolean, ...}>,
4147
* }>
4248
*/
43-
sections: $ReadOnlyArray<SectionT>,
49+
sections: $ReadOnlyArray<SectionData<ItemT, SectionT>>,
4450
};
4551

46-
type OptionalProps<SectionT: SectionBase<any>> = {
52+
type OptionalProps<ItemT, SectionT = DefaultSectionT> = {
4753
/**
4854
* Default renderer for every item in every section. Can be over-ridden on a per-section basis.
4955
*/
5056
renderItem?: (info: {
51-
item: Item,
57+
item: ItemT,
5258
index: number,
53-
section: SectionT,
59+
section: SectionData<ItemT, SectionT>,
5460
separators: {
5561
highlight: () => void,
5662
unhighlight: () => void,
@@ -81,7 +87,7 @@ type OptionalProps<SectionT: SectionBase<any>> = {
8187
* falls back to using the index, like react does. Note that this sets keys for each item, but
8288
* each overall section still needs its own key.
8389
*/
84-
keyExtractor?: ?(item: Item, index: number) => string,
90+
keyExtractor?: ?(item: ItemT, index: number) => string,
8591
/**
8692
* Called once when the scroll position gets within `onEndReachedThreshold` of the rendered
8793
* content.
@@ -95,28 +101,31 @@ type OptionalProps<SectionT: SectionBase<any>> = {
95101
removeClippedSubviews?: boolean,
96102
};
97103

98-
export type Props<SectionT: SectionBase<any>> = $ReadOnly<{
104+
export type Props<ItemT, SectionT = DefaultSectionT> = $ReadOnly<{
99105
...$Diff<
100-
VirtualizedSectionListProps<SectionT>,
106+
VirtualizedSectionListProps<ItemT, SectionT>,
101107
{
102-
getItem: $PropertyType<VirtualizedSectionListProps<SectionT>, 'getItem'>,
108+
getItem: $PropertyType<
109+
VirtualizedSectionListProps<ItemT, SectionT>,
110+
'getItem',
111+
>,
103112
getItemCount: $PropertyType<
104-
VirtualizedSectionListProps<SectionT>,
113+
VirtualizedSectionListProps<ItemT, SectionT>,
105114
'getItemCount',
106115
>,
107116
renderItem: $PropertyType<
108-
VirtualizedSectionListProps<SectionT>,
117+
VirtualizedSectionListProps<ItemT, SectionT>,
109118
'renderItem',
110119
>,
111120
keyExtractor: $PropertyType<
112-
VirtualizedSectionListProps<SectionT>,
121+
VirtualizedSectionListProps<ItemT, SectionT>,
113122
'keyExtractor',
114123
>,
115124
...
116125
},
117126
>,
118-
...RequiredProps<SectionT>,
119-
...OptionalProps<SectionT>,
127+
...RequiredProps<ItemT, SectionT>,
128+
...OptionalProps<ItemT, SectionT>,
120129
}>;
121130

122131
/**
@@ -176,8 +185,8 @@ export type Props<SectionT: SectionBase<any>> = $ReadOnly<{
176185
*/
177186
const SectionList: component(
178187
ref?: React.RefSetter<any>,
179-
...Props<SectionBase<any>>
180-
) = forwardRef<Props<SectionBase<any>>, any>((props, ref) => {
188+
...Props<any, DefaultSectionT>
189+
) = forwardRef<Props<any, DefaultSectionT>, any>((props, ref) => {
181190
const propsWithDefaults = {
182191
stickySectionHeadersEnabled: Platform.OS === 'ios',
183192
...props,

0 commit comments

Comments
 (0)