Skip to content

feat(example): add nested complex views #1111

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

Merged
merged 4 commits into from
Jan 29, 2024
Merged
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,6 @@ android/keystores/debug.keystore

# Vscode local history
.history/

# .idea run configurations
/.run/*
38 changes: 38 additions & 0 deletions examples/default/src/components/InputField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React, { forwardRef } from 'react';

import { KeyboardTypeOptions, StyleSheet, TextInput } from 'react-native';

interface InputFieldProps {
placeholder?: string;
value?: string;
onChangeText?: (text: string) => void;
keyboardType?: KeyboardTypeOptions;
}

export const InputField = forwardRef<TextInput, InputFieldProps>(
({ placeholder, value, onChangeText, keyboardType, ...restProps }, ref) => {
return (
<TextInput
ref={ref}
placeholder={placeholder}
style={styles.textInput}
keyboardType={keyboardType}
value={value}
onChangeText={onChangeText}
{...restProps}
/>
);
},
);

const styles = StyleSheet.create({
textInput: {
backgroundColor: 'white',
borderWidth: 1,
borderColor: '#ccc',
paddingVertical: 16,
paddingHorizontal: 24,
fontSize: 16,
borderRadius: 5,
},
});
31 changes: 31 additions & 0 deletions examples/default/src/components/NestedView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';

import { Text } from 'native-base';
import { StyleSheet, View } from 'react-native';

interface NestedViewProps {
children?: React.ReactNode;
depth: number;
breadth?: number;
}

export const NestedView: React.FC<NestedViewProps> = ({ depth, breadth = 1, children }) => {
if (!depth) {
return <>{children}</>;
}
return (
<View style={styles.container}>
<Text>{depth}</Text>
{new Array(breadth).fill(null).map((_, index) => (
<NestedView key={index} breadth={breadth} depth={depth - 1} />
))}
</View>
);
};

const styles = StyleSheet.create({
container: {
borderWidth: 1,
padding: 1,
},
});
7 changes: 7 additions & 0 deletions examples/default/src/navigation/HomeStack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { UserStepsScreen } from '../screens/user-steps/UserStepsScreen';
import { BasicComponentsScreen } from '../screens/user-steps/BasicComponentsScreen';
import { ScrollViewScreen } from '../screens/user-steps/ScrollViewScreen';
import { FlatListScreen } from '../screens/user-steps/FlatListScreen';
import { ComplexViewsScreen } from '../screens/user-steps/ComplexViewsScreen';
import { SectionListScreen } from '../screens/user-steps/SectionListScreen';
import { GesturesScreen } from '../screens/user-steps/GesturesScreen';

Expand All @@ -26,6 +27,7 @@ export type HomeStackParamList = {
BasicComponents: undefined;
ScrollView: undefined;
FlatList: undefined;
ComplexViews: undefined;
SectionList: undefined;
Gestures: undefined;
};
Expand Down Expand Up @@ -63,6 +65,11 @@ export const HomeStackNavigator: React.FC = () => {
component={BasicComponentsScreen}
options={{ title: 'Basic Components' }}
/>
<HomeStack.Screen
name="ComplexViews"
component={ComplexViewsScreen}
options={{ title: 'Basic Components' }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this screen title should be something like "Complex Views" instead of "Basic Components"

Copy link
Contributor Author

@abdelhamid-f-nasser abdelhamid-f-nasser Jan 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy Paste typo 😅.

/>
<HomeStack.Screen
name="ScrollView"
component={ScrollViewScreen}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
Pressable,
StyleSheet,
Text,
TextInput,
TouchableOpacity,
Switch,
useWindowDimensions,
Expand All @@ -18,6 +17,7 @@ import { Center, HStack, ScrollView, VStack } from 'native-base';
import { Screen } from '../../components/Screen';
import { Section } from '../../components/Section';
import { nativeBaseTheme } from '../../theme/nativeBaseTheme';
import { InputField } from '../../components/InputField';

/**
* A screen that demonstates the usage of user steps with basic React Native components.
Expand Down Expand Up @@ -55,7 +55,7 @@ export const BasicComponentsScreen: React.FC = () => {
</Section>

<Section title="Text Input">
<TextInput placeholder="Enter your name" style={styles.textInput} />
<InputField placeholder="Enter your name" />
</Section>

<Section title="Button">
Expand Down
47 changes: 47 additions & 0 deletions examples/default/src/screens/user-steps/ComplexViewsScreen.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React, { useRef, useState } from 'react';

import { Screen } from '../../components/Screen';
import { Section } from '../../components/Section';
import { NestedView } from '../../components/NestedView';
import { Button } from 'react-native';
import { ScrollView, VStack } from 'native-base';
import { InputField } from '../../components/InputField';

export const ComplexViewsScreen: React.FC = () => {
const initialDepth = 10;
const initialBreadth = 2;

const depthRef = useRef(initialDepth);
const breadthRef = useRef(initialBreadth);

const [depth, setDepth] = useState(initialDepth);
const [breadth, setBreadth] = useState(initialBreadth);

function handleRender() {
setDepth(depthRef.current);
setBreadth(breadthRef.current);
}

return (
<Screen>
<ScrollView>
<Section title="Complex View">
<VStack space="xs">
<InputField
placeholder={`Depth (default: ${initialDepth})`}
keyboardType="numeric"
onChangeText={(text) => (depthRef.current = +text)}
/>
<InputField
placeholder={`Breadth (default: ${initialBreadth})`}
keyboardType="numeric"
onChangeText={(text) => (breadthRef.current = +text)}
/>
<Button title="Render" onPress={handleRender} />
<NestedView depth={depth} breadth={breadth} />
</VStack>
</Section>
</ScrollView>
</Screen>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const UserStepsScreen: React.FC<NativeStackScreenProps<HomeStackParamList
<ListTile title="Scroll View" onPress={() => navigation.navigate('ScrollView')} />
<ListTile title="Flat List" onPress={() => navigation.navigate('FlatList')} />
<ListTile title="Section List" onPress={() => navigation.navigate('SectionList')} />
<ListTile title="Complex Views" onPress={() => navigation.navigate('ComplexViews')} />
<ListTile title="Gestures" onPress={() => navigation.navigate('Gestures')} />
</Screen>
);
Expand Down