Skip to content

Commit 653fd3e

Browse files
committed
feat: Add more examples
1 parent 68f3ca7 commit 653fd3e

7 files changed

Lines changed: 144 additions & 91 deletions

File tree

example/src/Navigation.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import ExampleInitialSetScreen from './screens/ExampleInitialSet';
44
import ExamplesScreen from './screens/Examples';
55

66
export const RootStack = createNativeStackNavigator({
7-
initialRouteName: 'SegmentedControlExample',
7+
initialRouteName: 'Examples',
88
screens: {
9-
SegmentedControlExample: {
9+
Examples: {
1010
screen: ExamplesScreen,
1111
options: () => ({
1212
title: 'Examples',
@@ -15,7 +15,7 @@ export const RootStack = createNativeStackNavigator({
1515
ExampleInitialSet: {
1616
screen: ExampleInitialSetScreen,
1717
options: () => ({
18-
title: 'Example Initial Based on Route',
18+
title: 'Preselected option',
1919
}),
2020
},
2121
},

example/src/examples/Align.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default function Align({ variant }: AlignProps) {
3737
{ value: 'First', label: 'First' },
3838
{ value: 'Second', label: 'Second' },
3939
]}
40-
defaultOption="Second"
40+
defaultOption="First"
4141
variant={variant}
4242
align="center"
4343
containerHeight={28}

example/src/examples/DayOfTime.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ const styles = StyleSheet.create({
6464
justifyContent: 'center',
6565
alignItems: 'center',
6666
},
67-
6867
containerStyle: {
6968
backgroundColor: 'rgba(59, 130, 246, 0.08)',
7069
},
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import {
2+
MultiswitchController,
3+
type ControllerVariant,
4+
} from 'react-native-multiswitch-controller';
5+
import ExampleWrapper from './ExampleWrapper';
6+
import { Button, StyleSheet, View } from 'react-native';
7+
import { useState } from 'react';
8+
9+
const mockLanguages: Record<
10+
'en' | 'de',
11+
{ food: string; drink: string; dessert: string }
12+
> = {
13+
en: {
14+
food: 'Butterfly',
15+
drink: 'Cheese',
16+
dessert: 'Lettuce',
17+
},
18+
de: {
19+
food: 'Schmetterling',
20+
drink: 'Käse',
21+
dessert: 'Salat',
22+
},
23+
};
24+
25+
type DynamicLabelsProps = {
26+
variant: ControllerVariant;
27+
};
28+
29+
export default function DynamicLabels({ variant }: DynamicLabelsProps) {
30+
const [language, setLanguage] = useState<keyof typeof mockLanguages>('en');
31+
32+
return (
33+
<ExampleWrapper title="Adjust labels width dynamically">
34+
<MultiswitchController<'food' | 'drink' | 'dessert'>
35+
options={[
36+
{ value: 'food', label: mockLanguages[language].food },
37+
{ value: 'drink', label: mockLanguages[language].drink },
38+
{ value: 'dessert', label: mockLanguages[language].dessert },
39+
]}
40+
variant={variant}
41+
defaultOption="food"
42+
containerHeight={48}
43+
optionHeight={36}
44+
optionGap={16}
45+
inactiveTextStyle={styles.inactiveTextStyle}
46+
containerStyle={styles.containerStyle}
47+
activeOptionContainerStyle={
48+
variant === 'tabs'
49+
? styles.activeOptionContainerStyleTabs
50+
: styles.activeOptionContainerStyleSegmentedControl
51+
}
52+
activeTextStyle={
53+
variant === 'tabs'
54+
? styles.activeTextStyleTabs
55+
: styles.activeTextStyleSegmentedControl
56+
}
57+
/>
58+
<View style={styles.buttonsContainer}>
59+
<Button
60+
title="Toggle language"
61+
onPress={() => {
62+
setLanguage(language === 'en' ? 'de' : 'en');
63+
}}
64+
/>
65+
</View>
66+
</ExampleWrapper>
67+
);
68+
}
69+
70+
const styles = StyleSheet.create({
71+
buttonsContainer: {
72+
flexDirection: 'row',
73+
gap: 10,
74+
justifyContent: 'center',
75+
alignItems: 'center',
76+
},
77+
inactiveTextStyle: {
78+
fontSize: 12,
79+
color: 'rgb(30, 64, 175)',
80+
},
81+
containerStyle: {
82+
backgroundColor: 'rgba(30, 64, 175, 0.08)',
83+
},
84+
activeOptionContainerStyleTabs: {
85+
backgroundColor: 'rgb(30, 64, 175)',
86+
height: 10,
87+
},
88+
activeOptionContainerStyleSegmentedControl: {
89+
backgroundColor: 'rgb(30, 64, 175)',
90+
},
91+
activeTextStyleTabs: {
92+
color: 'rgb(30, 64, 175)',
93+
},
94+
activeTextStyleSegmentedControl: {
95+
color: '#fff',
96+
},
97+
});
Lines changed: 31 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,50 @@
1-
import { LinearGradient } from 'expo-linear-gradient';
2-
import { Button, StyleSheet, Text, View } from 'react-native';
1+
import { Button } from 'react-native';
32
import { MultiswitchController } from 'react-native-multiswitch-controller';
43

54
import {
65
useNavigation,
76
type StaticScreenProps,
87
} from '@react-navigation/native';
9-
import type { TimeOfDay } from '../types';
8+
import ExampleWrapper from '../examples/ExampleWrapper';
9+
import type { FavoriteDrink } from '../types';
1010

1111
type ExampleInitialSetScreenProps = StaticScreenProps<{
12-
timeOfDay: TimeOfDay;
12+
favoriteDrink: FavoriteDrink;
1313
}>;
1414

1515
export default function ExampleInitialSetScreen({
1616
route,
1717
}: ExampleInitialSetScreenProps) {
18-
const { timeOfDay } = route.params;
18+
const { favoriteDrink } = route.params;
1919

2020
const navigation = useNavigation();
2121

2222
return (
23-
<LinearGradient colors={['#FFF5E6', '#FFE4B5']} style={styles.container}>
24-
<View style={styles.exampleContainer}>
25-
<Text style={styles.title}>Time of Day</Text>
26-
<MultiswitchController
27-
options={[
28-
{ value: 'morning', label: '🌅' },
29-
{ value: 'afternoon', label: '☀️' },
30-
{ value: 'evening', label: '🌇' },
31-
{ value: 'night', label: '🌙' },
32-
]}
33-
defaultOption="morning"
34-
variant="tabs"
35-
onChangeOption={(value) => {
36-
console.log(
37-
'State ExampleInitialSetScreen onChangeOption changed:',
38-
value
39-
);
40-
}}
41-
styleProps={{
42-
inactiveBackgroundColor: 'rgba(59, 130, 246, 0.08)',
43-
activeBackgroundColor: 'rgb(37, 99, 235)',
44-
}}
45-
/>
46-
<Button
47-
title="Go to SegmentedControlExample"
48-
onPress={() => {
49-
navigation.navigate(
50-
'SegmentedControlExample',
51-
{
52-
timeOfDay: 'evening',
53-
},
54-
{ pop: true }
55-
);
56-
}}
57-
/>
58-
</View>
59-
<Text style={styles.title}>RERENDER TEST {timeOfDay}</Text>
60-
</LinearGradient>
23+
<ExampleWrapper title={`Preselected option: ${favoriteDrink}`}>
24+
<MultiswitchController<FavoriteDrink>
25+
variant="tabs"
26+
defaultOption={favoriteDrink}
27+
options={[
28+
{ value: 'water', label: '💧' },
29+
{ value: 'coffee', label: '☕️' },
30+
{ value: 'tea', label: '🍵' },
31+
{ value: 'juice', label: '🍹' },
32+
{ value: 'soda', label: '🥤' },
33+
]}
34+
optionGap={10}
35+
/>
36+
<Button
37+
title="Back with new value for first example: 'evening'"
38+
onPress={() => {
39+
navigation.navigate(
40+
'Examples',
41+
{
42+
timeOfDay: 'evening',
43+
},
44+
{ pop: true }
45+
);
46+
}}
47+
/>
48+
</ExampleWrapper>
6149
);
6250
}
63-
64-
const styles = StyleSheet.create({
65-
container: {
66-
flex: 1,
67-
gap: 16,
68-
padding: 10,
69-
},
70-
title: {
71-
fontSize: 18,
72-
padding: 10,
73-
},
74-
selectedText: {
75-
fontSize: 16,
76-
padding: 4,
77-
alignSelf: 'center',
78-
},
79-
bigContainer: {
80-
width: '100%',
81-
marginBottom: 20,
82-
},
83-
exampleContainer: {},
84-
smallText: {
85-
fontSize: 12,
86-
},
87-
stateReader: {
88-
marginTop: 20,
89-
padding: 10,
90-
backgroundColor: 'rgba(255, 255, 255, 0.8)',
91-
borderRadius: 8,
92-
},
93-
subtitle: {
94-
fontSize: 14,
95-
color: '#666',
96-
marginTop: 5,
97-
},
98-
button: {
99-
fontSize: 14,
100-
color: '#007AFF',
101-
marginTop: 10,
102-
textDecorationLine: 'underline',
103-
},
104-
});

example/src/screens/Examples.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import DayOfTime from '../examples/DayOfTime';
1414
import LongLabel from '../examples/LongLabel';
1515
import Scrollable from '../examples/Scrollable';
1616
import type { TimeOfDay } from '../types';
17+
import DynamicLabels from '../examples/DynamicLabels';
1718

1819
type ExamplesScreenProps = StaticScreenProps<{
1920
timeOfDay?: TimeOfDay;
@@ -72,7 +73,6 @@ export default function ExamplesScreen({ route }: ExamplesScreenProps) {
7273

7374
// onChangeOptionHandler - after animation is done
7475
const onChangeOptionHandler = useCallback((value: TimeOfDay) => {
75-
console.log('TEST onChangeOptionHandler - after animation is done', value);
7676
setTimeOfDay(value);
7777
}, []);
7878

@@ -90,6 +90,15 @@ export default function ExamplesScreen({ route }: ExamplesScreenProps) {
9090
<Align variant={variant} key={`Align-${variant}`} />
9191
<LongLabel variant={variant} key={`LongLabel-${variant}`} />
9292
<Scrollable variant={variant} key={`Scrollable-${variant}`} />
93+
<DynamicLabels variant={variant} key={`DynamicLabels-${variant}`} />
94+
<Button
95+
title="Navigate with preselected option"
96+
onPress={() => {
97+
navigation.navigate('ExampleInitialSet', {
98+
favoriteDrink: 'juice',
99+
});
100+
}}
101+
/>
93102
</LinearGradient>
94103
);
95104
}

example/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
export type TimeOfDay = 'morning' | 'afternoon' | 'evening' | 'night';
2+
3+
export type FavoriteDrink = 'water' | 'coffee' | 'tea' | 'juice' | 'soda';

0 commit comments

Comments
 (0)