Skip to content

Commit 3ecbe64

Browse files
committed
feat(example-app): ⚡ add grouping of Drawer Screens
1 parent 08cd1e4 commit 3ecbe64

File tree

1 file changed

+135
-48
lines changed

1 file changed

+135
-48
lines changed

example/src/AppRoot.tsx

Lines changed: 135 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
import React from "react";
2-
import { createDrawerNavigator } from "@react-navigation/drawer";
1+
import React, { ReactNode } from "react";
2+
import {
3+
createDrawerNavigator,
4+
DrawerContentComponentProps,
5+
DrawerContentScrollView,
6+
DrawerItem,
7+
} from "@react-navigation/drawer";
8+
9+
import { Divider } from "../../src/components";
10+
import { Box, Text } from "../../src/primitives";
11+
import { useTheme } from "../../src/theme";
312

413
import {
514
AvatarGroupScreen,
@@ -25,101 +34,179 @@ import {
2534

2635
const Drawer = createDrawerNavigator();
2736

37+
const CustomDrawerSection = (props: DrawerContentComponentProps) => {
38+
const { state, descriptors, navigation } = props;
39+
let lastGroupName = "";
40+
let newGroup = true;
41+
const tailwind = useTheme();
42+
return (
43+
<DrawerContentScrollView {...props}>
44+
{state.routes.map(route => {
45+
const {
46+
title,
47+
// @ts-ignore
48+
groupName,
49+
drawerActiveTintColor,
50+
drawerInactiveTintColor,
51+
} = descriptors[route.key].options;
52+
if (lastGroupName !== groupName) {
53+
newGroup = true;
54+
lastGroupName = groupName;
55+
} else {
56+
newGroup = false;
57+
}
58+
return (
59+
<Box key={title}>
60+
{newGroup ? (
61+
<Box>
62+
<Divider
63+
labelPosition="center"
64+
label={groupName}
65+
dividerStyle={tailwind.style("my-4 bg-blue-700")}
66+
/>
67+
</Box>
68+
) : null}
69+
<DrawerItem
70+
key={route.key}
71+
label={({ color }) =>
72+
(<Text style={[{ color }]}>{title}</Text>) as ReactNode
73+
}
74+
focused={
75+
state.index ===
76+
state.routes.findIndex(e => e.name === route.name)
77+
}
78+
activeTintColor={drawerActiveTintColor}
79+
inactiveTintColor={drawerInactiveTintColor}
80+
onPress={() => navigation.navigate(route.name)}
81+
/>
82+
</Box>
83+
);
84+
})}
85+
</DrawerContentScrollView>
86+
);
87+
};
88+
2889
const AppRoot = () => {
2990
return (
30-
<Drawer.Navigator initialRouteName="AvatarScreen">
91+
<Drawer.Navigator
92+
drawerContent={CustomDrawerSection}
93+
initialRouteName="AvatarScreen"
94+
>
3195
<Drawer.Screen
32-
options={{ title: "Avatar" }}
96+
// @ts-ignore
97+
options={{ title: "Avatar", groupName: "PRIMITIVES" }}
3398
name="AvatarScreen"
3499
component={AvatarScreen}
35100
/>
36101
<Drawer.Screen
37-
options={{ title: "Avatar Group" }}
102+
// @ts-ignore
103+
options={{ title: "Avatar Group", groupName: "PRIMITIVES" }}
38104
name="AvatarGroupScreen"
39105
component={AvatarGroupScreen}
40106
/>
41107
<Drawer.Screen
42-
options={{ title: "Badge" }}
108+
// @ts-ignore
109+
options={{ title: "Badge", groupName: "PRIMITIVES" }}
43110
name="BadgeScreen"
44111
component={BadgeScreen}
45112
/>
46113
<Drawer.Screen
47-
options={{ title: "Button" }}
114+
// @ts-ignore
115+
options={{ title: "Button", groupName: "PRIMITIVES" }}
48116
name="ButtonScreen"
49117
component={ButtonScreen}
50118
/>
51119
<Drawer.Screen
52-
options={{ title: "Checkbox" }}
53-
name="CheckboxScreen"
54-
component={CheckboxScreen}
120+
// @ts-ignore
121+
options={{ title: "Tag ", groupName: "PRIMITIVES" }}
122+
name="TagScreen"
123+
component={TagScreen}
55124
/>
56125
<Drawer.Screen
57-
options={{ title: "Checkbox Group" }}
58-
name="CheckboxGroupScreen"
59-
component={CheckboxGroupScreen}
126+
// @ts-ignore
127+
options={{ title: "Divider", groupName: "PRIMITIVES" }}
128+
name="DividerScreen"
129+
component={DividerScreen}
60130
/>
61131
<Drawer.Screen
62-
options={{ title: "Circular Progress" }}
63-
name="CircularProgressScreen"
64-
component={CircularProgressScreen}
132+
// @ts-ignore
133+
options={{ title: "Checkbox", groupName: "FORMS" }}
134+
name="CheckboxScreen"
135+
component={CheckboxScreen}
65136
/>
66137
<Drawer.Screen
67-
options={{ title: "Divider" }}
68-
name="DividerScreen"
69-
component={DividerScreen}
138+
// @ts-ignore
139+
options={{ title: "Checkbox Group", groupName: "FORMS" }}
140+
name="CheckboxGroupScreen"
141+
component={CheckboxGroupScreen}
70142
/>
71143
<Drawer.Screen
72-
options={{ title: "Input" }}
144+
// @ts-ignore
145+
options={{ title: "Input", groupName: "FORMS" }}
73146
name="InputScreen"
74147
component={InputScreen}
75148
/>
76149
<Drawer.Screen
77-
options={{ title: "Meter" }}
78-
name="MeterComponentScreen"
79-
component={MeterComponentScreen}
150+
// @ts-ignore
151+
options={{ title: "Radio", groupName: "FORMS" }}
152+
name="RadioComponentScreen"
153+
component={RadioScreen}
80154
/>
81155
<Drawer.Screen
82-
options={{ title: "Select" }}
83-
name="SelectScreen"
84-
component={SelectScreen}
156+
// @ts-ignore
157+
options={{ title: "Slider", groupName: "FORMS" }}
158+
name="SliderComponentScreen"
159+
component={SliderComponentScreen}
85160
/>
86161
<Drawer.Screen
87-
options={{ title: "Spinner" }}
88-
name="SpinnerScreen"
89-
component={SpinnerScreen}
162+
// @ts-ignore
163+
options={{ title: "Switch", groupName: "FORMS" }}
164+
name="SwitchComponentScreen"
165+
component={SwitchComponentScreen}
90166
/>
167+
91168
<Drawer.Screen
92-
options={{ title: "Progress " }}
93-
name="ProgressScreen"
94-
component={ProgressScreen}
169+
// @ts-ignore
170+
options={{ title: "Text Area", groupName: "FORMS" }}
171+
name="TextAreaScreen"
172+
component={TextAreaScreen}
95173
/>
174+
96175
<Drawer.Screen
97-
options={{ title: "Radio" }}
98-
name="RadioComponentScreen"
99-
component={RadioScreen}
176+
// @ts-ignore
177+
options={{ title: "Circular Progress", groupName: "FEEDBACK" }}
178+
name="CircularProgressScreen"
179+
component={CircularProgressScreen}
100180
/>
181+
101182
<Drawer.Screen
102-
options={{ title: "Slider" }}
103-
name="SliderComponentScreen"
104-
component={SliderComponentScreen}
183+
// @ts-ignore
184+
options={{ title: "Meter", groupName: "FEEDBACK" }}
185+
name="MeterComponentScreen"
186+
component={MeterComponentScreen}
105187
/>
106188
<Drawer.Screen
107-
options={{ title: "Switch" }}
108-
name="SwitchComponentScreen"
109-
component={SwitchComponentScreen}
189+
// @ts-ignore
190+
options={{ title: "Select", groupName: "FEEDBACK" }}
191+
name="SelectScreen"
192+
component={SelectScreen}
110193
/>
111194
<Drawer.Screen
112-
options={{ title: "Tag " }}
113-
name="TagScreen"
114-
component={TagScreen}
195+
// @ts-ignore
196+
options={{ title: "Spinner", groupName: "FEEDBACK" }}
197+
name="SpinnerScreen"
198+
component={SpinnerScreen}
115199
/>
116200
<Drawer.Screen
117-
options={{ title: "Text Area" }}
118-
name="TextAreaScreen"
119-
component={TextAreaScreen}
201+
// @ts-ignore
202+
options={{ title: "Progress ", groupName: "FEEDBACK" }}
203+
name="ProgressScreen"
204+
component={ProgressScreen}
120205
/>
206+
121207
<Drawer.Screen
122-
options={{ title: "Tooltip" }}
208+
// @ts-ignore
209+
options={{ title: "Tooltip", groupName: "POPUPS" }}
123210
name="TooltipScreen"
124211
component={TooltipScreen}
125212
/>

0 commit comments

Comments
 (0)