Skip to content

Commit

Permalink
Support 3 levels of navigation
Browse files Browse the repository at this point in the history
Summary: Changelog: [Internal] RNTester to support 3 levels of navigation

Reviewed By: kacieb

Differential Revision: D29481463

fbshipit-source-id: e48281cce7fccd7096446c5f0f5583f2588b5028
  • Loading branch information
Luna Wei authored and facebook-github-bot committed Jul 1, 2021
1 parent 8765b93 commit c06d8d0
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 14 deletions.
28 changes: 26 additions & 2 deletions packages/rn-tester/js/RNTesterAppShared.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,13 @@ const RNTesterApp = (): React.Node => {
);
const colorScheme = useColorScheme();

const {activeModuleKey, screen, bookmarks, recentlyUsed} = state;
const {
activeModuleKey,
activeModuleExampleKey,
screen,
bookmarks,
recentlyUsed,
} = state;

React.useEffect(() => {
getInitialStateFromAsyncStorage(APP_STATE_KEY).then(
Expand Down Expand Up @@ -166,6 +172,16 @@ const RNTesterApp = (): React.Node => {
[dispatch],
);

const handleModuleExampleCardPress = React.useCallback(
exampleName => {
dispatch({
type: RNTesterActionsType.EXAMPLE_CARD_PRESS,
data: {key: exampleName},
});
},
[dispatch],
);

const toggleBookmark = React.useCallback(
({exampleType, key}) => {
dispatch({
Expand Down Expand Up @@ -194,6 +210,10 @@ const RNTesterApp = (): React.Node => {

const activeModule =
activeModuleKey != null ? RNTesterList.Modules[activeModuleKey] : null;
const activeModuleExample =
activeModuleExampleKey != null
? activeModule?.examples.find(e => e.name === activeModuleExampleKey)
: null;
const title = Screens.COMPONENTS
? 'Components'
: Screens.APIS
Expand All @@ -210,7 +230,11 @@ const RNTesterApp = (): React.Node => {
theme={theme}
documentationURL={activeModule.documentationURL}
/>
<RNTesterModuleContainer module={activeModule} />
<RNTesterModuleContainer
module={activeModule}
example={activeModuleExample}
onExampleCardPress={handleModuleExampleCardPress}
/>
</View>
) : (
<ModuleListsContainer
Expand Down
2 changes: 1 addition & 1 deletion packages/rn-tester/js/components/RNTPressableRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export default function RNTPressableRow({
rightAddOn,
bottomAddOn,
onPress,
accessibilityLabel,
style,
accessibilityLabel,
}: Props): React.Node {
const theme = React.useContext(RNTesterThemeContext);
const label = accessibilityLabel ?? `${title} ${description ?? ''}`;
Expand Down
37 changes: 28 additions & 9 deletions packages/rn-tester/js/components/RNTesterModuleContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
*/

const React = require('react');
const {Platform} = require('react-native');
const RNTesterBlock = require('./RNTesterBlock');
const RNTesterExampleFilter = require('./RNTesterExampleFilter');
import RNTPressableRow from './RNTPressableRow';
import {RNTesterThemeContext} from './RNTesterTheme';
import {StyleSheet, Platform} from 'react-native';

const invariant = require('invariant');
import ExamplePage from './ExamplePage';
Expand All @@ -30,15 +32,27 @@ function getExampleTitle(title, platform) {
}

export default function RNTesterModuleContainer(props: Props): React.Node {
const {module, example} = props;
const {module, example, onExampleCardPress} = props;
const theme = React.useContext(RNTesterThemeContext);
const renderExample = (e, i) => {
// Filter platform-specific es
const {description, platform} = e;
let {title} = e;
if (platform != null && Platform.OS !== platform) {
return null;
}
return (
return module.showIndividualExamples === true ? (
<RNTPressableRow
key={e.name}
onPress={() => onExampleCardPress(e.name)}
title={e.title}
description={description}
accessibilityLabel={e.name + ' ' + description}
style={StyleSheet.compose(styles.separator, {
borderBottomColor: theme.SeparatorColor,
})}
/>
) : (
<RNTesterBlock
key={i}
title={getExampleTitle(title, platform)}
Expand Down Expand Up @@ -99,12 +113,7 @@ export default function RNTesterModuleContainer(props: Props): React.Node {
documentationURL={module.documentationURL}
category={module.category}>
{module.showIndividualExamples === true && example != null ? (
<RNTesterBlock
key={example.name}
title={getExampleTitle(example.title, example.platform)}
description={example.description}>
{example.render()}
</RNTesterBlock>
example.render()
) : (
<RNTesterExampleFilter
testID="example_search"
Expand All @@ -120,3 +129,13 @@ export default function RNTesterModuleContainer(props: Props): React.Node {
</ExamplePage>
);
}

const styles = StyleSheet.create({
separator: {
borderBottomWidth: Platform.select({
ios: StyleSheet.hairlineWidth,
android: 0,
}),
marginHorizontal: Platform.select({ios: 15, android: 0}),
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export function SectionList_onEndReached(): React.Node {
export default {
title: 'SectionList Inverted',
name: 'SectionList-onEndReached',
description: 'Test onEndReached behavior',
render: function(): React.Element<typeof SectionList_onEndReached> {
return <SectionList_onEndReached />;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function SectionList_stickySectionHeadersEnabled(): React.Node {
export default {
title: 'SectionList Sticky Headers Enabled',
name: 'SectionList-stickyHeadersEnabled',
description: 'Toggle sticky headers on/off',
render: function(): React.Element<
typeof SectionList_stickySectionHeadersEnabled,
> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ const styles = StyleSheet.create({
height: 40,
},
output: {
width: '80%',
flex: 1,
fontSize: 12,
},
list: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ exports.title = 'SectionList';
exports.category = 'ListView';
exports.documentationURL = 'https://reactnative.dev/docs/sectionlist';
exports.description = 'Performant, scrollable list of data.';
exports.showIndividualExamples = true;
exports.examples = [
ContentInset,
onEndReached,
Expand Down
1 change: 1 addition & 0 deletions packages/rn-tester/js/types/RNTesterTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export type ComponentList = null | {components: string[], apis: string[]};

export type RNTesterState = {
activeModuleKey: null | string,
activeModuleExampleKey: null | string,
screen: ScreenTypes,
bookmarks: ComponentList,
recentlyUsed: ComponentList,
Expand Down
12 changes: 11 additions & 1 deletion packages/rn-tester/js/utils/RNTesterReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const RNTesterActionsType = {
BOOKMARK_PRESS: 'BOOKMARK_PRESS',
BACK_BUTTON_PRESS: 'BACK_BUTTON_PRESS',
MODULE_CARD_PRESS: 'MODULE_CARD_PRESS',
EXAMPLE_CARD_PRESS: 'EXAMPLE_CARD_PRESS',
};

const getUpdatedBookmarks = ({
Expand Down Expand Up @@ -77,18 +78,25 @@ export const RNTesterReducer = (
return {
...state,
activeModuleKey: null,
activeModuleExampleKey: null,
screen: action.data.screen,
};
case RNTesterActionsType.MODULE_CARD_PRESS:
return {
...state,
activeModuleKey: action.data.key,
activeModuleExampleKey: null,
recentlyUsed: getUpdatedRecentlyUsed({
exampleType: action.data.exampleType,
key: action.data.key,
recentlyUsed: state.recentlyUsed,
}),
};
case RNTesterActionsType.EXAMPLE_CARD_PRESS:
return {
...state,
activeModuleExampleKey: action.data.key,
};
case RNTesterActionsType.BOOKMARK_PRESS:
return {
...state,
Expand All @@ -102,7 +110,9 @@ export const RNTesterReducer = (
// Go back to module or list
return {
...state,
activeModuleKey: null,
activeModuleExampleKey: null,
activeModuleKey:
state.activeModuleExampleKey != null ? state.activeModuleKey : null,
};
default:
throw new Error(`Invalid action type ${action.type}`);
Expand Down
2 changes: 2 additions & 0 deletions packages/rn-tester/js/utils/testerStateUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const Screens = {

export const initialState: RNTesterState = {
activeModuleKey: null,
activeModuleExampleKey: null,
screen: null,
bookmarks: null,
recentlyUsed: null,
Expand Down Expand Up @@ -134,6 +135,7 @@ export const getInitialStateFromAsyncStorage = async (
if (!initialStateString) {
return {
activeModuleKey: null,
activeModuleExampleKey: null,
screen: Screens.COMPONENTS,
bookmarks: {components: [], apis: []},
recentlyUsed: {components: [], apis: []},
Expand Down

0 comments on commit c06d8d0

Please sign in to comment.