Skip to content

Commit 174372c

Browse files
Luna Weifacebook-github-bot
authored andcommitted
SectionList Separators Example
Summary: Changelog: [General][Added] - Added an example showcasing how separator callbacks work in SectionList for RNTester Reviewed By: nadiia Differential Revision: D26575122 fbshipit-source-id: 46710e2647c84bdf083265ce04ba330bd70eb2a7
1 parent 270060e commit 174372c

File tree

4 files changed

+106
-10
lines changed

4 files changed

+106
-10
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its 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+
* @flow
9+
*/
10+
11+
'use strict';
12+
import {SectionList_withSeparators} from './SectionListExamples';
13+
const React = require('react');
14+
15+
exports.title = 'SectionList with Separators';
16+
exports.testTitle = 'Test custom separator components';
17+
exports.category = 'ListView';
18+
exports.documentationURL = 'https://reactnative.dev/docs/sectionlist';
19+
exports.description = 'Tap to see pressed states for separator components.';
20+
exports.examples = [
21+
{
22+
title: 'SectionList with Separators',
23+
render: function(): React.Element<typeof SectionList_withSeparators> {
24+
return <SectionList_withSeparators />;
25+
},
26+
},
27+
];

packages/rn-tester/js/examples/SectionList/SectionListExamples.js

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

11-
import {Button, SectionList, StyleSheet, Text, View} from 'react-native';
11+
import {
12+
Pressable,
13+
Button,
14+
SectionList,
15+
StyleSheet,
16+
Text,
17+
View,
18+
} from 'react-native';
1219
import * as React from 'react';
1320

1421
const DATA = [
@@ -36,11 +43,34 @@ const VIEWABILITY_CONFIG = {
3643
waitForInteraction: true,
3744
};
3845

39-
const Item = ({title}) => (
40-
<View style={styles.item} testID={title}>
41-
<Text style={styles.title}>{title}</Text>
42-
</View>
43-
);
46+
const Item = ({item, section, separators}) => {
47+
return (
48+
<Pressable
49+
onPressIn={separators.highlight}
50+
onPressOut={separators.unhighlight}
51+
style={({pressed}) => [
52+
styles.item,
53+
{
54+
backgroundColor: pressed ? 'red' : 'pink',
55+
},
56+
]}
57+
testID={item}>
58+
<Text style={styles.title}>{item}</Text>
59+
</Pressable>
60+
);
61+
};
62+
63+
const Separator = (defaultColor, highlightColor, text) => ({highlighted}) => {
64+
return (
65+
<View
66+
style={[
67+
styles.separator,
68+
{backgroundColor: highlighted ? highlightColor : defaultColor},
69+
]}>
70+
<Text style={styles.separtorText}>{text}</Text>
71+
</View>
72+
);
73+
};
4474

4575
export function SectionList_inverted(): React.Node {
4676
const [output, setOutput] = React.useState('inverted false');
@@ -163,6 +193,29 @@ export function SectionList_onEndReached(): React.Node {
163193
);
164194
}
165195

196+
export function SectionList_withSeparators(): React.Node {
197+
const exampleProps = {
198+
ItemSeparatorComponent: Separator('lightgreen', 'green', 'Item Separator'),
199+
SectionSeparatorComponent: Separator(
200+
'lightblue',
201+
'blue',
202+
'Section Separator',
203+
),
204+
};
205+
const ref = React.createRef<?React.ElementRef<typeof SectionList>>();
206+
207+
const onTest = null;
208+
209+
return (
210+
<SectionListExampleWithForwardedRef
211+
ref={ref}
212+
exampleProps={exampleProps}
213+
testOutput="Tap for press state of section and item separators"
214+
onTest={onTest}
215+
/>
216+
);
217+
}
218+
166219
export function SectionList_onViewableItemsChanged(): React.Node {
167220
const [output, setOutput] = React.useState('');
168221
const exampleProps = {
@@ -216,7 +269,7 @@ const SectionListExampleWithForwardedRef = React.forwardRef(
216269
testID="section_list"
217270
sections={DATA}
218271
keyExtractor={(item, index) => item + index}
219-
renderItem={({item}) => <Item title={item} />}
272+
renderItem={Item}
220273
renderSectionHeader={({section: {title}}) => (
221274
<Text style={styles.header}>{title}</Text>
222275
)}
@@ -265,4 +318,10 @@ const styles = StyleSheet.create({
265318
output: {
266319
fontSize: 12,
267320
},
321+
separator: {
322+
height: 12,
323+
},
324+
separtorText: {
325+
fontSize: 10,
326+
},
268327
});

packages/rn-tester/js/utils/RNTesterList.android.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ const ComponentExamples: Array<RNTesterExample> = [
8585
category: 'ListView',
8686
},
8787
{
88-
key: 'SectionList_inverted',
88+
key: 'SectionList-inverted',
8989
module: require('../examples/SectionList/SectionList-inverted'),
9090
category: 'ListView',
9191
},
@@ -95,10 +95,15 @@ const ComponentExamples: Array<RNTesterExample> = [
9595
category: 'ListView',
9696
},
9797
{
98-
key: 'SectionList_stickyHeadersEnabled',
98+
key: 'SectionList-stickyHeadersEnabled',
9999
module: require('../examples/SectionList/SectionList-stickyHeadersEnabled'),
100100
category: 'ListView',
101101
},
102+
{
103+
key: 'SectionList-withSeparators',
104+
module: require('../examples/SectionList/SectionList-withSeparators'),
105+
category: 'ListView',
106+
},
102107
{
103108
key: 'SectionListExample',
104109
category: 'ListView',

packages/rn-tester/js/utils/RNTesterList.ios.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,15 @@ const ComponentExamples: Array<RNTesterExample> = [
109109
category: 'ListView',
110110
},
111111
{
112-
key: 'SectionList_stickyHeadersEnabled',
112+
key: 'SectionList-stickyHeadersEnabled',
113113
module: require('../examples/SectionList/SectionList-stickyHeadersEnabled'),
114114
category: 'ListView',
115115
},
116+
{
117+
key: 'SectionList-withSeparators',
118+
module: require('../examples/SectionList/SectionList-withSeparators'),
119+
category: 'ListView',
120+
},
116121
{
117122
key: 'SectionList-onEndReached',
118123
module: require('../examples/SectionList/SectionList-onEndReached'),

0 commit comments

Comments
 (0)