Skip to content

Commit 94cdc17

Browse files
abdelhamid-f-nasserHeshamMegid
authored andcommitted
feat(example): add nested complex views (#1111)
Jira ID: MOB-13737
1 parent f77c94c commit 94cdc17

File tree

7 files changed

+129
-2
lines changed

7 files changed

+129
-2
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,6 @@ android/keystores/debug.keystore
6464

6565
# Vscode local history
6666
.history/
67+
68+
# .idea run configurations
69+
/.run/*
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React, { forwardRef } from 'react';
2+
3+
import { KeyboardTypeOptions, StyleSheet, TextInput } from 'react-native';
4+
5+
interface InputFieldProps {
6+
placeholder?: string;
7+
value?: string;
8+
onChangeText?: (text: string) => void;
9+
keyboardType?: KeyboardTypeOptions;
10+
}
11+
12+
export const InputField = forwardRef<TextInput, InputFieldProps>(
13+
({ placeholder, value, onChangeText, keyboardType, ...restProps }, ref) => {
14+
return (
15+
<TextInput
16+
ref={ref}
17+
placeholder={placeholder}
18+
style={styles.textInput}
19+
keyboardType={keyboardType}
20+
value={value}
21+
onChangeText={onChangeText}
22+
{...restProps}
23+
/>
24+
);
25+
},
26+
);
27+
28+
const styles = StyleSheet.create({
29+
textInput: {
30+
backgroundColor: 'white',
31+
borderWidth: 1,
32+
borderColor: '#ccc',
33+
paddingVertical: 16,
34+
paddingHorizontal: 24,
35+
fontSize: 16,
36+
borderRadius: 5,
37+
},
38+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React from 'react';
2+
3+
import { Text } from 'native-base';
4+
import { StyleSheet, View } from 'react-native';
5+
6+
interface NestedViewProps {
7+
children?: React.ReactNode;
8+
depth: number;
9+
breadth?: number;
10+
}
11+
12+
export const NestedView: React.FC<NestedViewProps> = ({ depth, breadth = 1, children }) => {
13+
if (!depth) {
14+
return <>{children}</>;
15+
}
16+
return (
17+
<View style={styles.container}>
18+
<Text>{depth}</Text>
19+
{new Array(breadth).fill(null).map((_, index) => (
20+
<NestedView key={index} breadth={breadth} depth={depth - 1} />
21+
))}
22+
</View>
23+
);
24+
};
25+
26+
const styles = StyleSheet.create({
27+
container: {
28+
borderWidth: 1,
29+
padding: 1,
30+
},
31+
});

examples/default/src/navigation/HomeStack.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { UserStepsScreen } from '../screens/user-steps/UserStepsScreen';
1212
import { BasicComponentsScreen } from '../screens/user-steps/BasicComponentsScreen';
1313
import { ScrollViewScreen } from '../screens/user-steps/ScrollViewScreen';
1414
import { FlatListScreen } from '../screens/user-steps/FlatListScreen';
15+
import { ComplexViewsScreen } from '../screens/user-steps/ComplexViewsScreen';
1516
import { SectionListScreen } from '../screens/user-steps/SectionListScreen';
1617
import { GesturesScreen } from '../screens/user-steps/GesturesScreen';
1718

@@ -26,6 +27,7 @@ export type HomeStackParamList = {
2627
BasicComponents: undefined;
2728
ScrollView: undefined;
2829
FlatList: undefined;
30+
ComplexViews: undefined;
2931
SectionList: undefined;
3032
Gestures: undefined;
3133
};
@@ -63,6 +65,11 @@ export const HomeStackNavigator: React.FC = () => {
6365
component={BasicComponentsScreen}
6466
options={{ title: 'Basic Components' }}
6567
/>
68+
<HomeStack.Screen
69+
name="ComplexViews"
70+
component={ComplexViewsScreen}
71+
options={{ title: 'Basic Components' }}
72+
/>
6673
<HomeStack.Screen
6774
name="ScrollView"
6875
component={ScrollViewScreen}

examples/default/src/screens/user-steps/BasicComponentsScreen.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
Pressable,
77
StyleSheet,
88
Text,
9-
TextInput,
109
TouchableOpacity,
1110
Switch,
1211
useWindowDimensions,
@@ -18,6 +17,7 @@ import { Center, HStack, ScrollView, VStack } from 'native-base';
1817
import { Screen } from '../../components/Screen';
1918
import { Section } from '../../components/Section';
2019
import { nativeBaseTheme } from '../../theme/nativeBaseTheme';
20+
import { InputField } from '../../components/InputField';
2121

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

5757
<Section title="Text Input">
58-
<TextInput placeholder="Enter your name" style={styles.textInput} />
58+
<InputField placeholder="Enter your name" />
5959
</Section>
6060

6161
<Section title="Button">
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import React, { useRef, useState } from 'react';
2+
3+
import { Screen } from '../../components/Screen';
4+
import { Section } from '../../components/Section';
5+
import { NestedView } from '../../components/NestedView';
6+
import { Button } from 'react-native';
7+
import { ScrollView, VStack } from 'native-base';
8+
import { InputField } from '../../components/InputField';
9+
10+
export const ComplexViewsScreen: React.FC = () => {
11+
const initialDepth = 10;
12+
const initialBreadth = 2;
13+
14+
const depthRef = useRef(initialDepth);
15+
const breadthRef = useRef(initialBreadth);
16+
17+
const [depth, setDepth] = useState(initialDepth);
18+
const [breadth, setBreadth] = useState(initialBreadth);
19+
20+
function handleRender() {
21+
setDepth(depthRef.current);
22+
setBreadth(breadthRef.current);
23+
}
24+
25+
return (
26+
<Screen>
27+
<ScrollView>
28+
<Section title="Complex View">
29+
<VStack space="xs">
30+
<InputField
31+
placeholder={`Depth (default: ${initialDepth})`}
32+
keyboardType="numeric"
33+
onChangeText={(text) => (depthRef.current = +text)}
34+
/>
35+
<InputField
36+
placeholder={`Breadth (default: ${initialBreadth})`}
37+
keyboardType="numeric"
38+
onChangeText={(text) => (breadthRef.current = +text)}
39+
/>
40+
<Button title="Render" onPress={handleRender} />
41+
<NestedView depth={depth} breadth={breadth} />
42+
</VStack>
43+
</Section>
44+
</ScrollView>
45+
</Screen>
46+
);
47+
};

examples/default/src/screens/user-steps/UserStepsScreen.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export const UserStepsScreen: React.FC<NativeStackScreenProps<HomeStackParamList
1414
<ListTile title="Scroll View" onPress={() => navigation.navigate('ScrollView')} />
1515
<ListTile title="Flat List" onPress={() => navigation.navigate('FlatList')} />
1616
<ListTile title="Section List" onPress={() => navigation.navigate('SectionList')} />
17+
<ListTile title="Complex Views" onPress={() => navigation.navigate('ComplexViews')} />
1718
<ListTile title="Gestures" onPress={() => navigation.navigate('Gestures')} />
1819
</Screen>
1920
);

0 commit comments

Comments
 (0)