Files
react-native/RNTester/js/RNTesterExampleList.js
Josh JusticeandMike Grabowski bd79303d5b Added filtering to RNTester example screens (#22777)
Summary:
This PR adds filtering functionality to individual example screens of RNTester. This is useful for Detox testing of RNTester, since Detox requires elements to be visible on the screen before they can be interacted with. Instead of needing to scroll an arbitrary amount, the test can enter the name of the example to be tested, just as is done on the main screen. This will lead to simpler and more reliable E2E tests for long example screens. This PR doesn't add any automated tests using the filter; those will be added in a separate PR.

This is implemented by extracting the existing filtering functionality out of `RNTesterExampleList` into a shared `RNTesterExampleFilter` component that can be used both within `RNTesterExampleList` (the main screen) and `RNTesterExampleContainer` (the example screen).

![simulator screen shot - iphone 8 - 2018-12-24 at 08 22 46](https://user-images.githubusercontent.com/15832198/50401564-4273a300-0755-11e9-9120-9bf8fbb70261.png)

![simulator screen shot - iphone 8 - 2018-12-24 at 08 22 51](https://user-images.githubusercontent.com/15832198/50401566-44d5fd00-0755-11e9-9637-6e5ddce1c476.png)

Changelog:
----------

[General] [Added] - Added filtering to RNTester example screens
Pull Request resolved: https://github.com/facebook/react-native/pull/22777

Reviewed By: TheSavior

Differential Revision: D13561744

Pulled By: rickhanlonii

fbshipit-source-id: cb120626a8e2b8440f88b871557c0b92fbef5edc
2019-01-03 18:58:05 +01:00

207 lines
5.4 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/
'use strict';
const Platform = require('Platform');
const React = require('react');
const SectionList = require('SectionList');
const StyleSheet = require('StyleSheet');
const Text = require('Text');
const TouchableHighlight = require('TouchableHighlight');
const RNTesterActions = require('./RNTesterActions');
const RNTesterExampleFilter = require('./RNTesterExampleFilter');
const View = require('View');
/* $FlowFixMe(>=0.78.0 site=react_native_android_fb) This issue was found when
* making Flow check .android.js files. */
import type {RNTesterExample} from './RNTesterList.ios';
import type {ViewStyleProp} from 'StyleSheet';
type Props = {
onNavigate: Function,
list: {
ComponentExamples: Array<RNTesterExample>,
APIExamples: Array<RNTesterExample>,
},
style?: ?ViewStyleProp,
};
class RowComponent extends React.PureComponent<{
item: Object,
onNavigate: Function,
onPress?: Function,
onShowUnderlay?: Function,
onHideUnderlay?: Function,
}> {
_onPress = () => {
if (this.props.onPress) {
this.props.onPress();
return;
}
this.props.onNavigate(RNTesterActions.ExampleAction(this.props.item.key));
};
render() {
const {item} = this.props;
return (
<TouchableHighlight
onShowUnderlay={this.props.onShowUnderlay}
onHideUnderlay={this.props.onHideUnderlay}
onPress={this._onPress}>
<View style={styles.row}>
<Text style={styles.rowTitleText}>{item.module.title}</Text>
<Text style={styles.rowDetailText}>{item.module.description}</Text>
</View>
</TouchableHighlight>
);
}
}
const renderSectionHeader = ({section}) => (
<Text style={styles.sectionHeader}>{section.title}</Text>
);
class RNTesterExampleList extends React.Component<Props, $FlowFixMeState> {
render() {
const filter = ({example, filterRegex}) =>
/* $FlowFixMe(>=0.68.0 site=react_native_fb) This comment suppresses an
* error found when Flow v0.68 was deployed. To see the error delete this
* comment and run Flow. */
filterRegex.test(example.module.title) &&
(!Platform.isTV || example.supportsTVOS);
const sections = [
{
data: this.props.list.ComponentExamples,
title: 'COMPONENTS',
key: 'c',
},
{
data: this.props.list.APIExamples,
title: 'APIS',
key: 'a',
},
];
return (
<View style={[styles.listContainer, this.props.style]}>
{this._renderTitleRow()}
<RNTesterExampleFilter
sections={sections}
filter={filter}
render={({filteredSections}) => (
<SectionList
ItemSeparatorComponent={ItemSeparator}
contentContainerStyle={styles.sectionListContentContainer}
style={styles.list}
sections={filteredSections}
renderItem={this._renderItem}
enableEmptySections={true}
itemShouldUpdate={this._itemShouldUpdate}
keyboardShouldPersistTaps="handled"
automaticallyAdjustContentInsets={false}
keyboardDismissMode="on-drag"
renderSectionHeader={renderSectionHeader}
/>
)}
/>
</View>
);
}
_itemShouldUpdate(curr, prev) {
return curr.item !== prev.item;
}
_renderItem = ({item, separators}) => (
<RowComponent
item={item}
onNavigate={this.props.onNavigate}
onShowUnderlay={separators.highlight}
onHideUnderlay={separators.unhighlight}
/>
);
_renderTitleRow(): ?React.Element<any> {
/* $FlowFixMe(>=0.68.0 site=react_native_fb) This comment suppresses an
* error found when Flow v0.68 was deployed. To see the error delete this
* comment and run Flow. */
if (!this.props.displayTitleRow) {
return null;
}
return (
<RowComponent
item={{
module: {
title: 'RNTester',
description: 'React Native Examples',
},
}}
onNavigate={this.props.onNavigate}
onPress={() => {
this.props.onNavigate(RNTesterActions.ExampleList());
}}
/>
);
}
_handleRowPress(exampleKey: string): void {
this.props.onNavigate(RNTesterActions.ExampleAction(exampleKey));
}
}
const ItemSeparator = ({highlighted}) => (
<View style={highlighted ? styles.separatorHighlighted : styles.separator} />
);
const styles = StyleSheet.create({
listContainer: {
flex: 1,
},
list: {
backgroundColor: '#eeeeee',
},
sectionHeader: {
backgroundColor: '#eeeeee',
padding: 5,
fontWeight: '500',
fontSize: 11,
},
row: {
backgroundColor: 'white',
justifyContent: 'center',
paddingHorizontal: 15,
paddingVertical: 8,
},
separator: {
height: StyleSheet.hairlineWidth,
backgroundColor: '#bbbbbb',
marginLeft: 15,
},
separatorHighlighted: {
height: StyleSheet.hairlineWidth,
backgroundColor: 'rgb(217, 217, 217)',
},
sectionListContentContainer: {
backgroundColor: 'white',
},
rowTitleText: {
fontSize: 17,
fontWeight: '500',
},
rowDetailText: {
fontSize: 15,
color: '#888888',
lineHeight: 20,
},
});
module.exports = RNTesterExampleList;