Skip to content

Commit 9effce3

Browse files
authored
Merge pull request #19 from xiaoyudesu/sig
feat: 新增drawer库
2 parents 255059c + 310371c commit 9effce3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+3943
-0
lines changed

packages/drawer/CHANGELOG.md

Lines changed: 1003 additions & 0 deletions
Large diffs are not rendered by default.

packages/drawer/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 React Navigation Contributors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

packages/drawer/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# `@react-navigation/drawer`
2+
3+
Drawer navigator for React Navigation following Material Design guidelines.
4+
5+
Installation instructions and documentation can be found on the [React Navigation website](https://reactnavigation.org/docs/drawer-navigator/).

packages/drawer/package.json

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
{
2+
"name": "@react-native-oh-tpl/drawer",
3+
"description": "Drawer navigator component with animated transitions and gesturess",
4+
"version": "6.7.1",
5+
"harmony": {
6+
"alias": "@react-navigation/drawer"
7+
},
8+
"keywords": [
9+
"react-native-component",
10+
"react-component",
11+
"react-native",
12+
"react-navigation",
13+
"harmony",
14+
"ios",
15+
"android",
16+
"material",
17+
"drawer"
18+
],
19+
"license": "MIT",
20+
"repository": {
21+
"type": "git",
22+
"url": "https://github.com/react-native-oh-library/react-navigation.git",
23+
"directory": "packages/drawer"
24+
},
25+
"bugs": {
26+
"url": "https://github.com/react-navigation/react-navigation/issues"
27+
},
28+
"homepage": "https://reactnavigation.org/docs/drawer-navigator/",
29+
"main": "lib/commonjs/index.js",
30+
"react-native": "src/index.tsx",
31+
"source": "src/index.tsx",
32+
"module": "lib/module/index.js",
33+
"types": "lib/typescript/src/index.d.ts",
34+
"files": [
35+
"harmony",
36+
"src",
37+
"lib",
38+
"!**/__tests__"
39+
],
40+
"sideEffects": false,
41+
"publishConfig": {
42+
"access": "public"
43+
},
44+
"scripts": {
45+
"prepack": "bob build",
46+
"clean": "del lib"
47+
},
48+
"dependencies": {
49+
"@react-navigation/elements": "^1.3.30",
50+
"color": "^4.2.3",
51+
"warn-once": "^0.1.0"
52+
},
53+
"devDependencies": {
54+
"@react-navigation/native": "^6.1.17",
55+
"@testing-library/react-native": "^11.5.0",
56+
"@types/react": "~18.0.27",
57+
"@types/react-native": "~0.71.3",
58+
"del-cli": "^5.0.0",
59+
"react": "18.2.0",
60+
"react-native": "0.71.8",
61+
"react-native-builder-bob": "^0.20.4",
62+
"react-native-gesture-handler": "~2.9.0",
63+
"react-native-reanimated": "~2.14.4",
64+
"react-native-safe-area-context": "4.5.0",
65+
"react-native-screens": "~3.29.0",
66+
"typescript": "^4.9.4"
67+
},
68+
"peerDependencies": {
69+
"@react-navigation/native": "^6.0.0",
70+
"react": "*",
71+
"react-native": "*",
72+
"react-native-gesture-handler": ">= 1.0.0",
73+
"react-native-reanimated": ">= 1.0.0",
74+
"react-native-safe-area-context": ">= 3.0.0",
75+
"react-native-screens": ">= 3.0.0"
76+
},
77+
"react-native-builder-bob": {
78+
"source": "src",
79+
"output": "lib",
80+
"targets": [
81+
"commonjs",
82+
"module",
83+
[
84+
"typescript",
85+
{
86+
"project": "tsconfig.build.json"
87+
}
88+
]
89+
]
90+
}
91+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { NavigationContainer, ParamListBase } from '@react-navigation/native';
2+
import { fireEvent, render } from '@testing-library/react-native';
3+
import * as React from 'react';
4+
import { Button, Text, View } from 'react-native';
5+
6+
import { createDrawerNavigator, DrawerScreenProps } from '../index';
7+
8+
it('renders a drawer navigator with screens', async () => {
9+
const Test = ({ route, navigation }: DrawerScreenProps<ParamListBase>) => (
10+
<View>
11+
<Text>Screen {route.name}</Text>
12+
<Button onPress={() => navigation.navigate('A')} title="Go to A" />
13+
<Button onPress={() => navigation.navigate('B')} title="Go to B" />
14+
</View>
15+
);
16+
17+
const Drawer = createDrawerNavigator();
18+
19+
const { findByText, queryByText } = render(
20+
<NavigationContainer>
21+
<Drawer.Navigator>
22+
<Drawer.Screen name="A" component={Test} />
23+
<Drawer.Screen name="B" component={Test} />
24+
</Drawer.Navigator>
25+
</NavigationContainer>
26+
);
27+
28+
expect(queryByText('Screen A')).not.toBeNull();
29+
expect(queryByText('Screen B')).toBeNull();
30+
31+
fireEvent(await findByText('Go to B'), 'press');
32+
33+
expect(queryByText('Screen B')).not.toBeNull();
34+
});

packages/drawer/src/index.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Navigators
3+
*/
4+
export { default as createDrawerNavigator } from './navigators/createDrawerNavigator';
5+
6+
/**
7+
* Views
8+
*/
9+
export { default as DrawerContent } from './views/DrawerContent';
10+
export { default as DrawerContentScrollView } from './views/DrawerContentScrollView';
11+
export { default as DrawerItem } from './views/DrawerItem';
12+
export { default as DrawerItemList } from './views/DrawerItemList';
13+
export { default as DrawerToggleButton } from './views/DrawerToggleButton';
14+
export { default as DrawerView } from './views/DrawerView';
15+
16+
/**
17+
* Utilities
18+
*/
19+
export { default as DrawerGestureContext } from './utils/DrawerGestureContext';
20+
export { default as DrawerProgressContext } from './utils/DrawerProgressContext';
21+
export { default as DrawerStatusContext } from './utils/DrawerStatusContext';
22+
export { default as getDrawerStatusFromState } from './utils/getDrawerStatusFromState';
23+
export { default as useDrawerProgress } from './utils/useDrawerProgress';
24+
export { default as useDrawerStatus } from './utils/useDrawerStatus';
25+
26+
/**
27+
* Types
28+
*/
29+
export type {
30+
DrawerContentComponentProps,
31+
DrawerHeaderProps,
32+
DrawerNavigationEventMap,
33+
DrawerNavigationOptions,
34+
DrawerNavigationProp,
35+
DrawerScreenProps,
36+
} from './types';
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import {
2+
createNavigatorFactory,
3+
DefaultNavigatorOptions,
4+
DrawerActionHelpers,
5+
DrawerNavigationState,
6+
DrawerRouter,
7+
DrawerRouterOptions,
8+
DrawerStatus,
9+
ParamListBase,
10+
useNavigationBuilder,
11+
} from '@react-navigation/native';
12+
import * as React from 'react';
13+
import warnOnce from 'warn-once';
14+
15+
import type {
16+
DrawerNavigationConfig,
17+
DrawerNavigationEventMap,
18+
DrawerNavigationOptions,
19+
} from '../types';
20+
import DrawerView from '../views/DrawerView';
21+
22+
type Props = DefaultNavigatorOptions<
23+
ParamListBase,
24+
DrawerNavigationState<ParamListBase>,
25+
DrawerNavigationOptions,
26+
DrawerNavigationEventMap
27+
> &
28+
DrawerRouterOptions &
29+
DrawerNavigationConfig;
30+
31+
function DrawerNavigator({
32+
id,
33+
initialRouteName,
34+
defaultStatus: customDefaultStatus,
35+
backBehavior,
36+
children,
37+
screenListeners,
38+
screenOptions,
39+
...restWithDeprecated
40+
}: Props) {
41+
const {
42+
// @ts-expect-error: openByDefault is deprecated
43+
openByDefault,
44+
// @ts-expect-error: lazy is deprecated
45+
lazy,
46+
// @ts-expect-error: drawerContentOptions is deprecated
47+
drawerContentOptions,
48+
...rest
49+
} = restWithDeprecated;
50+
51+
let defaultScreenOptions: DrawerNavigationOptions = {};
52+
53+
if (drawerContentOptions) {
54+
Object.assign(defaultScreenOptions, {
55+
drawerPosition: drawerContentOptions.drawerPosition,
56+
drawerType: drawerContentOptions.drawerType,
57+
swipeEdgeWidth: drawerContentOptions.edgeWidth,
58+
drawerHideStatusBarOnOpen: drawerContentOptions.hideStatusBar,
59+
keyboardDismissMode: drawerContentOptions.keyboardDismissMode,
60+
swipeMinDistance: drawerContentOptions.minSwipeDistance,
61+
overlayColor: drawerContentOptions.overlayColor,
62+
drawerStatusBarAnimation: drawerContentOptions.statusBarAnimation,
63+
gestureHandlerProps: drawerContentOptions.gestureHandlerProps,
64+
});
65+
66+
(
67+
Object.keys(defaultScreenOptions) as (keyof DrawerNavigationOptions)[]
68+
).forEach((key) => {
69+
if (defaultScreenOptions[key] === undefined) {
70+
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
71+
delete defaultScreenOptions[key];
72+
}
73+
});
74+
75+
warnOnce(
76+
drawerContentOptions,
77+
`Drawer Navigator: 'drawerContentOptions' is deprecated. Migrate the options to 'screenOptions' instead.\n\nPlace the following in 'screenOptions' in your code to keep current behavior:\n\n${JSON.stringify(
78+
defaultScreenOptions,
79+
null,
80+
2
81+
)}\n\nSee https://reactnavigation.org/docs/drawer-navigator#options for more details.`
82+
);
83+
}
84+
85+
if (typeof lazy === 'boolean') {
86+
defaultScreenOptions.lazy = lazy;
87+
88+
warnOnce(
89+
true,
90+
`Drawer Navigator: 'lazy' in props is deprecated. Move it to 'screenOptions' instead.\n\nSee https://reactnavigation.org/docs/drawer-navigator/#lazy for more details.`
91+
);
92+
}
93+
94+
if (typeof openByDefault === 'boolean') {
95+
warnOnce(
96+
true,
97+
`Drawer Navigator: 'openByDefault' is deprecated. Use 'defaultStatus' and set it to 'open' or 'closed' instead.\n\nSee https://reactnavigation.org/docs/drawer-navigator/#defaultstatus for more details.`
98+
);
99+
}
100+
101+
const defaultStatus: DrawerStatus =
102+
customDefaultStatus !== undefined
103+
? customDefaultStatus
104+
: openByDefault
105+
? 'open'
106+
: 'closed';
107+
108+
const { state, descriptors, navigation, NavigationContent } =
109+
useNavigationBuilder<
110+
DrawerNavigationState<ParamListBase>,
111+
DrawerRouterOptions,
112+
DrawerActionHelpers<ParamListBase>,
113+
DrawerNavigationOptions,
114+
DrawerNavigationEventMap
115+
>(DrawerRouter, {
116+
id,
117+
initialRouteName,
118+
defaultStatus,
119+
backBehavior,
120+
children,
121+
screenListeners,
122+
screenOptions,
123+
defaultScreenOptions,
124+
});
125+
126+
return (
127+
<NavigationContent>
128+
<DrawerView
129+
{...rest}
130+
defaultStatus={defaultStatus}
131+
state={state}
132+
descriptors={descriptors}
133+
navigation={navigation}
134+
/>
135+
</NavigationContent>
136+
);
137+
}
138+
139+
export default createNavigatorFactory<
140+
DrawerNavigationState<ParamListBase>,
141+
DrawerNavigationOptions,
142+
DrawerNavigationEventMap,
143+
typeof DrawerNavigator
144+
>(DrawerNavigator);

0 commit comments

Comments
 (0)