Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor Code Improvements in RNTester #29868

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions packages/rn-tester/js/RNTesterAppShared.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,22 @@ const RNTesterApp = (): React.Node => {

// Setup hardware back button press listener
React.useEffect(() => {
BackHandler.addEventListener('hardwareBackPress', () => {
const handleHardwareBackPress = () => {
if (openExample) {
handleBackPress();
return true;
}
return false;
});
};

BackHandler.addEventListener('hardwareBackPress', handleHardwareBackPress);

return () => {
BackHandler.removeEventListener(
'hardwareBackPress',
handleHardwareBackPress,
);
};
}, [openExample, handleBackPress]);

const handleExampleCardPress = React.useCallback(
Expand Down
38 changes: 0 additions & 38 deletions packages/rn-tester/js/components/RNTesterBookmark.js

This file was deleted.

10 changes: 7 additions & 3 deletions packages/rn-tester/js/components/RNTesterDocumentationURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/

'use strict';

import React from 'react';
import * as React from 'react';
import {Image, StyleSheet, TouchableOpacity} from 'react-native';
import openURLInBrowser from 'react-native/Libraries/Core/Devtools/openURLInBrowser';

const RNTesterDocumentationURL = ({documentationURL}) => (
type Props = $ReadOnly<{|
documentationURL: string,
|}>;

const RNTesterDocumentationURL = ({documentationURL}: Props): React.Node => (
<TouchableOpacity
style={styles.container}
onPress={() => openURLInBrowser(documentationURL)}>
Expand All @@ -28,7 +33,6 @@ export default RNTesterDocumentationURL;

const styles = StyleSheet.create({
container: {
textDecorationLine: 'underline',
position: 'absolute',
bottom: 0,
right: -15,
Expand Down
173 changes: 95 additions & 78 deletions packages/rn-tester/js/components/RNTesterNavbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,86 @@ import {Text, View, StyleSheet, Image, Pressable} from 'react-native';

import {RNTesterThemeContext} from './RNTesterTheme';

const BookmarkTab = ({handleNavBarPress, isBookmarkActive, theme}) => (
<View style={styles.centerBox}>
<Image
style={styles.centralBoxCutout}
source={require('./../assets/bottom-nav-center-box.png')}
/>
<View style={styles.floatContainer}>
<Pressable
testID="bookmarks-tab"
onPress={() => handleNavBarPress({screen: 'bookmarks'})}>
<View
style={[styles.floatingButton, {backgroundColor: theme.BorderColor}]}>
<Image
style={styles.bookmarkIcon}
source={
isBookmarkActive
? require('../assets/bottom-nav-bookmark-fill.png')
: require('../assets/bottom-nav-bookmark-outline.png')
}
/>
</View>
</Pressable>
</View>
</View>
);

const NavbarButton = ({
testID,
theme,
isActive,
activeImage,
inactiveImage,
label,
handlePress,
iconStyle,
}) => (
<Pressable
testID={testID}
onPress={handlePress}
style={[styles.navButton, {backgroundColor: theme.BackgroundColor}]}>
<View
style={[styles.pressableContent, isActive ? styles.activeBar : null]}
collapsable={false}>
<Image
style={iconStyle}
source={isActive ? activeImage : inactiveImage}
/>
<Text style={isActive ? styles.activeText : styles.inactiveText}>
{label}
</Text>
</View>
</Pressable>
);

const ComponentTab = ({isComponentActive, handleNavBarPress, theme}) => (
<NavbarButton
testID="components-tab"
label="Components"
handlePress={() => handleNavBarPress({screen: 'components'})}
activeImage={require('./../assets/bottom-nav-components-icon-active.png')}
inactiveImage={require('./../assets/bottom-nav-components-icon-inactive.png')}
isActive={isComponentActive}
theme={theme}
iconStyle={styles.componentIcon}
/>
);

const APITab = ({isAPIActive, handleNavBarPress, theme}) => (
<NavbarButton
testID="apis-tab"
label="APIs"
handlePress={() => handleNavBarPress({screen: 'apis'})}
activeImage={require('./../assets/bottom-nav-apis-icon-active.png')}
inactiveImage={require('./../assets/bottom-nav-apis-icon-inactive.png')}
isActive={isAPIActive}
theme={theme}
iconStyle={styles.apiIcon}
/>
);

type Props = $ReadOnly<{|
handleNavBarPress: (data: {screen: string}) => void,
screen: string,
Expand All @@ -33,84 +113,21 @@ const RNTesterNavbar = ({
return (
<View>
<View style={styles.buttonContainer}>
<Pressable
testID="components-tab"
onPress={() => handleNavBarPress({screen: 'components'})}
style={[styles.navButton, {backgroundColor: theme.BackgroundColor}]}>
<View
style={[
styles.pressableContent,
isComponentActive ? styles.activeBar : null,
]}
collapsable={false}>
<Image
style={styles.componentIcon}
source={
isComponentActive
? require('./../assets/bottom-nav-components-icon-active.png')
: require('./../assets/bottom-nav-components-icon-inactive.png')
}
/>
<Text
style={
isComponentActive ? styles.activeText : styles.inactiveText
}>
Components
</Text>
</View>
</Pressable>

<View style={styles.centerBox}>
<Image
style={styles.centralBoxCutout}
source={require('./../assets/bottom-nav-center-box.png')}
/>

<View style={styles.floatContainer}>
<Pressable
testID="bookmarks-tab"
onPress={() => handleNavBarPress({screen: 'bookmarks'})}>
<View
style={[
styles.floatingButton,
{backgroundColor: theme.BorderColor},
]}>
<Image
style={styles.bookmarkIcon}
source={
isBookmarkActive
? require('../assets/bottom-nav-bookmark-fill.png')
: require('../assets/bottom-nav-bookmark-outline.png')
}
/>
</View>
</Pressable>
</View>
</View>

<Pressable
testID="apis-tab"
onPress={() => handleNavBarPress({screen: 'apis'})}
style={[styles.navButton, {backgroundColor: theme.BackgroundColor}]}>
<View
style={[
styles.pressableContent,
isAPIActive ? styles.activeBar : null,
]}
collapsable={false}>
<Image
style={styles.apiIcon}
source={
isAPIActive
? require('./../assets/bottom-nav-apis-icon-active.png')
: require('./../assets/bottom-nav-apis-icon-inactive.png')
}
/>
<Text style={isAPIActive ? styles.activeText : styles.inactiveText}>
APIs
</Text>
</View>
</Pressable>
<ComponentTab
isComponentActive={isComponentActive}
handleNavBarPress={handleNavBarPress}
theme={theme}
/>
<BookmarkTab
isBookmarkActive={isBookmarkActive}
handleNavBarPress={handleNavBarPress}
theme={theme}
/>
<APITab
isAPIActive={isAPIActive}
handleNavBarPress={handleNavBarPress}
theme={theme}
/>
</View>
</View>
);
Expand Down
24 changes: 14 additions & 10 deletions packages/rn-tester/js/utils/testerStateUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,33 @@ export const getExamplesListWithBookmarksAndRecentlyUsed = ({
return null;
}

const components = RNTesterList.ComponentExamples.map(c => ({
...c,
isBookmarked: bookmarks.components.includes(c.key),
const components = RNTesterList.ComponentExamples.map(componentExample => ({
...componentExample,
isBookmarked: bookmarks.components.includes(componentExample.key),
exampleType: Screens.COMPONENTS,
}));

const recentlyUsedComponents = recentlyUsed.components
.map(k => components.find(c => c.key === k))
.map(recentComponentKey =>
components.find(component => component.key === recentComponentKey),
)
.filter(Boolean);

const bookmarkedComponents = components.filter(c => c.isBookmarked);
const bookmarkedComponents = components.filter(
component => component.isBookmarked,
);

const apis = RNTesterList.APIExamples.map(c => ({
...c,
isBookmarked: bookmarks.apis.includes(c.key),
const apis = RNTesterList.APIExamples.map(apiExample => ({
...apiExample,
isBookmarked: bookmarks.apis.includes(apiExample.key),
exampleType: Screens.APIS,
}));

const recentlyUsedAPIs = recentlyUsed.apis
.map(k => apis.find(c => c.key === k))
.map(recentAPIKey => apis.find(apiEample => apiEample.key === recentAPIKey))
.filter(Boolean);

const bookmarkedAPIs = apis.filter(c => c.isBookmarked);
const bookmarkedAPIs = apis.filter(apiEample => apiEample.isBookmarked);

const examplesList: ExamplesList = {
[Screens.COMPONENTS]: [
Expand Down