mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-01-23 04:18:18 +08:00
Compare commits
19 Commits
@react-nav
...
@react-nav
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d5b4210eb2 | ||
|
|
38336b0290 | ||
|
|
fc37e93b5b | ||
|
|
093858b68b | ||
|
|
3703ab6353 | ||
|
|
7990cf2575 | ||
|
|
fb9d1837a1 | ||
|
|
935c588000 | ||
|
|
7d526e5881 | ||
|
|
2adccdef1d | ||
|
|
dee25057e8 | ||
|
|
9e1104c31f | ||
|
|
469ec31cc5 | ||
|
|
d26b77f9c9 | ||
|
|
1bbd6ac422 | ||
|
|
1a8281d37d | ||
|
|
b0a0857b0a | ||
|
|
4e07461526 | ||
|
|
f18231541b |
@@ -42,6 +42,13 @@ jobs:
|
|||||||
- store_artifacts:
|
- store_artifacts:
|
||||||
path: coverage
|
path: coverage
|
||||||
destination: coverage
|
destination: coverage
|
||||||
|
build-packages:
|
||||||
|
<<: *defaults
|
||||||
|
steps:
|
||||||
|
- attach_workspace:
|
||||||
|
at: ~/project
|
||||||
|
- run: |
|
||||||
|
yarn lerna run prepare
|
||||||
|
|
||||||
workflows:
|
workflows:
|
||||||
version: 2
|
version: 2
|
||||||
@@ -54,3 +61,6 @@ workflows:
|
|||||||
- unit-test:
|
- unit-test:
|
||||||
requires:
|
requires:
|
||||||
- install-dependencies
|
- install-dependencies
|
||||||
|
- build-packages:
|
||||||
|
requires:
|
||||||
|
- install-dependencies
|
||||||
|
|||||||
68
README.md
68
README.md
@@ -1,5 +1,9 @@
|
|||||||
# Rethinking Navigation
|
# Rethinking Navigation
|
||||||
|
|
||||||
|
[![Build Status][build-badge]][build]
|
||||||
|
[![Code Coverage][coverage-badge]][coverage]
|
||||||
|
[![MIT License][license-badge]][license]
|
||||||
|
|
||||||
An exploration of a component-first API for React Navigation for building more dynamic navigation solutions.
|
An exploration of a component-first API for React Navigation for building more dynamic navigation solutions.
|
||||||
|
|
||||||
## Considerations
|
## Considerations
|
||||||
@@ -31,6 +35,8 @@ Navigators bundle a router and a view which takes the navigation state and decid
|
|||||||
A simple navigator could look like this:
|
A simple navigator could look like this:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
import { createNavigator } from '@react-navigation/core';
|
||||||
|
|
||||||
function StackNavigator({ initialRouteName, children, ...rest }) {
|
function StackNavigator({ initialRouteName, children, ...rest }) {
|
||||||
// The `navigation` object contains the navigation state and some helpers (e.g. push, pop)
|
// The `navigation` object contains the navigation state and some helpers (e.g. push, pop)
|
||||||
// The `descriptors` object contains the screen options and a helper for rendering a screen
|
// The `descriptors` object contains the screen options and a helper for rendering a screen
|
||||||
@@ -127,8 +133,11 @@ It's also possible to disable bubbling of actions when dispatching them by addin
|
|||||||
## Basic usage
|
## Basic usage
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
import { createStackNavigator } from '@react-navigation/stack';
|
||||||
|
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
|
||||||
|
|
||||||
const Stack = createStackNavigator();
|
const Stack = createStackNavigator();
|
||||||
const Tab = createTabNavigator();
|
const Tab = createBottomTabNavigator();
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
return (
|
return (
|
||||||
@@ -210,6 +219,8 @@ function Profile({ navigation }) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
The `navigation.addListener` method returns a function to remove the listener which can be returned as the cleanup function in an effect.
|
||||||
|
|
||||||
Navigators can also emit custom events using the `emit` method in the `navigation` object passed:
|
Navigators can also emit custom events using the `emit` method in the `navigation` object passed:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
@@ -245,6 +256,8 @@ Sometimes we want to run side-effects when a screen is focused. A side effect ma
|
|||||||
To make this easier, the library exports a `useFocusEffect` hook:
|
To make this easier, the library exports a `useFocusEffect` hook:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
import { useFocusEffect } from '@react-navigation/core';
|
||||||
|
|
||||||
function Profile({ userId }) {
|
function Profile({ userId }) {
|
||||||
const [user, setUser] = React.useState(null);
|
const [user, setUser] = React.useState(null);
|
||||||
|
|
||||||
@@ -272,6 +285,10 @@ The `useFocusEffect` is analogous to React's `useEffect` hook. The only differen
|
|||||||
We might want to render different content based on the current focus state of the screen. The library exports a `useIsFocused` hook to make this easier:
|
We might want to render different content based on the current focus state of the screen. The library exports a `useIsFocused` hook to make this easier:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
import { useIsFocused } from '@react-navigation/core';
|
||||||
|
|
||||||
|
// ...
|
||||||
|
|
||||||
const isFocused = useIsFocused();
|
const isFocused = useIsFocused();
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -284,6 +301,10 @@ For proper UX in React Native, we need to respect platform behavior such as the
|
|||||||
When the back button on the device is pressed, we also want to navigate back in the focused navigator. The library exports a `useBackButton` hook to handle this:
|
When the back button on the device is pressed, we also want to navigate back in the focused navigator. The library exports a `useBackButton` hook to handle this:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
import { useBackButton } from '@react-navigation/native';
|
||||||
|
|
||||||
|
// ...
|
||||||
|
|
||||||
const ref = React.useRef();
|
const ref = React.useRef();
|
||||||
|
|
||||||
useBackButton(ref);
|
useBackButton(ref);
|
||||||
@@ -291,6 +312,24 @@ useBackButton(ref);
|
|||||||
return <NavigationContainer ref={ref}>{/* content */}</NavigationContainer>;
|
return <NavigationContainer ref={ref}>{/* content */}</NavigationContainer>;
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Scroll to top on tab button press
|
||||||
|
|
||||||
|
When there's a scroll view in a tab and the user taps on the already focused tab bar again, we might want to scroll to top in our scroll view. The library exports a `useScrollToTop` hook to handle this:
|
||||||
|
|
||||||
|
```js
|
||||||
|
import { useScrollToTop } from '@react-navigation/native';
|
||||||
|
|
||||||
|
// ...
|
||||||
|
|
||||||
|
const ref = React.useRef();
|
||||||
|
|
||||||
|
useScrollToTop(ref);
|
||||||
|
|
||||||
|
return <ScrollView ref={ref}>{/* content */}</ScrollView>;
|
||||||
|
```
|
||||||
|
|
||||||
|
The hook can accept a ref object to any view that has a `scrollTo` method.
|
||||||
|
|
||||||
### Deep-link integration
|
### Deep-link integration
|
||||||
|
|
||||||
To handle incoming links, we need to handle 2 scenarios:
|
To handle incoming links, we need to handle 2 scenarios:
|
||||||
@@ -325,6 +364,10 @@ For example, the path `/rooms/chat?user=jane` will be translated to a state obje
|
|||||||
The `useLinking` hooks makes it easier to handle incoming links:
|
The `useLinking` hooks makes it easier to handle incoming links:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
import { useLinking } from '@react-navigation/native';
|
||||||
|
|
||||||
|
// ...
|
||||||
|
|
||||||
const ref = React.useRef();
|
const ref = React.useRef();
|
||||||
|
|
||||||
const { getInitialState } = useLinking(ref, {
|
const { getInitialState } = useLinking(ref, {
|
||||||
@@ -451,10 +494,10 @@ Unfortunately it's not possible to verify that the type of children elements are
|
|||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
The project uses a monorepo structure for the packages managed by [yarn workspaces](https://yarnpkg.com/lang/en/docs/workspaces/) and [lerna](https://lerna.js.org). To get started with the project, run `lerna bootstrap` to install the required dependencies for each package:
|
The project uses a monorepo structure for the packages managed by [yarn workspaces](https://yarnpkg.com/lang/en/docs/workspaces/) and [lerna](https://lerna.js.org). To get started with the project, run `yarn` in the root directory to install the required dependencies for each package:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
lerna bootstrap
|
yarn
|
||||||
```
|
```
|
||||||
|
|
||||||
While developing, you can run the [example app](/example/) with [Expo](https://expo.io/) to test your changes:
|
While developing, you can run the [example app](/example/) with [Expo](https://expo.io/) to test your changes:
|
||||||
@@ -476,13 +519,7 @@ To fix formatting errors, run the following:
|
|||||||
yarn lint --fix
|
yarn lint --fix
|
||||||
```
|
```
|
||||||
|
|
||||||
Remember to add tests for your change if possible. To run tests, first build all the files:
|
Remember to add tests for your change if possible. Run the tests by:
|
||||||
|
|
||||||
```sh
|
|
||||||
lerna run prepare
|
|
||||||
```
|
|
||||||
|
|
||||||
Then run the tests:
|
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
yarn test
|
yarn test
|
||||||
@@ -493,7 +530,16 @@ yarn test
|
|||||||
To publish a new version, first we need to export a `GH_TOKEN` environment variable as mentioned [here](https://github.com/lerna/lerna/tree/master/commands/version#--create-release-type). Then run:
|
To publish a new version, first we need to export a `GH_TOKEN` environment variable as mentioned [here](https://github.com/lerna/lerna/tree/master/commands/version#--create-release-type). Then run:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
lerna publish
|
yarn lerna publish
|
||||||
```
|
```
|
||||||
|
|
||||||
This will automatically bump the version and publish the packages. It'll also publish the changelogs on GitHub for each package.
|
This will automatically bump the version and publish the packages. It'll also publish the changelogs on GitHub for each package.
|
||||||
|
|
||||||
|
<!-- badges -->
|
||||||
|
|
||||||
|
[build-badge]: https://img.shields.io/circleci/project/github/react-navigation/navigation-ex/master.svg?style=flat-square
|
||||||
|
[build]: https://circleci.com/gh/react-navigation/navigation-ex
|
||||||
|
[coverage-badge]: https://img.shields.io/codecov/c/github/react-navigation/navigation-ex.svg?style=flat-square
|
||||||
|
[coverage]: https://codecov.io/github/react-navigation/navigation-ex
|
||||||
|
[license-badge]: https://img.shields.io/npm/l/@react-navigation/core.svg?style=flat-square
|
||||||
|
[license]: https://opensource.org/licenses/MIT
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git+https://github.com/satya164/react-navigation.git"
|
"url": "git+https://github.com/satya164/react-navigation.git"
|
||||||
},
|
},
|
||||||
"author": "Satyajit Sahoo <satyajit.happy@gmail.com> (https://github.com/satya164/)",
|
"author": "Satyajit Sahoo <satyajit.happy@gmail.com> (https://github.com/satya164/), Michał Osadnik <micosa97@gmail.com> (https://github.com/osdnk/)",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"lint": "eslint --ext '.js,.ts,.tsx' .",
|
"lint": "eslint --ext '.js,.ts,.tsx' .",
|
||||||
"typescript": "tsc --noEmit",
|
"typescript": "tsc --noEmit",
|
||||||
@@ -59,7 +59,10 @@
|
|||||||
},
|
},
|
||||||
"setupFiles": [
|
"setupFiles": [
|
||||||
"<rootDir>/jest/setup.js"
|
"<rootDir>/jest/setup.js"
|
||||||
]
|
],
|
||||||
|
"moduleNameMapper": {
|
||||||
|
"@react-navigation/([^/]+)": "<rootDir>/packages/$1/src"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"name": "react-navigation"
|
"name": "react-navigation"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,36 @@
|
|||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
||||||
|
|
||||||
|
# [5.0.0-alpha.5](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/bottom-tabs@5.0.0-alpha.4...@react-navigation/bottom-tabs@5.0.0-alpha.5) (2019-08-28)
|
||||||
|
|
||||||
|
**Note:** Version bump only for package @react-navigation/bottom-tabs
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# [5.0.0-alpha.4](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/bottom-tabs@5.0.0-alpha.3...@react-navigation/bottom-tabs@5.0.0-alpha.4) (2019-08-27)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* add hook to scroll to top on tab press ([9e1104c](https://github.com/react-navigation/navigation-ex/commit/9e1104c))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# [5.0.0-alpha.3](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/bottom-tabs@5.0.0-alpha.2...@react-navigation/bottom-tabs@5.0.0-alpha.3) (2019-08-22)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* fix path to typescript definitions ([f182315](https://github.com/react-navigation/navigation-ex/commit/f182315))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# [5.0.0-alpha.2](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/bottom-tabs@5.0.0-alpha.1...@react-navigation/bottom-tabs@5.0.0-alpha.2) (2019-08-22)
|
# [5.0.0-alpha.2](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/bottom-tabs@5.0.0-alpha.1...@react-navigation/bottom-tabs@5.0.0-alpha.2) (2019-08-22)
|
||||||
|
|
||||||
**Note:** Version bump only for package @react-navigation/bottom-tabs
|
**Note:** Version bump only for package @react-navigation/bottom-tabs
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
"android",
|
"android",
|
||||||
"tab"
|
"tab"
|
||||||
],
|
],
|
||||||
"version": "5.0.0-alpha.2",
|
"version": "5.0.0-alpha.5",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -20,7 +20,7 @@
|
|||||||
"main": "lib/commonjs/index.js",
|
"main": "lib/commonjs/index.js",
|
||||||
"react-native": "src/index.tsx",
|
"react-native": "src/index.tsx",
|
||||||
"module": "lib/module/index.js",
|
"module": "lib/module/index.js",
|
||||||
"types": "lib/typescript/src/index.d.ts",
|
"types": "lib/typescript/bottom-tabssrc/index.d.ts",
|
||||||
"files": [
|
"files": [
|
||||||
"src",
|
"src",
|
||||||
"lib"
|
"lib"
|
||||||
@@ -33,7 +33,7 @@
|
|||||||
"clean": "del lib"
|
"clean": "del lib"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@react-navigation/routers": "^5.0.0-alpha.2",
|
"@react-navigation/routers": "^5.0.0-alpha.5",
|
||||||
"react-native-safe-area-view": "^0.14.6"
|
"react-native-safe-area-view": "^0.14.6"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import BottomTabView from '../views/BottomTabView';
|
|||||||
import {
|
import {
|
||||||
BottomTabNavigationConfig,
|
BottomTabNavigationConfig,
|
||||||
BottomTabNavigationOptions,
|
BottomTabNavigationOptions,
|
||||||
|
BottomTabNavigationEventMap,
|
||||||
} from '../types';
|
} from '../types';
|
||||||
|
|
||||||
type Props = DefaultNavigatorOptions<BottomTabNavigationOptions> &
|
type Props = DefaultNavigatorOptions<BottomTabNavigationOptions> &
|
||||||
@@ -28,8 +29,9 @@ function BottomTabNavigator({
|
|||||||
}: Props) {
|
}: Props) {
|
||||||
const { state, descriptors, navigation } = useNavigationBuilder<
|
const { state, descriptors, navigation } = useNavigationBuilder<
|
||||||
TabNavigationState,
|
TabNavigationState,
|
||||||
|
TabRouterOptions,
|
||||||
BottomTabNavigationOptions,
|
BottomTabNavigationOptions,
|
||||||
TabRouterOptions
|
BottomTabNavigationEventMap
|
||||||
>(TabRouter, {
|
>(TabRouter, {
|
||||||
initialRouteName,
|
initialRouteName,
|
||||||
backBehavior,
|
backBehavior,
|
||||||
|
|||||||
@@ -17,10 +17,6 @@ import {
|
|||||||
import { TabNavigationState } from '@react-navigation/routers';
|
import { TabNavigationState } from '@react-navigation/routers';
|
||||||
|
|
||||||
export type BottomTabNavigationEventMap = {
|
export type BottomTabNavigationEventMap = {
|
||||||
/**
|
|
||||||
* Event which fires on tapping on the tab for an already focused screen.
|
|
||||||
*/
|
|
||||||
refocus: undefined;
|
|
||||||
/**
|
/**
|
||||||
* Event which fires on tapping on the tab in the tab bar.
|
* Event which fires on tapping on the tab in the tab bar.
|
||||||
*/
|
*/
|
||||||
@@ -35,6 +31,11 @@ export type Orientation = 'horizontal' | 'vertical';
|
|||||||
|
|
||||||
export type LabelPosition = 'beside-icon' | 'below-icon';
|
export type LabelPosition = 'beside-icon' | 'below-icon';
|
||||||
|
|
||||||
|
export type BottomTabNavigationHelpers = NavigationHelpers<
|
||||||
|
ParamListBase,
|
||||||
|
BottomTabNavigationEventMap
|
||||||
|
>;
|
||||||
|
|
||||||
export type BottomTabNavigationProp<
|
export type BottomTabNavigationProp<
|
||||||
ParamList extends ParamListBase,
|
ParamList extends ParamListBase,
|
||||||
RouteName extends keyof ParamList = string
|
RouteName extends keyof ParamList = string
|
||||||
|
|||||||
@@ -5,23 +5,22 @@ import {
|
|||||||
AccessibilityRole,
|
AccessibilityRole,
|
||||||
AccessibilityStates,
|
AccessibilityStates,
|
||||||
} from 'react-native';
|
} from 'react-native';
|
||||||
import {
|
import { Route, CommonActions } from '@react-navigation/core';
|
||||||
NavigationHelpers,
|
|
||||||
ParamListBase,
|
|
||||||
Route,
|
|
||||||
BaseActions,
|
|
||||||
} from '@react-navigation/core';
|
|
||||||
import { TabNavigationState } from '@react-navigation/routers';
|
import { TabNavigationState } from '@react-navigation/routers';
|
||||||
// eslint-disable-next-line import/no-unresolved
|
// eslint-disable-next-line import/no-unresolved
|
||||||
import { ScreenContainer } from 'react-native-screens';
|
import { ScreenContainer } from 'react-native-screens';
|
||||||
|
|
||||||
import BottomTabBar from './BottomTabBar';
|
import BottomTabBar from './BottomTabBar';
|
||||||
import { BottomTabNavigationConfig, BottomTabDescriptorMap } from '../types';
|
import {
|
||||||
|
BottomTabNavigationConfig,
|
||||||
|
BottomTabDescriptorMap,
|
||||||
|
BottomTabNavigationHelpers,
|
||||||
|
} from '../types';
|
||||||
import ResourceSavingScene from './ResourceSavingScene';
|
import ResourceSavingScene from './ResourceSavingScene';
|
||||||
|
|
||||||
type Props = BottomTabNavigationConfig & {
|
type Props = BottomTabNavigationConfig & {
|
||||||
state: TabNavigationState;
|
state: TabNavigationState;
|
||||||
navigation: NavigationHelpers<ParamListBase>;
|
navigation: BottomTabNavigationHelpers;
|
||||||
descriptors: BottomTabDescriptorMap;
|
descriptors: BottomTabDescriptorMap;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -139,14 +138,12 @@ export default class BottomTabView extends React.Component<Props, State> {
|
|||||||
target: route.key,
|
target: route.key,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (state.routes[state.index].key === route.key) {
|
if (
|
||||||
navigation.emit({
|
state.routes[state.index].key !== route.key &&
|
||||||
type: 'refocus',
|
!event.defaultPrevented
|
||||||
target: route.key,
|
) {
|
||||||
});
|
|
||||||
} else if (!event.defaultPrevented) {
|
|
||||||
navigation.dispatch({
|
navigation.dispatch({
|
||||||
...BaseActions.navigate(route.name),
|
...CommonActions.navigate(route.name),
|
||||||
target: state.key,
|
target: state.key,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,29 @@
|
|||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
||||||
|
|
||||||
|
# [5.0.0-alpha.3](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/core@5.0.0-alpha.2...@react-navigation/core@5.0.0-alpha.3) (2019-08-27)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* add hook to scroll to top on tab press ([9e1104c](https://github.com/react-navigation/navigation-ex/commit/9e1104c))
|
||||||
|
* add native container ([d26b77f](https://github.com/react-navigation/navigation-ex/commit/d26b77f))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# [5.0.0-alpha.2](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/core@5.0.0-alpha.1...@react-navigation/core@5.0.0-alpha.2) (2019-08-22)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* fix path to typescript definitions ([f182315](https://github.com/react-navigation/navigation-ex/commit/f182315))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# 5.0.0-alpha.1 (2019-08-21)
|
# 5.0.0-alpha.1 (2019-08-21)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
"react-native",
|
"react-native",
|
||||||
"react-navigation"
|
"react-navigation"
|
||||||
],
|
],
|
||||||
"version": "5.0.0-alpha.1",
|
"version": "5.0.0-alpha.3",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -16,7 +16,7 @@
|
|||||||
"main": "lib/commonjs/index.js",
|
"main": "lib/commonjs/index.js",
|
||||||
"react-native": "src/index.tsx",
|
"react-native": "src/index.tsx",
|
||||||
"module": "lib/module/index.js",
|
"module": "lib/module/index.js",
|
||||||
"types": "lib/typescript/src/index.d.ts",
|
"types": "lib/typescript/index.d.ts",
|
||||||
"files": [
|
"files": [
|
||||||
"src",
|
"src",
|
||||||
"lib"
|
"lib"
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import * as BaseActions from './BaseActions';
|
import * as CommonActions from './CommonActions';
|
||||||
import EnsureSingleNavigator from './EnsureSingleNavigator';
|
import EnsureSingleNavigator from './EnsureSingleNavigator';
|
||||||
import NavigationBuilderContext from './NavigationBuilderContext';
|
import NavigationBuilderContext from './NavigationBuilderContext';
|
||||||
import useFocusedListeners from './useFocusedListeners';
|
import useFocusedListeners from './useFocusedListeners';
|
||||||
@@ -12,16 +12,11 @@ import {
|
|||||||
PartialState,
|
PartialState,
|
||||||
NavigationAction,
|
NavigationAction,
|
||||||
NavigationContainerRef,
|
NavigationContainerRef,
|
||||||
|
NavigationContainerProps,
|
||||||
} from './types';
|
} from './types';
|
||||||
|
|
||||||
type State = NavigationState | PartialState<NavigationState> | undefined;
|
type State = NavigationState | PartialState<NavigationState> | undefined;
|
||||||
|
|
||||||
type Props = {
|
|
||||||
initialState?: InitialState;
|
|
||||||
onStateChange?: (state: State) => void;
|
|
||||||
children: React.ReactNode;
|
|
||||||
};
|
|
||||||
|
|
||||||
const MISSING_CONTEXT_ERROR =
|
const MISSING_CONTEXT_ERROR =
|
||||||
"We couldn't find a navigation context. Have you wrapped your app with 'NavigationContainer'?";
|
"We couldn't find a navigation context. Have you wrapped your app with 'NavigationContainer'?";
|
||||||
|
|
||||||
@@ -84,7 +79,7 @@ const getPartialState = (
|
|||||||
* @param props.ref Ref object which refers to the navigation object containing helper methods.
|
* @param props.ref Ref object which refers to the navigation object containing helper methods.
|
||||||
*/
|
*/
|
||||||
const Container = React.forwardRef(function NavigationContainer(
|
const Container = React.forwardRef(function NavigationContainer(
|
||||||
{ initialState, onStateChange, children }: Props,
|
{ initialState, onStateChange, children }: NavigationContainerProps,
|
||||||
ref: React.Ref<NavigationContainerRef>
|
ref: React.Ref<NavigationContainerRef>
|
||||||
) {
|
) {
|
||||||
const [state, setNavigationState] = React.useState<State>(() =>
|
const [state, setNavigationState] = React.useState<State>(() =>
|
||||||
@@ -110,13 +105,13 @@ const Container = React.forwardRef(function NavigationContainer(
|
|||||||
};
|
};
|
||||||
|
|
||||||
React.useImperativeHandle(ref, () => ({
|
React.useImperativeHandle(ref, () => ({
|
||||||
...(Object.keys(BaseActions) as Array<keyof typeof BaseActions>).reduce<
|
...(Object.keys(CommonActions) as Array<keyof typeof CommonActions>).reduce<
|
||||||
any
|
any
|
||||||
>((acc, name) => {
|
>((acc, name) => {
|
||||||
acc[name] = (...args: any[]) =>
|
acc[name] = (...args: any[]) =>
|
||||||
dispatch(
|
dispatch(
|
||||||
// eslint-disable-next-line import/namespace
|
// eslint-disable-next-line import/namespace
|
||||||
BaseActions[name](
|
CommonActions[name](
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
...args
|
...args
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import BaseRouter from '../BaseRouter';
|
import BaseRouter from '../BaseRouter';
|
||||||
import * as BaseActions from '../BaseActions';
|
import * as CommonActions from '../CommonActions';
|
||||||
|
|
||||||
jest.mock('shortid', () => () => 'test');
|
jest.mock('shortid', () => () => 'test');
|
||||||
|
|
||||||
@@ -18,7 +18,7 @@ const STATE = {
|
|||||||
it('replaces focused screen with REPLACE', () => {
|
it('replaces focused screen with REPLACE', () => {
|
||||||
const result = BaseRouter.getStateForAction(
|
const result = BaseRouter.getStateForAction(
|
||||||
STATE,
|
STATE,
|
||||||
BaseActions.replace('qux', { answer: 42 })
|
CommonActions.replace('qux', { answer: 42 })
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(result).toEqual({
|
expect(result).toEqual({
|
||||||
@@ -36,7 +36,7 @@ it('replaces focused screen with REPLACE', () => {
|
|||||||
|
|
||||||
it('replaces source screen with REPLACE', () => {
|
it('replaces source screen with REPLACE', () => {
|
||||||
const result = BaseRouter.getStateForAction(STATE, {
|
const result = BaseRouter.getStateForAction(STATE, {
|
||||||
...BaseActions.replace('qux', { answer: 42 }),
|
...CommonActions.replace('qux', { answer: 42 }),
|
||||||
source: 'baz',
|
source: 'baz',
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -55,7 +55,7 @@ it('replaces source screen with REPLACE', () => {
|
|||||||
|
|
||||||
it("doesn't handle REPLACE if source key isn't present", () => {
|
it("doesn't handle REPLACE if source key isn't present", () => {
|
||||||
const result = BaseRouter.getStateForAction(STATE, {
|
const result = BaseRouter.getStateForAction(STATE, {
|
||||||
...BaseActions.replace('qux', { answer: 42 }),
|
...CommonActions.replace('qux', { answer: 42 }),
|
||||||
source: 'magic',
|
source: 'magic',
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -65,7 +65,7 @@ it("doesn't handle REPLACE if source key isn't present", () => {
|
|||||||
it('sets params for the focused screen with SET_PARAMS', () => {
|
it('sets params for the focused screen with SET_PARAMS', () => {
|
||||||
const result = BaseRouter.getStateForAction(
|
const result = BaseRouter.getStateForAction(
|
||||||
STATE,
|
STATE,
|
||||||
BaseActions.setParams({ answer: 42 })
|
CommonActions.setParams({ answer: 42 })
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(result).toEqual({
|
expect(result).toEqual({
|
||||||
@@ -83,7 +83,7 @@ it('sets params for the focused screen with SET_PARAMS', () => {
|
|||||||
|
|
||||||
it('sets params for the source screen with SET_PARAMS', () => {
|
it('sets params for the source screen with SET_PARAMS', () => {
|
||||||
const result = BaseRouter.getStateForAction(STATE, {
|
const result = BaseRouter.getStateForAction(STATE, {
|
||||||
...BaseActions.setParams({ answer: 42 }),
|
...CommonActions.setParams({ answer: 42 }),
|
||||||
source: 'foo',
|
source: 'foo',
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -102,7 +102,7 @@ it('sets params for the source screen with SET_PARAMS', () => {
|
|||||||
|
|
||||||
it("doesn't handle SET_PARAMS if source key isn't present", () => {
|
it("doesn't handle SET_PARAMS if source key isn't present", () => {
|
||||||
const result = BaseRouter.getStateForAction(STATE, {
|
const result = BaseRouter.getStateForAction(STATE, {
|
||||||
...BaseActions.setParams({ answer: 42 }),
|
...CommonActions.setParams({ answer: 42 }),
|
||||||
source: 'magic',
|
source: 'magic',
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -119,7 +119,7 @@ it('resets state to new state with RESET', () => {
|
|||||||
|
|
||||||
const result = BaseRouter.getStateForAction(
|
const result = BaseRouter.getStateForAction(
|
||||||
STATE,
|
STATE,
|
||||||
BaseActions.reset({
|
CommonActions.reset({
|
||||||
index: 0,
|
index: 0,
|
||||||
routes,
|
routes,
|
||||||
})
|
})
|
||||||
@@ -132,7 +132,7 @@ it('ignores key and routeNames when resetting with RESET', () => {
|
|||||||
const result = BaseRouter.getStateForAction(
|
const result = BaseRouter.getStateForAction(
|
||||||
STATE,
|
STATE,
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
BaseActions.reset({ index: 2, key: 'foo', routeNames: ['test'] })
|
CommonActions.reset({ index: 2, key: 'foo', routeNames: ['test'] })
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(result).toEqual({ ...STATE, index: 2 });
|
expect(result).toEqual({ ...STATE, index: 2 });
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ import { render, act } from 'react-native-testing-library';
|
|||||||
import Screen from '../Screen';
|
import Screen from '../Screen';
|
||||||
import NavigationContainer from '../NavigationContainer';
|
import NavigationContainer from '../NavigationContainer';
|
||||||
import useNavigationBuilder from '../useNavigationBuilder';
|
import useNavigationBuilder from '../useNavigationBuilder';
|
||||||
import MockRouter, { MockRouterKey } from './__fixtures__/MockRouter';
|
|
||||||
import useNavigation from '../useNavigation';
|
import useNavigation from '../useNavigation';
|
||||||
|
import MockRouter, { MockRouterKey } from './__fixtures__/MockRouter';
|
||||||
import { NavigationState } from '../types';
|
import { NavigationState } from '../types';
|
||||||
|
|
||||||
beforeEach(() => (MockRouterKey.current = 0));
|
beforeEach(() => (MockRouterKey.current = 0));
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ it('sets options with options prop as an object', () => {
|
|||||||
const TestNavigator = (props: any) => {
|
const TestNavigator = (props: any) => {
|
||||||
const { state, descriptors } = useNavigationBuilder<
|
const { state, descriptors } = useNavigationBuilder<
|
||||||
NavigationState,
|
NavigationState,
|
||||||
|
any,
|
||||||
{ title?: string },
|
{ title?: string },
|
||||||
any
|
any
|
||||||
>(MockRouter, props);
|
>(MockRouter, props);
|
||||||
@@ -61,6 +62,7 @@ it("returns correct value for canGoBack when it's not overridden", () => {
|
|||||||
const TestNavigator = (props: any) => {
|
const TestNavigator = (props: any) => {
|
||||||
const { state, descriptors } = useNavigationBuilder<
|
const { state, descriptors } = useNavigationBuilder<
|
||||||
NavigationState,
|
NavigationState,
|
||||||
|
any,
|
||||||
{ title?: string },
|
{ title?: string },
|
||||||
any
|
any
|
||||||
>(MockRouter, props);
|
>(MockRouter, props);
|
||||||
@@ -123,6 +125,7 @@ it(`returns false for canGoBack when current router doesn't handle GO_BACK`, ()
|
|||||||
const { state, descriptors } = useNavigationBuilder<
|
const { state, descriptors } = useNavigationBuilder<
|
||||||
NavigationState,
|
NavigationState,
|
||||||
any,
|
any,
|
||||||
|
any,
|
||||||
any
|
any
|
||||||
>(TestRouter, props);
|
>(TestRouter, props);
|
||||||
|
|
||||||
@@ -172,6 +175,7 @@ it('returns true for canGoBack when current router handles GO_BACK', () => {
|
|||||||
const ParentNavigator = (props: any) => {
|
const ParentNavigator = (props: any) => {
|
||||||
const { state, descriptors } = useNavigationBuilder<
|
const { state, descriptors } = useNavigationBuilder<
|
||||||
NavigationState,
|
NavigationState,
|
||||||
|
any,
|
||||||
{ title?: string },
|
{ title?: string },
|
||||||
any
|
any
|
||||||
>(ParentRouter, props);
|
>(ParentRouter, props);
|
||||||
@@ -181,6 +185,7 @@ it('returns true for canGoBack when current router handles GO_BACK', () => {
|
|||||||
const ChildNavigator = (props: any) => {
|
const ChildNavigator = (props: any) => {
|
||||||
const { state, descriptors } = useNavigationBuilder<
|
const { state, descriptors } = useNavigationBuilder<
|
||||||
NavigationState,
|
NavigationState,
|
||||||
|
any,
|
||||||
{ title?: string },
|
{ title?: string },
|
||||||
any
|
any
|
||||||
>(MockRouter, props);
|
>(MockRouter, props);
|
||||||
@@ -237,6 +242,7 @@ it('returns true for canGoBack when parent router handles GO_BACK', () => {
|
|||||||
const OverrodeNavigator = (props: any) => {
|
const OverrodeNavigator = (props: any) => {
|
||||||
const { state, descriptors } = useNavigationBuilder<
|
const { state, descriptors } = useNavigationBuilder<
|
||||||
NavigationState,
|
NavigationState,
|
||||||
|
any,
|
||||||
{ title?: string },
|
{ title?: string },
|
||||||
any
|
any
|
||||||
>(OverrodeRouter, props);
|
>(OverrodeRouter, props);
|
||||||
@@ -246,6 +252,7 @@ it('returns true for canGoBack when parent router handles GO_BACK', () => {
|
|||||||
const TestNavigator = (props: any) => {
|
const TestNavigator = (props: any) => {
|
||||||
const { state, descriptors } = useNavigationBuilder<
|
const { state, descriptors } = useNavigationBuilder<
|
||||||
NavigationState,
|
NavigationState,
|
||||||
|
any,
|
||||||
{ title?: string },
|
{ title?: string },
|
||||||
any
|
any
|
||||||
>(MockRouter, props);
|
>(MockRouter, props);
|
||||||
@@ -293,6 +300,7 @@ it('sets options with options prop as a fuction', () => {
|
|||||||
const TestNavigator = (props: any) => {
|
const TestNavigator = (props: any) => {
|
||||||
const { state, descriptors } = useNavigationBuilder<
|
const { state, descriptors } = useNavigationBuilder<
|
||||||
NavigationState,
|
NavigationState,
|
||||||
|
any,
|
||||||
{ title?: string },
|
{ title?: string },
|
||||||
any
|
any
|
||||||
>(MockRouter, props);
|
>(MockRouter, props);
|
||||||
@@ -338,6 +346,7 @@ it('sets initial options with setOptions', () => {
|
|||||||
const TestNavigator = (props: any) => {
|
const TestNavigator = (props: any) => {
|
||||||
const { state, descriptors } = useNavigationBuilder<
|
const { state, descriptors } = useNavigationBuilder<
|
||||||
NavigationState,
|
NavigationState,
|
||||||
|
any,
|
||||||
{
|
{
|
||||||
title?: string;
|
title?: string;
|
||||||
color?: string;
|
color?: string;
|
||||||
@@ -392,6 +401,7 @@ it('updates options with setOptions', () => {
|
|||||||
const { state, descriptors } = useNavigationBuilder<
|
const { state, descriptors } = useNavigationBuilder<
|
||||||
NavigationState,
|
NavigationState,
|
||||||
any,
|
any,
|
||||||
|
any,
|
||||||
any
|
any
|
||||||
>(MockRouter, props);
|
>(MockRouter, props);
|
||||||
const { render, options } = descriptors[state.routes[state.index].key];
|
const { render, options } = descriptors[state.routes[state.index].key];
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import * as BaseActions from './BaseActions';
|
import * as CommonActions from './CommonActions';
|
||||||
|
|
||||||
export { BaseActions };
|
export { CommonActions };
|
||||||
|
|
||||||
export { default as BaseRouter } from './BaseRouter';
|
export { default as BaseRouter } from './BaseRouter';
|
||||||
export { default as NavigationContainer } from './NavigationContainer';
|
export { default as NavigationContainer } from './NavigationContainer';
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import * as BaseActions from './BaseActions';
|
import * as CommonActions from './CommonActions';
|
||||||
|
import * as React from 'react';
|
||||||
|
|
||||||
export type CommonAction = BaseActions.Action;
|
export type CommonAction = CommonActions.Action;
|
||||||
|
|
||||||
export type NavigationState = {
|
export type NavigationState = {
|
||||||
/**
|
/**
|
||||||
@@ -186,7 +187,7 @@ export type EventMapBase = {
|
|||||||
blur: undefined;
|
blur: undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type EventArg<EventName extends string, Data> = {
|
export type EventArg<EventName extends string, Data = undefined> = {
|
||||||
/**
|
/**
|
||||||
* Type of the event (e.g. `focus`, `blur`)
|
* Type of the event (e.g. `focus`, `blur`)
|
||||||
*/
|
*/
|
||||||
@@ -231,11 +232,14 @@ export type EventEmitter<EventMap extends { [key: string]: any }> = {
|
|||||||
* @param [options.target] Key of the target route which should receive the event.
|
* @param [options.target] Key of the target route which should receive the event.
|
||||||
* If not specified, all routes receive the event.
|
* If not specified, all routes receive the event.
|
||||||
*/
|
*/
|
||||||
emit<EventName extends Extract<keyof EventMap, string>>(options: {
|
emit<EventName extends Extract<keyof EventMap, string>>(
|
||||||
type: EventName;
|
options: {
|
||||||
data?: EventMap[EventName];
|
type: EventName;
|
||||||
target?: string;
|
target?: string;
|
||||||
}): EventArg<EventName, EventMap[EventName]>;
|
} & (EventMap[EventName] extends undefined
|
||||||
|
? {}
|
||||||
|
: { data: EventMap[EventName] })
|
||||||
|
): EventArg<EventName, EventMap[EventName]>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export class PrivateValueStore<A, B, C> {
|
export class PrivateValueStore<A, B, C> {
|
||||||
@@ -326,9 +330,10 @@ type NavigationHelpersCommon<
|
|||||||
} & PrivateValueStore<ParamList, keyof ParamList, {}>;
|
} & PrivateValueStore<ParamList, keyof ParamList, {}>;
|
||||||
|
|
||||||
export type NavigationHelpers<
|
export type NavigationHelpers<
|
||||||
ParamList extends ParamListBase
|
ParamList extends ParamListBase,
|
||||||
|
EventMap extends { [key: string]: any } = {}
|
||||||
> = NavigationHelpersCommon<ParamList> &
|
> = NavigationHelpersCommon<ParamList> &
|
||||||
EventEmitter<{ [key: string]: any }> & {
|
EventEmitter<EventMap> & {
|
||||||
/**
|
/**
|
||||||
* Update the param object for the route.
|
* Update the param object for the route.
|
||||||
* The new params will be shallow merged with the old one.
|
* The new params will be shallow merged with the old one.
|
||||||
@@ -340,6 +345,14 @@ export type NavigationHelpers<
|
|||||||
): void;
|
): void;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type NavigationContainerProps = {
|
||||||
|
initialState?: InitialState;
|
||||||
|
onStateChange?: (
|
||||||
|
state: NavigationState | PartialState<NavigationState> | undefined
|
||||||
|
) => void;
|
||||||
|
children: React.ReactNode;
|
||||||
|
};
|
||||||
|
|
||||||
export type NavigationProp<
|
export type NavigationProp<
|
||||||
ParamList extends ParamListBase,
|
ParamList extends ParamListBase,
|
||||||
RouteName extends keyof ParamList = string,
|
RouteName extends keyof ParamList = string,
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import {
|
|||||||
RouterFactory,
|
RouterFactory,
|
||||||
PartialState,
|
PartialState,
|
||||||
PrivateValueStore,
|
PrivateValueStore,
|
||||||
|
NavigationAction,
|
||||||
} from './types';
|
} from './types';
|
||||||
|
|
||||||
// This is to make TypeScript compiler happy
|
// This is to make TypeScript compiler happy
|
||||||
@@ -84,8 +85,9 @@ const getRouteConfigsFromChildren = <ScreenOptions extends object>(
|
|||||||
*/
|
*/
|
||||||
export default function useNavigationBuilder<
|
export default function useNavigationBuilder<
|
||||||
State extends NavigationState,
|
State extends NavigationState,
|
||||||
|
RouterOptions extends DefaultRouterOptions,
|
||||||
ScreenOptions extends object,
|
ScreenOptions extends object,
|
||||||
RouterOptions extends DefaultRouterOptions
|
EventMap extends { [key: string]: any }
|
||||||
>(
|
>(
|
||||||
createRouter: RouterFactory<State, any, RouterOptions>,
|
createRouter: RouterFactory<State, any, RouterOptions>,
|
||||||
options: DefaultNavigatorOptions<ScreenOptions> & RouterOptions
|
options: DefaultNavigatorOptions<ScreenOptions> & RouterOptions
|
||||||
@@ -240,7 +242,7 @@ export default function useNavigationBuilder<
|
|||||||
setState,
|
setState,
|
||||||
});
|
});
|
||||||
|
|
||||||
const navigation = useNavigationHelpers({
|
const navigation = useNavigationHelpers<State, NavigationAction, EventMap>({
|
||||||
onAction,
|
onAction,
|
||||||
getState,
|
getState,
|
||||||
setState,
|
setState,
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import * as BaseActions from './BaseActions';
|
import * as CommonActions from './CommonActions';
|
||||||
import { NavigationEventEmitter } from './useEventEmitter';
|
import { NavigationEventEmitter } from './useEventEmitter';
|
||||||
import NavigationContext from './NavigationContext';
|
import NavigationContext from './NavigationContext';
|
||||||
|
|
||||||
@@ -53,7 +53,7 @@ export default function useNavigationCache<
|
|||||||
|
|
||||||
const actions = {
|
const actions = {
|
||||||
...router.actionCreators,
|
...router.actionCreators,
|
||||||
...BaseActions,
|
...CommonActions,
|
||||||
};
|
};
|
||||||
|
|
||||||
cache.current = state.routes.reduce<NavigationCache<State, ScreenOptions>>(
|
cache.current = state.routes.reduce<NavigationCache<State, ScreenOptions>>(
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import * as BaseActions from './BaseActions';
|
import * as CommonActions from './CommonActions';
|
||||||
import NavigationContext from './NavigationContext';
|
import NavigationContext from './NavigationContext';
|
||||||
import { NavigationStateContext } from './NavigationContainer';
|
import { NavigationStateContext } from './NavigationContainer';
|
||||||
import { NavigationEventEmitter } from './useEventEmitter';
|
import { NavigationEventEmitter } from './useEventEmitter';
|
||||||
@@ -34,7 +34,8 @@ type Options<State extends NavigationState, Action extends NavigationAction> = {
|
|||||||
*/
|
*/
|
||||||
export default function useNavigationHelpers<
|
export default function useNavigationHelpers<
|
||||||
State extends NavigationState,
|
State extends NavigationState,
|
||||||
Action extends NavigationAction
|
Action extends NavigationAction,
|
||||||
|
EventMap extends { [key: string]: any }
|
||||||
>({ onAction, getState, setState, emitter, router }: Options<State, Action>) {
|
>({ onAction, getState, setState, emitter, router }: Options<State, Action>) {
|
||||||
const parentNavigationHelpers = React.useContext(NavigationContext);
|
const parentNavigationHelpers = React.useContext(NavigationContext);
|
||||||
const { performTransaction } = React.useContext(NavigationStateContext);
|
const { performTransaction } = React.useContext(NavigationStateContext);
|
||||||
@@ -51,7 +52,7 @@ export default function useNavigationHelpers<
|
|||||||
|
|
||||||
const actions = {
|
const actions = {
|
||||||
...router.actionCreators,
|
...router.actionCreators,
|
||||||
...BaseActions,
|
...CommonActions,
|
||||||
};
|
};
|
||||||
|
|
||||||
const helpers = Object.keys(actions).reduce(
|
const helpers = Object.keys(actions).reduce(
|
||||||
@@ -72,11 +73,13 @@ export default function useNavigationHelpers<
|
|||||||
? parentNavigationHelpers.isFocused
|
? parentNavigationHelpers.isFocused
|
||||||
: () => true,
|
: () => true,
|
||||||
canGoBack: () =>
|
canGoBack: () =>
|
||||||
router.getStateForAction(getState(), BaseActions.goBack() as Action) !==
|
router.getStateForAction(
|
||||||
null ||
|
getState(),
|
||||||
|
CommonActions.goBack() as Action
|
||||||
|
) !== null ||
|
||||||
(parentNavigationHelpers && parentNavigationHelpers.canGoBack()) ||
|
(parentNavigationHelpers && parentNavigationHelpers.canGoBack()) ||
|
||||||
false,
|
false,
|
||||||
} as NavigationHelpers<ParamListBase> &
|
} as NavigationHelpers<ParamListBase, EventMap> &
|
||||||
(NavigationProp<ParamListBase, string, any, any, any> | undefined);
|
(NavigationProp<ParamListBase, string, any, any, any> | undefined);
|
||||||
}, [
|
}, [
|
||||||
router,
|
router,
|
||||||
|
|||||||
@@ -3,6 +3,33 @@
|
|||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
||||||
|
|
||||||
|
# [5.0.0-alpha.5](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/drawer@5.0.0-alpha.4...@react-navigation/drawer@5.0.0-alpha.5) (2019-08-28)
|
||||||
|
|
||||||
|
**Note:** Version bump only for package @react-navigation/drawer
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# [5.0.0-alpha.4](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/drawer@5.0.0-alpha.3...@react-navigation/drawer@5.0.0-alpha.4) (2019-08-27)
|
||||||
|
|
||||||
|
**Note:** Version bump only for package @react-navigation/drawer
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# [5.0.0-alpha.3](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/drawer@5.0.0-alpha.2...@react-navigation/drawer@5.0.0-alpha.3) (2019-08-22)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* fix path to typescript definitions ([f182315](https://github.com/react-navigation/navigation-ex/commit/f182315))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# [5.0.0-alpha.2](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/drawer@5.0.0-alpha.1...@react-navigation/drawer@5.0.0-alpha.2) (2019-08-22)
|
# [5.0.0-alpha.2](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/drawer@5.0.0-alpha.1...@react-navigation/drawer@5.0.0-alpha.2) (2019-08-22)
|
||||||
|
|
||||||
**Note:** Version bump only for package @react-navigation/drawer
|
**Note:** Version bump only for package @react-navigation/drawer
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
"material",
|
"material",
|
||||||
"drawer"
|
"drawer"
|
||||||
],
|
],
|
||||||
"version": "5.0.0-alpha.2",
|
"version": "5.0.0-alpha.5",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -21,7 +21,7 @@
|
|||||||
"main": "lib/commonjs/index.js",
|
"main": "lib/commonjs/index.js",
|
||||||
"react-native": "src/index.tsx",
|
"react-native": "src/index.tsx",
|
||||||
"module": "lib/module/index.js",
|
"module": "lib/module/index.js",
|
||||||
"types": "lib/typescript/src/index.d.ts",
|
"types": "lib/typescript/drawer/src/index.d.ts",
|
||||||
"files": [
|
"files": [
|
||||||
"src",
|
"src",
|
||||||
"lib"
|
"lib"
|
||||||
@@ -34,7 +34,7 @@
|
|||||||
"clean": "del lib"
|
"clean": "del lib"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@react-navigation/routers": "^5.0.0-alpha.2",
|
"@react-navigation/routers": "^5.0.0-alpha.5",
|
||||||
"react-native-safe-area-view": "^0.14.6"
|
"react-native-safe-area-view": "^0.14.6"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@@ -11,7 +11,11 @@ import {
|
|||||||
} from '@react-navigation/routers';
|
} from '@react-navigation/routers';
|
||||||
|
|
||||||
import DrawerView from '../views/DrawerView';
|
import DrawerView from '../views/DrawerView';
|
||||||
import { DrawerNavigationOptions, DrawerNavigationConfig } from '../types';
|
import {
|
||||||
|
DrawerNavigationOptions,
|
||||||
|
DrawerNavigationConfig,
|
||||||
|
DrawerNavigationEventMap,
|
||||||
|
} from '../types';
|
||||||
|
|
||||||
type Props = DefaultNavigatorOptions<DrawerNavigationOptions> &
|
type Props = DefaultNavigatorOptions<DrawerNavigationOptions> &
|
||||||
DrawerRouterOptions &
|
DrawerRouterOptions &
|
||||||
@@ -25,8 +29,9 @@ function DrawerNavigator({
|
|||||||
}: Props) {
|
}: Props) {
|
||||||
const { state, descriptors, navigation } = useNavigationBuilder<
|
const { state, descriptors, navigation } = useNavigationBuilder<
|
||||||
DrawerNavigationState,
|
DrawerNavigationState,
|
||||||
|
DrawerRouterOptions,
|
||||||
DrawerNavigationOptions,
|
DrawerNavigationOptions,
|
||||||
DrawerRouterOptions
|
DrawerNavigationEventMap
|
||||||
>(DrawerRouter, {
|
>(DrawerRouter, {
|
||||||
initialRouteName,
|
initialRouteName,
|
||||||
children,
|
children,
|
||||||
|
|||||||
@@ -182,6 +182,11 @@ export type DrawerNavigationEventMap = {
|
|||||||
drawerClose: undefined;
|
drawerClose: undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type DrawerNavigationHelpers = NavigationHelpers<
|
||||||
|
ParamListBase,
|
||||||
|
DrawerNavigationEventMap
|
||||||
|
>;
|
||||||
|
|
||||||
export type DrawerNavigationProp<
|
export type DrawerNavigationProp<
|
||||||
ParamList extends ParamListBase,
|
ParamList extends ParamListBase,
|
||||||
RouteName extends keyof ParamList = string
|
RouteName extends keyof ParamList = string
|
||||||
|
|||||||
@@ -1,24 +1,24 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { StyleSheet, View, ViewStyle, StyleProp } from 'react-native';
|
import { StyleSheet, View, ViewStyle, StyleProp } from 'react-native';
|
||||||
import Animated from 'react-native-reanimated';
|
import Animated from 'react-native-reanimated';
|
||||||
import {
|
import { Route, CommonActions } from '@react-navigation/core';
|
||||||
NavigationHelpers,
|
|
||||||
ParamListBase,
|
|
||||||
Route,
|
|
||||||
BaseActions,
|
|
||||||
} from '@react-navigation/core';
|
|
||||||
import {
|
import {
|
||||||
DrawerActions,
|
DrawerActions,
|
||||||
DrawerNavigationState,
|
DrawerNavigationState,
|
||||||
} from '@react-navigation/routers';
|
} from '@react-navigation/routers';
|
||||||
|
|
||||||
import { Scene, ContentComponentProps, DrawerDescriptorMap } from '../types';
|
import {
|
||||||
|
Scene,
|
||||||
|
ContentComponentProps,
|
||||||
|
DrawerDescriptorMap,
|
||||||
|
DrawerNavigationHelpers,
|
||||||
|
} from '../types';
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
contentComponent?: React.ComponentType<ContentComponentProps>;
|
contentComponent?: React.ComponentType<ContentComponentProps>;
|
||||||
contentOptions?: object;
|
contentOptions?: object;
|
||||||
state: DrawerNavigationState;
|
state: DrawerNavigationState;
|
||||||
navigation: NavigationHelpers<ParamListBase>;
|
navigation: DrawerNavigationHelpers;
|
||||||
descriptors: DrawerDescriptorMap;
|
descriptors: DrawerDescriptorMap;
|
||||||
drawerOpenProgress: Animated.Node<number>;
|
drawerOpenProgress: Animated.Node<number>;
|
||||||
drawerPosition: 'left' | 'right';
|
drawerPosition: 'left' | 'right';
|
||||||
@@ -78,7 +78,7 @@ class DrawerSidebar extends React.PureComponent<Props> {
|
|||||||
navigation.dispatch({
|
navigation.dispatch({
|
||||||
...(focused
|
...(focused
|
||||||
? DrawerActions.closeDrawer()
|
? DrawerActions.closeDrawer()
|
||||||
: BaseActions.navigate(route.name)),
|
: CommonActions.navigate(route.name)),
|
||||||
target: state.key,
|
target: state.key,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import { Dimensions, StyleSheet, I18nManager, Platform } from 'react-native';
|
|||||||
import { ScreenContainer } from 'react-native-screens';
|
import { ScreenContainer } from 'react-native-screens';
|
||||||
import SafeAreaView from 'react-native-safe-area-view';
|
import SafeAreaView from 'react-native-safe-area-view';
|
||||||
import { PanGestureHandler, ScrollView } from 'react-native-gesture-handler';
|
import { PanGestureHandler, ScrollView } from 'react-native-gesture-handler';
|
||||||
import { ParamListBase, NavigationHelpers } from '@react-navigation/core';
|
|
||||||
import {
|
import {
|
||||||
DrawerNavigationState,
|
DrawerNavigationState,
|
||||||
DrawerActions,
|
DrawerActions,
|
||||||
@@ -19,11 +18,12 @@ import {
|
|||||||
DrawerDescriptorMap,
|
DrawerDescriptorMap,
|
||||||
DrawerNavigationConfig,
|
DrawerNavigationConfig,
|
||||||
ContentComponentProps,
|
ContentComponentProps,
|
||||||
|
DrawerNavigationHelpers,
|
||||||
} from '../types';
|
} from '../types';
|
||||||
|
|
||||||
type Props = DrawerNavigationConfig & {
|
type Props = DrawerNavigationConfig & {
|
||||||
state: DrawerNavigationState;
|
state: DrawerNavigationState;
|
||||||
navigation: NavigationHelpers<ParamListBase>;
|
navigation: DrawerNavigationHelpers;
|
||||||
descriptors: DrawerDescriptorMap;
|
descriptors: DrawerDescriptorMap;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,17 @@
|
|||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
||||||
|
|
||||||
|
# [5.0.0-alpha.2](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/example@5.0.0-alpha.1...@react-navigation/example@5.0.0-alpha.2) (2019-08-27)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* add native container ([d26b77f](https://github.com/react-navigation/navigation-ex/commit/d26b77f))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# 5.0.0-alpha.1 (2019-08-21)
|
# 5.0.0-alpha.1 (2019-08-21)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,6 @@
|
|||||||
|
|
||||||
If you want to run the example from the repo,
|
If you want to run the example from the repo,
|
||||||
|
|
||||||
- Clone the repository and run `lerna bootstrap` in the root
|
- Clone the repository and run `yarn` in the project root
|
||||||
- Run `yarn example start` to start the packager
|
- Run `yarn example start` to start the packager
|
||||||
- Follow the instructions to open it with the [Expo app](https://expo.io/)
|
- Follow the instructions to open it with the [Expo app](https://expo.io/)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "@react-navigation/example",
|
"name": "@react-navigation/example",
|
||||||
"description": "Demo app to showcase various functionality of React Navigation",
|
"description": "Demo app to showcase various functionality of React Navigation",
|
||||||
"version": "5.0.0-alpha.1",
|
"version": "5.0.0-alpha.2",
|
||||||
"private": true,
|
"private": true,
|
||||||
"workspaces": {
|
"workspaces": {
|
||||||
"nohoist": [
|
"nohoist": [
|
||||||
|
|||||||
@@ -4,12 +4,11 @@ import { Linking } from 'expo';
|
|||||||
import { Appbar, List } from 'react-native-paper';
|
import { Appbar, List } from 'react-native-paper';
|
||||||
import { Asset } from 'expo-asset';
|
import { Asset } from 'expo-asset';
|
||||||
import {
|
import {
|
||||||
NavigationContainer,
|
|
||||||
InitialState,
|
InitialState,
|
||||||
getStateFromPath,
|
getStateFromPath,
|
||||||
NavigationContainerRef,
|
NavigationContainerRef,
|
||||||
} from '@react-navigation/core';
|
} from '@react-navigation/core';
|
||||||
import { useBackButton, useLinking } from '@react-navigation/native';
|
import { useLinking, NativeContainer } from '@react-navigation/native';
|
||||||
import {
|
import {
|
||||||
createDrawerNavigator,
|
createDrawerNavigator,
|
||||||
DrawerNavigationProp,
|
DrawerNavigationProp,
|
||||||
@@ -60,8 +59,6 @@ Asset.loadAsync(StackAssets);
|
|||||||
export default function App() {
|
export default function App() {
|
||||||
const containerRef = React.useRef<NavigationContainerRef>();
|
const containerRef = React.useRef<NavigationContainerRef>();
|
||||||
|
|
||||||
useBackButton(containerRef);
|
|
||||||
|
|
||||||
// To test deep linking on, run the following in the Terminal:
|
// To test deep linking on, run the following in the Terminal:
|
||||||
// Android: adb shell am start -a android.intent.action.VIEW -d "exp://127.0.0.1:19000/--/simple-stack"
|
// Android: adb shell am start -a android.intent.action.VIEW -d "exp://127.0.0.1:19000/--/simple-stack"
|
||||||
// iOS: xcrun simctl openurl booted exp://127.0.0.1:19000/--/simple-stack
|
// iOS: xcrun simctl openurl booted exp://127.0.0.1:19000/--/simple-stack
|
||||||
@@ -116,7 +113,7 @@ export default function App() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<NavigationContainer
|
<NativeContainer
|
||||||
ref={containerRef}
|
ref={containerRef}
|
||||||
initialState={initialState}
|
initialState={initialState}
|
||||||
onStateChange={state =>
|
onStateChange={state =>
|
||||||
@@ -175,6 +172,6 @@ export default function App() {
|
|||||||
)}
|
)}
|
||||||
</Drawer.Screen>
|
</Drawer.Screen>
|
||||||
</Drawer.Navigator>
|
</Drawer.Navigator>
|
||||||
</NavigationContainer>
|
</NativeContainer>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,36 @@
|
|||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
||||||
|
|
||||||
|
# [5.0.0-alpha.5](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/material-bottom-tabs@5.0.0-alpha.4...@react-navigation/material-bottom-tabs@5.0.0-alpha.5) (2019-08-28)
|
||||||
|
|
||||||
|
**Note:** Version bump only for package @react-navigation/material-bottom-tabs
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# [5.0.0-alpha.4](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/material-bottom-tabs@5.0.0-alpha.3...@react-navigation/material-bottom-tabs@5.0.0-alpha.4) (2019-08-27)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* add hook to scroll to top on tab press ([9e1104c](https://github.com/react-navigation/navigation-ex/commit/9e1104c))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# [5.0.0-alpha.3](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/material-bottom-tabs@5.0.0-alpha.2...@react-navigation/material-bottom-tabs@5.0.0-alpha.3) (2019-08-22)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* fix path to typescript definitions ([f182315](https://github.com/react-navigation/navigation-ex/commit/f182315))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# [5.0.0-alpha.2](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/material-bottom-tabs@5.0.0-alpha.1...@react-navigation/material-bottom-tabs@5.0.0-alpha.2) (2019-08-22)
|
# [5.0.0-alpha.2](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/material-bottom-tabs@5.0.0-alpha.1...@react-navigation/material-bottom-tabs@5.0.0-alpha.2) (2019-08-22)
|
||||||
|
|
||||||
**Note:** Version bump only for package @react-navigation/material-bottom-tabs
|
**Note:** Version bump only for package @react-navigation/material-bottom-tabs
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
"material",
|
"material",
|
||||||
"tab"
|
"tab"
|
||||||
],
|
],
|
||||||
"version": "5.0.0-alpha.2",
|
"version": "5.0.0-alpha.5",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -21,7 +21,7 @@
|
|||||||
"main": "lib/commonjs/index.js",
|
"main": "lib/commonjs/index.js",
|
||||||
"react-native": "src/index.tsx",
|
"react-native": "src/index.tsx",
|
||||||
"module": "lib/module/index.js",
|
"module": "lib/module/index.js",
|
||||||
"types": "lib/typescript/src/index.d.ts",
|
"types": "lib/typescript/material-bottom-tabs/src/index.d.ts",
|
||||||
"files": [
|
"files": [
|
||||||
"src",
|
"src",
|
||||||
"lib"
|
"lib"
|
||||||
@@ -34,7 +34,7 @@
|
|||||||
"clean": "del lib"
|
"clean": "del lib"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@react-navigation/routers": "^5.0.0-alpha.2"
|
"@react-navigation/routers": "^5.0.0-alpha.5"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@react-native-community/bob": "^0.7.0",
|
"@react-native-community/bob": "^0.7.0",
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import MaterialBottomTabView from '../views/MaterialBottomTabView';
|
|||||||
import {
|
import {
|
||||||
MaterialBottomTabNavigationConfig,
|
MaterialBottomTabNavigationConfig,
|
||||||
MaterialBottomTabNavigationOptions,
|
MaterialBottomTabNavigationOptions,
|
||||||
|
MaterialBottomTabNavigationEventMap,
|
||||||
} from '../types';
|
} from '../types';
|
||||||
|
|
||||||
type Props = DefaultNavigatorOptions<MaterialBottomTabNavigationOptions> &
|
type Props = DefaultNavigatorOptions<MaterialBottomTabNavigationOptions> &
|
||||||
@@ -29,8 +30,9 @@ function MaterialBottomTabNavigator({
|
|||||||
}: Props) {
|
}: Props) {
|
||||||
const { state, descriptors, navigation } = useNavigationBuilder<
|
const { state, descriptors, navigation } = useNavigationBuilder<
|
||||||
TabNavigationState,
|
TabNavigationState,
|
||||||
|
TabRouterOptions,
|
||||||
MaterialBottomTabNavigationOptions,
|
MaterialBottomTabNavigationOptions,
|
||||||
TabRouterOptions
|
MaterialBottomTabNavigationEventMap
|
||||||
>(TabRouter, {
|
>(TabRouter, {
|
||||||
initialRouteName,
|
initialRouteName,
|
||||||
backBehavior,
|
backBehavior,
|
||||||
|
|||||||
@@ -3,14 +3,22 @@ import {
|
|||||||
ParamListBase,
|
ParamListBase,
|
||||||
Descriptor,
|
Descriptor,
|
||||||
NavigationProp,
|
NavigationProp,
|
||||||
|
NavigationHelpers,
|
||||||
} from '@react-navigation/core';
|
} from '@react-navigation/core';
|
||||||
import { TabNavigationState } from '@react-navigation/routers';
|
import { TabNavigationState } from '@react-navigation/routers';
|
||||||
|
|
||||||
export type MaterialBottomTabNavigationEventMap = {
|
export type MaterialBottomTabNavigationEventMap = {
|
||||||
refocus: undefined;
|
/**
|
||||||
|
* Event which fires on tapping on the tab in the tab bar.
|
||||||
|
*/
|
||||||
tabPress: undefined;
|
tabPress: undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type MaterialBottomTabNavigationHelpers = NavigationHelpers<
|
||||||
|
ParamListBase,
|
||||||
|
MaterialBottomTabNavigationEventMap
|
||||||
|
>;
|
||||||
|
|
||||||
export type MaterialBottomTabNavigationProp<
|
export type MaterialBottomTabNavigationProp<
|
||||||
ParamList extends ParamListBase,
|
ParamList extends ParamListBase,
|
||||||
RouteName extends keyof ParamList = string
|
RouteName extends keyof ParamList = string
|
||||||
|
|||||||
@@ -2,21 +2,18 @@ import * as React from 'react';
|
|||||||
import { StyleSheet } from 'react-native';
|
import { StyleSheet } from 'react-native';
|
||||||
import { BottomNavigation } from 'react-native-paper';
|
import { BottomNavigation } from 'react-native-paper';
|
||||||
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialIcons';
|
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialIcons';
|
||||||
import {
|
import { Route } from '@react-navigation/core';
|
||||||
NavigationHelpers,
|
|
||||||
ParamListBase,
|
|
||||||
Route,
|
|
||||||
} from '@react-navigation/core';
|
|
||||||
import { TabNavigationState, TabActions } from '@react-navigation/routers';
|
import { TabNavigationState, TabActions } from '@react-navigation/routers';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
MaterialBottomTabDescriptorMap,
|
MaterialBottomTabDescriptorMap,
|
||||||
MaterialBottomTabNavigationConfig,
|
MaterialBottomTabNavigationConfig,
|
||||||
|
MaterialBottomTabNavigationHelpers,
|
||||||
} from '../types';
|
} from '../types';
|
||||||
|
|
||||||
type Props = MaterialBottomTabNavigationConfig & {
|
type Props = MaterialBottomTabNavigationConfig & {
|
||||||
state: TabNavigationState;
|
state: TabNavigationState;
|
||||||
navigation: NavigationHelpers<ParamListBase>;
|
navigation: MaterialBottomTabNavigationHelpers;
|
||||||
descriptors: MaterialBottomTabDescriptorMap;
|
descriptors: MaterialBottomTabDescriptorMap;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -65,19 +62,12 @@ export default class MaterialBottomTabView extends React.PureComponent<Props> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
private handleTabPress = ({ route }: Scene) => {
|
private handleTabPress = ({ route }: Scene) => {
|
||||||
const { state, navigation } = this.props;
|
const { navigation } = this.props;
|
||||||
|
|
||||||
navigation.emit({
|
navigation.emit({
|
||||||
type: 'tabPress',
|
type: 'tabPress',
|
||||||
target: route.key,
|
target: route.key,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (state.routes[state.index].key === route.key) {
|
|
||||||
navigation.emit({
|
|
||||||
type: 'refocus',
|
|
||||||
target: route.key,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
private renderIcon = ({
|
private renderIcon = ({
|
||||||
|
|||||||
@@ -3,6 +3,36 @@
|
|||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
||||||
|
|
||||||
|
# [5.0.0-alpha.5](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/material-top-tabs@5.0.0-alpha.4...@react-navigation/material-top-tabs@5.0.0-alpha.5) (2019-08-28)
|
||||||
|
|
||||||
|
**Note:** Version bump only for package @react-navigation/material-top-tabs
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# [5.0.0-alpha.4](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/material-top-tabs@5.0.0-alpha.3...@react-navigation/material-top-tabs@5.0.0-alpha.4) (2019-08-27)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* add hook to scroll to top on tab press ([9e1104c](https://github.com/react-navigation/navigation-ex/commit/9e1104c))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# [5.0.0-alpha.3](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/material-top-tabs@5.0.0-alpha.2...@react-navigation/material-top-tabs@5.0.0-alpha.3) (2019-08-22)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* fix path to typescript definitions ([f182315](https://github.com/react-navigation/navigation-ex/commit/f182315))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# [5.0.0-alpha.2](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/material-top-tabs@5.0.0-alpha.1...@react-navigation/material-top-tabs@5.0.0-alpha.2) (2019-08-22)
|
# [5.0.0-alpha.2](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/material-top-tabs@5.0.0-alpha.1...@react-navigation/material-top-tabs@5.0.0-alpha.2) (2019-08-22)
|
||||||
|
|
||||||
**Note:** Version bump only for package @react-navigation/material-top-tabs
|
**Note:** Version bump only for package @react-navigation/material-top-tabs
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
"material",
|
"material",
|
||||||
"tab"
|
"tab"
|
||||||
],
|
],
|
||||||
"version": "5.0.0-alpha.2",
|
"version": "5.0.0-alpha.5",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -21,7 +21,7 @@
|
|||||||
"main": "lib/commonjs/index.js",
|
"main": "lib/commonjs/index.js",
|
||||||
"react-native": "src/index.tsx",
|
"react-native": "src/index.tsx",
|
||||||
"module": "lib/module/index.js",
|
"module": "lib/module/index.js",
|
||||||
"types": "lib/typescript/src/index.d.ts",
|
"types": "lib/typescript/material-top-tabs/src/index.d.ts",
|
||||||
"files": [
|
"files": [
|
||||||
"src",
|
"src",
|
||||||
"lib"
|
"lib"
|
||||||
@@ -34,7 +34,7 @@
|
|||||||
"clean": "del lib"
|
"clean": "del lib"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@react-navigation/routers": "^5.0.0-alpha.2"
|
"@react-navigation/routers": "^5.0.0-alpha.5"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@react-native-community/bob": "^0.7.0",
|
"@react-native-community/bob": "^0.7.0",
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import MaterialTopTabView from '../views/MaterialTopTabView';
|
|||||||
import {
|
import {
|
||||||
MaterialTopTabNavigationConfig,
|
MaterialTopTabNavigationConfig,
|
||||||
MaterialTopTabNavigationOptions,
|
MaterialTopTabNavigationOptions,
|
||||||
|
MaterialTopTabNavigationEventMap,
|
||||||
} from '../types';
|
} from '../types';
|
||||||
|
|
||||||
type Props = DefaultNavigatorOptions<MaterialTopTabNavigationOptions> &
|
type Props = DefaultNavigatorOptions<MaterialTopTabNavigationOptions> &
|
||||||
@@ -28,8 +29,9 @@ function MaterialTopTabNavigator({
|
|||||||
}: Props) {
|
}: Props) {
|
||||||
const { state, descriptors, navigation } = useNavigationBuilder<
|
const { state, descriptors, navigation } = useNavigationBuilder<
|
||||||
TabNavigationState,
|
TabNavigationState,
|
||||||
|
TabRouterOptions,
|
||||||
MaterialTopTabNavigationOptions,
|
MaterialTopTabNavigationOptions,
|
||||||
TabRouterOptions
|
MaterialTopTabNavigationEventMap
|
||||||
>(TabRouter, {
|
>(TabRouter, {
|
||||||
initialRouteName,
|
initialRouteName,
|
||||||
backBehavior,
|
backBehavior,
|
||||||
|
|||||||
@@ -10,10 +10,6 @@ import {
|
|||||||
import { TabNavigationState } from '@react-navigation/routers';
|
import { TabNavigationState } from '@react-navigation/routers';
|
||||||
|
|
||||||
export type MaterialTopTabNavigationEventMap = {
|
export type MaterialTopTabNavigationEventMap = {
|
||||||
/**
|
|
||||||
* Event which fires on tapping on the tab for an already focused screen.
|
|
||||||
*/
|
|
||||||
refocus: undefined;
|
|
||||||
/**
|
/**
|
||||||
* Event which fires on tapping on the tab in the tab bar.
|
* Event which fires on tapping on the tab in the tab bar.
|
||||||
*/
|
*/
|
||||||
@@ -32,6 +28,11 @@ export type MaterialTopTabNavigationEventMap = {
|
|||||||
swipeEnd: undefined;
|
swipeEnd: undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type MaterialTopTabNavigationHelpers = NavigationHelpers<
|
||||||
|
ParamListBase,
|
||||||
|
MaterialTopTabNavigationEventMap
|
||||||
|
>;
|
||||||
|
|
||||||
export type MaterialTopTabNavigationProp<
|
export type MaterialTopTabNavigationProp<
|
||||||
ParamList extends ParamListBase,
|
ParamList extends ParamListBase,
|
||||||
RouteName extends keyof ParamList = string
|
RouteName extends keyof ParamList = string
|
||||||
|
|||||||
@@ -1,21 +1,18 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { TabView, SceneRendererProps } from 'react-native-tab-view';
|
import { TabView, SceneRendererProps } from 'react-native-tab-view';
|
||||||
import {
|
import { Route } from '@react-navigation/core';
|
||||||
NavigationHelpers,
|
|
||||||
ParamListBase,
|
|
||||||
Route,
|
|
||||||
} from '@react-navigation/core';
|
|
||||||
import { TabNavigationState, TabActions } from '@react-navigation/routers';
|
import { TabNavigationState, TabActions } from '@react-navigation/routers';
|
||||||
|
|
||||||
import MaterialTopTabBar from './MaterialTopTabBar';
|
import MaterialTopTabBar from './MaterialTopTabBar';
|
||||||
import {
|
import {
|
||||||
MaterialTopTabDescriptorMap,
|
MaterialTopTabDescriptorMap,
|
||||||
MaterialTopTabNavigationConfig,
|
MaterialTopTabNavigationConfig,
|
||||||
|
MaterialTopTabNavigationHelpers,
|
||||||
} from '../types';
|
} from '../types';
|
||||||
|
|
||||||
type Props = MaterialTopTabNavigationConfig & {
|
type Props = MaterialTopTabNavigationConfig & {
|
||||||
state: TabNavigationState;
|
state: TabNavigationState;
|
||||||
navigation: NavigationHelpers<ParamListBase>;
|
navigation: MaterialTopTabNavigationHelpers;
|
||||||
descriptors: MaterialTopTabDescriptorMap;
|
descriptors: MaterialTopTabDescriptorMap;
|
||||||
tabBarPosition: 'top' | 'bottom';
|
tabBarPosition: 'top' | 'bottom';
|
||||||
};
|
};
|
||||||
@@ -76,7 +73,6 @@ export default class MaterialTopTabView extends React.PureComponent<Props> {
|
|||||||
route: Route<string>;
|
route: Route<string>;
|
||||||
preventDefault: () => void;
|
preventDefault: () => void;
|
||||||
}) => {
|
}) => {
|
||||||
const { state, navigation } = this.props;
|
|
||||||
const event = this.props.navigation.emit({
|
const event = this.props.navigation.emit({
|
||||||
type: 'tabPress',
|
type: 'tabPress',
|
||||||
target: route.key,
|
target: route.key,
|
||||||
@@ -85,13 +81,6 @@ export default class MaterialTopTabView extends React.PureComponent<Props> {
|
|||||||
if (event.defaultPrevented) {
|
if (event.defaultPrevented) {
|
||||||
preventDefault();
|
preventDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (state.routes[state.index].key === route.key) {
|
|
||||||
navigation.emit({
|
|
||||||
type: 'refocus',
|
|
||||||
target: route.key,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
private handleTabLongPress = ({ route }: { route: Route<string> }) => {
|
private handleTabLongPress = ({ route }: { route: Route<string> }) => {
|
||||||
|
|||||||
@@ -3,6 +3,29 @@
|
|||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
||||||
|
|
||||||
|
# [5.0.0-alpha.3](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/native@5.0.0-alpha.2...@react-navigation/native@5.0.0-alpha.3) (2019-08-27)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* add hook to scroll to top on tab press ([9e1104c](https://github.com/react-navigation/navigation-ex/commit/9e1104c))
|
||||||
|
* add native container ([d26b77f](https://github.com/react-navigation/navigation-ex/commit/d26b77f))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# [5.0.0-alpha.2](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/native@5.0.0-alpha.1...@react-navigation/native@5.0.0-alpha.2) (2019-08-22)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* fix path to typescript definitions ([f182315](https://github.com/react-navigation/navigation-ex/commit/f182315))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# 5.0.0-alpha.1 (2019-08-21)
|
# 5.0.0-alpha.1 (2019-08-21)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
"ios",
|
"ios",
|
||||||
"android"
|
"android"
|
||||||
],
|
],
|
||||||
"version": "5.0.0-alpha.1",
|
"version": "5.0.0-alpha.3",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -17,7 +17,7 @@
|
|||||||
"main": "lib/commonjs/index.js",
|
"main": "lib/commonjs/index.js",
|
||||||
"react-native": "src/index.tsx",
|
"react-native": "src/index.tsx",
|
||||||
"module": "lib/module/index.js",
|
"module": "lib/module/index.js",
|
||||||
"types": "lib/typescript/src/index.d.ts",
|
"types": "lib/typescript/native/src/index.d.ts",
|
||||||
"files": [
|
"files": [
|
||||||
"src",
|
"src",
|
||||||
"lib"
|
"lib"
|
||||||
|
|||||||
38
packages/native/src/NativeContainer.tsx
Normal file
38
packages/native/src/NativeContainer.tsx
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
import * as React from 'react';
|
||||||
|
import {
|
||||||
|
NavigationContainer,
|
||||||
|
NavigationContainerProps,
|
||||||
|
NavigationContainerRef,
|
||||||
|
} from '@react-navigation/core';
|
||||||
|
import useBackButton from './useBackButton';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Container component which holds the navigation state
|
||||||
|
* designed for mobile apps.
|
||||||
|
* This should be rendered at the root wrapping the whole app.
|
||||||
|
*
|
||||||
|
* @param props.initialState Initial state object for the navigation tree.
|
||||||
|
* @param props.onStateChange Callback which is called with the latest navigation state when it changes.
|
||||||
|
* @param props.children Child elements to render the content.
|
||||||
|
* @param props.ref Ref object which refers to the navigation object containing helper methods.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const NativeContainer = React.forwardRef(function NativeContainer(
|
||||||
|
props: NavigationContainerProps,
|
||||||
|
ref: React.Ref<NavigationContainerRef>
|
||||||
|
) {
|
||||||
|
const refContainer = React.useRef<NavigationContainerRef>(null);
|
||||||
|
|
||||||
|
useBackButton(refContainer);
|
||||||
|
React.useImperativeHandle(ref, () => refContainer.current);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<NavigationContainer
|
||||||
|
{...props}
|
||||||
|
ref={refContainer}
|
||||||
|
children={props.children}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
export default NativeContainer;
|
||||||
@@ -1,2 +1,4 @@
|
|||||||
|
export { default as NativeContainer } from './NativeContainer';
|
||||||
export { default as useBackButton } from './useBackButton';
|
export { default as useBackButton } from './useBackButton';
|
||||||
export { default as useLinking } from './useLinking';
|
export { default as useLinking } from './useLinking';
|
||||||
|
export { default as useScrollToTop } from './useScrollToTop';
|
||||||
|
|||||||
28
packages/native/src/useScrollToTop.tsx
Normal file
28
packages/native/src/useScrollToTop.tsx
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import * as React from 'react';
|
||||||
|
import { useNavigation, EventArg } from '@react-navigation/core';
|
||||||
|
|
||||||
|
type ScrollableView = {
|
||||||
|
scrollTo(options: { x?: number; y?: number; animated?: boolean }): void;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function useScrollToTop(ref: React.RefObject<ScrollableView>) {
|
||||||
|
const navigation = useNavigation();
|
||||||
|
|
||||||
|
React.useEffect(
|
||||||
|
() =>
|
||||||
|
// @ts-ignore
|
||||||
|
// We don't wanna import tab types here to avoid extra deps
|
||||||
|
// in addition, there are multiple tab implementations
|
||||||
|
navigation.addListener('tabPress', (e: EventArg<'tabPress'>) => {
|
||||||
|
// Run the operation in the next frame so we're sure all listeners have been run
|
||||||
|
// This is necessary to know if preventDefault() has been called
|
||||||
|
requestAnimationFrame(() => {
|
||||||
|
if (navigation.isFocused() && !e.defaultPrevented && ref.current) {
|
||||||
|
// When user taps on already focused tab, scroll to top
|
||||||
|
ref.current.scrollTo({ y: 0 });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}),
|
||||||
|
[navigation, ref]
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -3,6 +3,33 @@
|
|||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
||||||
|
|
||||||
|
# [5.0.0-alpha.5](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/routers@5.0.0-alpha.4...@react-navigation/routers@5.0.0-alpha.5) (2019-08-28)
|
||||||
|
|
||||||
|
**Note:** Version bump only for package @react-navigation/routers
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# [5.0.0-alpha.4](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/routers@5.0.0-alpha.3...@react-navigation/routers@5.0.0-alpha.4) (2019-08-27)
|
||||||
|
|
||||||
|
**Note:** Version bump only for package @react-navigation/routers
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# [5.0.0-alpha.3](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/routers@5.0.0-alpha.2...@react-navigation/routers@5.0.0-alpha.3) (2019-08-22)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* fix path to typescript definitions ([f182315](https://github.com/react-navigation/navigation-ex/commit/f182315))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# [5.0.0-alpha.2](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/routers@5.0.0-alpha.1...@react-navigation/routers@5.0.0-alpha.2) (2019-08-22)
|
# [5.0.0-alpha.2](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/routers@5.0.0-alpha.1...@react-navigation/routers@5.0.0-alpha.2) (2019-08-22)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
440
packages/routers/__tests__/DrawerRouter.test.tsx
Normal file
440
packages/routers/__tests__/DrawerRouter.test.tsx
Normal file
@@ -0,0 +1,440 @@
|
|||||||
|
import { CommonActions } from '@react-navigation/core';
|
||||||
|
import { DrawerRouter, DrawerActions } from '../src';
|
||||||
|
|
||||||
|
jest.mock('shortid', () => () => 'test');
|
||||||
|
|
||||||
|
it('gets initial state from route names and params with initialRouteName', () => {
|
||||||
|
const router = DrawerRouter({ initialRouteName: 'baz' });
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getInitialState({
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routeParamList: {
|
||||||
|
baz: { answer: 42 },
|
||||||
|
qux: { name: 'Jane' },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
).toEqual({
|
||||||
|
index: 1,
|
||||||
|
key: 'drawer-test',
|
||||||
|
isDrawerOpen: false,
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-test', name: 'bar' },
|
||||||
|
{ key: 'baz-test', name: 'baz', params: { answer: 42 } },
|
||||||
|
{ key: 'qux-test', name: 'qux', params: { name: 'Jane' } },
|
||||||
|
],
|
||||||
|
stale: false,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('gets initial state from route names and params without initialRouteName', () => {
|
||||||
|
const router = DrawerRouter({});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getInitialState({
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routeParamList: {
|
||||||
|
baz: { answer: 42 },
|
||||||
|
qux: { name: 'Jane' },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
).toEqual({
|
||||||
|
index: 0,
|
||||||
|
key: 'drawer-test',
|
||||||
|
isDrawerOpen: false,
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-test', name: 'bar' },
|
||||||
|
{ key: 'baz-test', name: 'baz', params: { answer: 42 } },
|
||||||
|
{ key: 'qux-test', name: 'qux', params: { name: 'Jane' } },
|
||||||
|
],
|
||||||
|
stale: false,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('gets rehydrated state from partial state', () => {
|
||||||
|
const router = DrawerRouter({});
|
||||||
|
|
||||||
|
const options = {
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routeParamList: {
|
||||||
|
baz: { answer: 42 },
|
||||||
|
qux: { name: 'Jane' },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getRehydratedState(
|
||||||
|
{
|
||||||
|
routes: [{ key: 'bar-0', name: 'bar' }, { key: 'qux-1', name: 'qux' }],
|
||||||
|
},
|
||||||
|
options
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
index: 0,
|
||||||
|
key: 'drawer-test',
|
||||||
|
isDrawerOpen: false,
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-0', name: 'bar' },
|
||||||
|
{ key: 'baz-test', name: 'baz', params: { answer: 42 } },
|
||||||
|
{ key: 'qux-1', name: 'qux', params: { name: 'Jane' } },
|
||||||
|
],
|
||||||
|
stale: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getRehydratedState(
|
||||||
|
{
|
||||||
|
index: 2,
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-0', name: 'bar' },
|
||||||
|
{ key: 'baz-1', name: 'baz' },
|
||||||
|
{ key: 'qux-2', name: 'qux' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
options
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
index: 2,
|
||||||
|
key: 'drawer-test',
|
||||||
|
isDrawerOpen: false,
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-0', name: 'bar' },
|
||||||
|
{ key: 'baz-1', name: 'baz', params: { answer: 42 } },
|
||||||
|
{ key: 'qux-2', name: 'qux', params: { name: 'Jane' } },
|
||||||
|
],
|
||||||
|
stale: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getRehydratedState(
|
||||||
|
{
|
||||||
|
index: 4,
|
||||||
|
routes: [],
|
||||||
|
},
|
||||||
|
options
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
index: 0,
|
||||||
|
key: 'drawer-test',
|
||||||
|
isDrawerOpen: false,
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-test', name: 'bar' },
|
||||||
|
{ key: 'baz-test', name: 'baz', params: { answer: 42 } },
|
||||||
|
{ key: 'qux-test', name: 'qux', params: { name: 'Jane' } },
|
||||||
|
],
|
||||||
|
stale: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getRehydratedState(
|
||||||
|
{
|
||||||
|
index: 1,
|
||||||
|
isDrawerOpen: true,
|
||||||
|
routeKeyHistory: ['bar-test', 'qux-test', 'foo-test'],
|
||||||
|
routes: [],
|
||||||
|
},
|
||||||
|
options
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
index: 1,
|
||||||
|
key: 'drawer-test',
|
||||||
|
isDrawerOpen: true,
|
||||||
|
routeKeyHistory: ['bar-test', 'qux-test'],
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-test', name: 'bar' },
|
||||||
|
{ key: 'baz-test', name: 'baz', params: { answer: 42 } },
|
||||||
|
{ key: 'qux-test', name: 'qux', params: { name: 'Jane' } },
|
||||||
|
],
|
||||||
|
stale: false,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("doesn't rehydrate state if it's not stale", () => {
|
||||||
|
const router = DrawerRouter({});
|
||||||
|
|
||||||
|
const state = {
|
||||||
|
index: 0,
|
||||||
|
key: 'drawer-test',
|
||||||
|
isDrawerOpen: true,
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-test', name: 'bar' },
|
||||||
|
{ key: 'baz-test', name: 'baz', params: { answer: 42 } },
|
||||||
|
{ key: 'qux-test', name: 'qux', params: { name: 'Jane' } },
|
||||||
|
],
|
||||||
|
stale: false as const,
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getRehydratedState(state, {
|
||||||
|
routeNames: [],
|
||||||
|
routeParamList: {},
|
||||||
|
})
|
||||||
|
).toBe(state);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles navigate action', () => {
|
||||||
|
const router = DrawerRouter({});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 1,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
routeKeyHistory: [],
|
||||||
|
isDrawerOpen: false,
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
},
|
||||||
|
CommonActions.navigate('baz', { answer: 42 })
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 0,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
isDrawerOpen: false,
|
||||||
|
routeKeyHistory: ['bar'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'baz', name: 'baz', params: { answer: 42 } },
|
||||||
|
{ key: 'bar', name: 'bar' },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles open drawer action', () => {
|
||||||
|
const router = DrawerRouter({});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 1,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
routeKeyHistory: [],
|
||||||
|
isDrawerOpen: false,
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
},
|
||||||
|
DrawerActions.openDrawer()
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 1,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
isDrawerOpen: true,
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
});
|
||||||
|
|
||||||
|
const state = {
|
||||||
|
stale: false as const,
|
||||||
|
key: 'root',
|
||||||
|
index: 1,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
isDrawerOpen: true,
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(router.getStateForAction(state, DrawerActions.openDrawer())).toBe(
|
||||||
|
state
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles close drawer action', () => {
|
||||||
|
const router = DrawerRouter({});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 1,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
routeKeyHistory: [],
|
||||||
|
isDrawerOpen: true,
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
},
|
||||||
|
DrawerActions.closeDrawer()
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 1,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
isDrawerOpen: false,
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
});
|
||||||
|
|
||||||
|
const state = {
|
||||||
|
stale: false as const,
|
||||||
|
key: 'root',
|
||||||
|
index: 1,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
isDrawerOpen: false,
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(router.getStateForAction(state, DrawerActions.closeDrawer())).toBe(
|
||||||
|
state
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles toggle drawer action', () => {
|
||||||
|
const router = DrawerRouter({});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 1,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
routeKeyHistory: [],
|
||||||
|
isDrawerOpen: true,
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
},
|
||||||
|
DrawerActions.toggleDrawer()
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 1,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
isDrawerOpen: false,
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 1,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
routeKeyHistory: [],
|
||||||
|
isDrawerOpen: false,
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
},
|
||||||
|
DrawerActions.toggleDrawer()
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 1,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
isDrawerOpen: true,
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('updates route key history on focus change', () => {
|
||||||
|
const router = DrawerRouter({ backBehavior: 'history' });
|
||||||
|
|
||||||
|
const state = {
|
||||||
|
index: 0,
|
||||||
|
key: 'drawer-test',
|
||||||
|
isDrawerOpen: false,
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-0', name: 'bar' },
|
||||||
|
{ key: 'baz-0', name: 'baz', params: { answer: 42 } },
|
||||||
|
{ key: 'qux-0', name: 'qux', params: { name: 'Jane' } },
|
||||||
|
],
|
||||||
|
stale: false as const,
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(router.getStateForRouteFocus(state, 'bar-0').routeKeyHistory).toEqual(
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(router.getStateForRouteFocus(state, 'baz-0').routeKeyHistory).toEqual([
|
||||||
|
'bar-0',
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('closes drawer on focus change', () => {
|
||||||
|
const router = DrawerRouter({ backBehavior: 'history' });
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForRouteFocus(
|
||||||
|
{
|
||||||
|
index: 0,
|
||||||
|
key: 'drawer-test',
|
||||||
|
isDrawerOpen: false,
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-0', name: 'bar' },
|
||||||
|
{ key: 'baz-0', name: 'baz' },
|
||||||
|
{ key: 'qux-0', name: 'qux' },
|
||||||
|
],
|
||||||
|
stale: false,
|
||||||
|
},
|
||||||
|
'baz-0'
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
index: 1,
|
||||||
|
isDrawerOpen: false,
|
||||||
|
key: 'drawer-test',
|
||||||
|
routeKeyHistory: ['bar-0'],
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-0', name: 'bar' },
|
||||||
|
{ key: 'baz-0', name: 'baz' },
|
||||||
|
{ key: 'qux-0', name: 'qux' },
|
||||||
|
],
|
||||||
|
stale: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForRouteFocus(
|
||||||
|
{
|
||||||
|
index: 0,
|
||||||
|
key: 'drawer-test',
|
||||||
|
isDrawerOpen: true,
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-0', name: 'bar' },
|
||||||
|
{ key: 'baz-0', name: 'baz' },
|
||||||
|
{ key: 'qux-0', name: 'qux' },
|
||||||
|
],
|
||||||
|
stale: false,
|
||||||
|
},
|
||||||
|
'baz-0'
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
index: 1,
|
||||||
|
isDrawerOpen: false,
|
||||||
|
key: 'drawer-test',
|
||||||
|
routeKeyHistory: ['bar-0'],
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-0', name: 'bar' },
|
||||||
|
{ key: 'baz-0', name: 'baz' },
|
||||||
|
{ key: 'qux-0', name: 'qux' },
|
||||||
|
],
|
||||||
|
stale: false,
|
||||||
|
});
|
||||||
|
});
|
||||||
496
packages/routers/__tests__/StackRouter.test.tsx
Normal file
496
packages/routers/__tests__/StackRouter.test.tsx
Normal file
@@ -0,0 +1,496 @@
|
|||||||
|
import { CommonActions } from '@react-navigation/core';
|
||||||
|
import { StackRouter, StackActions } from '../src';
|
||||||
|
|
||||||
|
jest.mock('shortid', () => () => 'test');
|
||||||
|
|
||||||
|
it('gets initial state from route names and params with initialRouteName', () => {
|
||||||
|
const router = StackRouter({ initialRouteName: 'baz' });
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getInitialState({
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routeParamList: {
|
||||||
|
baz: { answer: 42 },
|
||||||
|
qux: { name: 'Jane' },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
).toEqual({
|
||||||
|
index: 0,
|
||||||
|
key: 'stack-test',
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [{ key: 'baz-test', name: 'baz', params: { answer: 42 } }],
|
||||||
|
stale: false,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('gets initial state from route names and params without initialRouteName', () => {
|
||||||
|
const router = StackRouter({});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getInitialState({
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routeParamList: {
|
||||||
|
baz: { answer: 42 },
|
||||||
|
qux: { name: 'Jane' },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
).toEqual({
|
||||||
|
index: 0,
|
||||||
|
key: 'stack-test',
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [{ key: 'bar-test', name: 'bar' }],
|
||||||
|
stale: false,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('gets rehydrated state from partial state', () => {
|
||||||
|
const router = StackRouter({});
|
||||||
|
|
||||||
|
const options = {
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routeParamList: {
|
||||||
|
baz: { answer: 42 },
|
||||||
|
qux: { name: 'Jane' },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getRehydratedState(
|
||||||
|
{
|
||||||
|
routes: [{ key: 'bar-0', name: 'bar' }, { key: 'qux-1', name: 'qux' }],
|
||||||
|
},
|
||||||
|
options
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
index: 1,
|
||||||
|
key: 'stack-test',
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-0', name: 'bar' },
|
||||||
|
{ key: 'qux-1', name: 'qux', params: { name: 'Jane' } },
|
||||||
|
],
|
||||||
|
stale: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getRehydratedState(
|
||||||
|
{
|
||||||
|
index: 2,
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-0', name: 'bar' },
|
||||||
|
{ key: 'baz-1', name: 'baz' },
|
||||||
|
{ key: 'qux-2', name: 'qux' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
options
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
index: 2,
|
||||||
|
key: 'stack-test',
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-0', name: 'bar' },
|
||||||
|
{ key: 'baz-1', name: 'baz', params: { answer: 42 } },
|
||||||
|
{ key: 'qux-2', name: 'qux', params: { name: 'Jane' } },
|
||||||
|
],
|
||||||
|
stale: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getRehydratedState(
|
||||||
|
{
|
||||||
|
index: 4,
|
||||||
|
routes: [],
|
||||||
|
},
|
||||||
|
options
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
index: 0,
|
||||||
|
key: 'stack-test',
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [{ key: 'bar-test', name: 'bar' }],
|
||||||
|
stale: false,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("doesn't rehydrate state if it's not stale", () => {
|
||||||
|
const router = StackRouter({});
|
||||||
|
|
||||||
|
const state = {
|
||||||
|
index: 0,
|
||||||
|
key: 'stack-test',
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [{ key: 'bar-test', name: 'bar' }],
|
||||||
|
stale: false as const,
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getRehydratedState(state, {
|
||||||
|
routeNames: [],
|
||||||
|
routeParamList: {},
|
||||||
|
})
|
||||||
|
).toBe(state);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('gets state on route names change', () => {
|
||||||
|
const router = StackRouter({});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForRouteNamesChange(
|
||||||
|
{
|
||||||
|
index: 0,
|
||||||
|
key: 'stack-test',
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-test', name: 'bar' },
|
||||||
|
{ key: 'baz-test', name: 'baz', params: { answer: 42 } },
|
||||||
|
{ key: 'qux-test', name: 'qux', params: { name: 'Jane' } },
|
||||||
|
],
|
||||||
|
stale: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
routeNames: ['qux', 'baz', 'foo', 'fiz'],
|
||||||
|
routeParamList: {
|
||||||
|
qux: { name: 'John' },
|
||||||
|
fiz: { fruit: 'apple' },
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
index: 0,
|
||||||
|
key: 'stack-test',
|
||||||
|
routeNames: ['qux', 'baz', 'foo', 'fiz'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'baz-test', name: 'baz', params: { answer: 42 } },
|
||||||
|
{ key: 'qux-test', name: 'qux', params: { name: 'Jane' } },
|
||||||
|
],
|
||||||
|
stale: false,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles navigate action', () => {
|
||||||
|
const router = StackRouter({});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 1,
|
||||||
|
routeNames: ['baz', 'bar', 'qux'],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
},
|
||||||
|
CommonActions.navigate('qux', { answer: 42 })
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 2,
|
||||||
|
routeNames: ['baz', 'bar', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'baz', name: 'baz' },
|
||||||
|
{ key: 'bar', name: 'bar' },
|
||||||
|
{
|
||||||
|
key: 'qux-test',
|
||||||
|
name: 'qux',
|
||||||
|
params: { answer: 42 },
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 1,
|
||||||
|
routeNames: ['baz', 'bar', 'qux'],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
},
|
||||||
|
CommonActions.navigate('baz', { answer: 42 })
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 0,
|
||||||
|
routeNames: ['baz', 'bar', 'qux'],
|
||||||
|
routes: [{ key: 'baz', name: 'baz', params: { answer: 42 } }],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 1,
|
||||||
|
routeNames: ['baz', 'bar', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'baz', name: 'baz' },
|
||||||
|
{ key: 'bar', name: 'bar', params: { answer: 42 } },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
CommonActions.navigate('bar', { answer: 96 })
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 1,
|
||||||
|
routeNames: ['baz', 'bar', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'baz', name: 'baz' },
|
||||||
|
{ key: 'bar', name: 'bar', params: { answer: 96 } },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 1,
|
||||||
|
routeNames: ['baz', 'bar', 'qux'],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
},
|
||||||
|
CommonActions.navigate('unknown')
|
||||||
|
)
|
||||||
|
).toBe(null);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 1,
|
||||||
|
routeNames: ['baz', 'bar', 'qux'],
|
||||||
|
routes: [{ key: 'baz-0', name: 'baz' }, { key: 'bar-0', name: 'bar' }],
|
||||||
|
},
|
||||||
|
CommonActions.navigate({ key: 'unknown' })
|
||||||
|
)
|
||||||
|
).toBe(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles go back action', () => {
|
||||||
|
const router = StackRouter({});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 1,
|
||||||
|
routeNames: ['baz', 'bar', 'qux'],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
},
|
||||||
|
CommonActions.goBack()
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 0,
|
||||||
|
routeNames: ['baz', 'bar', 'qux'],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 0,
|
||||||
|
routeNames: ['baz', 'bar', 'qux'],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }],
|
||||||
|
},
|
||||||
|
CommonActions.goBack()
|
||||||
|
)
|
||||||
|
).toBe(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles pop action', () => {
|
||||||
|
const router = StackRouter({});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 2,
|
||||||
|
routeNames: ['baz', 'bar', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'baz', name: 'baz' },
|
||||||
|
{ key: 'bar', name: 'bar' },
|
||||||
|
{ key: 'qux', name: 'qux' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
StackActions.pop()
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 1,
|
||||||
|
routeNames: ['baz', 'bar', 'qux'],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 2,
|
||||||
|
routeNames: ['baz', 'bar', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'baz', name: 'baz' },
|
||||||
|
{ key: 'bar', name: 'bar' },
|
||||||
|
{ key: 'qux', name: 'qux' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
StackActions.pop(2)
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 0,
|
||||||
|
routeNames: ['baz', 'bar', 'qux'],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 2,
|
||||||
|
routeNames: ['baz', 'bar', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'baz-0', name: 'baz' },
|
||||||
|
{ key: 'bar-0', name: 'bar' },
|
||||||
|
{ key: 'qux-0', name: 'qux' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
...StackActions.pop(),
|
||||||
|
target: 'root',
|
||||||
|
source: 'bar-0',
|
||||||
|
}
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 0,
|
||||||
|
routeNames: ['baz', 'bar', 'qux'],
|
||||||
|
routes: [{ key: 'baz-0', name: 'baz' }],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 0,
|
||||||
|
routeNames: ['baz', 'bar', 'qux'],
|
||||||
|
routes: [{ key: 'baz-0', name: 'baz' }],
|
||||||
|
},
|
||||||
|
StackActions.pop()
|
||||||
|
)
|
||||||
|
).toBe(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles pop to top action', () => {
|
||||||
|
const router = StackRouter({});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 2,
|
||||||
|
routeNames: ['baz', 'bar', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'baz', name: 'baz' },
|
||||||
|
{ key: 'bar', name: 'bar' },
|
||||||
|
{ key: 'qux', name: 'qux' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
StackActions.popToTop()
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 0,
|
||||||
|
routeNames: ['baz', 'bar', 'qux'],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles push action', () => {
|
||||||
|
const router = StackRouter({});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 2,
|
||||||
|
routeNames: ['baz', 'bar', 'qux'],
|
||||||
|
routes: [{ key: 'bar', name: 'bar' }],
|
||||||
|
},
|
||||||
|
StackActions.push('baz')
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 3,
|
||||||
|
routeNames: ['baz', 'bar', 'qux'],
|
||||||
|
routes: [{ key: 'bar', name: 'bar' }, { key: 'baz-test', name: 'baz' }],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 2,
|
||||||
|
routeNames: ['baz', 'bar', 'qux'],
|
||||||
|
routes: [{ key: 'bar', name: 'bar' }],
|
||||||
|
},
|
||||||
|
StackActions.push('unknown')
|
||||||
|
)
|
||||||
|
).toBe(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('changes index on focus change', () => {
|
||||||
|
const router = StackRouter({});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForRouteFocus(
|
||||||
|
{
|
||||||
|
index: 2,
|
||||||
|
key: 'stack-test',
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-0', name: 'bar' },
|
||||||
|
{ key: 'baz-0', name: 'baz' },
|
||||||
|
{ key: 'qux-0', name: 'qux' },
|
||||||
|
],
|
||||||
|
stale: false,
|
||||||
|
},
|
||||||
|
'baz-0'
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
index: 1,
|
||||||
|
key: 'stack-test',
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [{ key: 'bar-0', name: 'bar' }, { key: 'baz-0', name: 'baz' }],
|
||||||
|
stale: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
const state = {
|
||||||
|
index: 0,
|
||||||
|
key: 'stack-test',
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [{ key: 'bar-0', name: 'bar' }, { key: 'baz-0', name: 'baz' }],
|
||||||
|
stale: false as const,
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(router.getStateForRouteFocus(state, 'qux-0')).toEqual(state);
|
||||||
|
});
|
||||||
526
packages/routers/__tests__/TabRouter.test.tsx
Normal file
526
packages/routers/__tests__/TabRouter.test.tsx
Normal file
@@ -0,0 +1,526 @@
|
|||||||
|
import { CommonActions } from '@react-navigation/core';
|
||||||
|
import { TabRouter, TabActions, TabNavigationState } from '../src';
|
||||||
|
|
||||||
|
jest.mock('shortid', () => () => 'test');
|
||||||
|
|
||||||
|
it('gets initial state from route names and params with initialRouteName', () => {
|
||||||
|
const router = TabRouter({ initialRouteName: 'baz' });
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getInitialState({
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routeParamList: {
|
||||||
|
baz: { answer: 42 },
|
||||||
|
qux: { name: 'Jane' },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
).toEqual({
|
||||||
|
index: 1,
|
||||||
|
key: 'tab-test',
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-test', name: 'bar' },
|
||||||
|
{ key: 'baz-test', name: 'baz', params: { answer: 42 } },
|
||||||
|
{ key: 'qux-test', name: 'qux', params: { name: 'Jane' } },
|
||||||
|
],
|
||||||
|
stale: false,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('gets initial state from route names and params without initialRouteName', () => {
|
||||||
|
const router = TabRouter({});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getInitialState({
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routeParamList: {
|
||||||
|
baz: { answer: 42 },
|
||||||
|
qux: { name: 'Jane' },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
).toEqual({
|
||||||
|
index: 0,
|
||||||
|
key: 'tab-test',
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-test', name: 'bar' },
|
||||||
|
{ key: 'baz-test', name: 'baz', params: { answer: 42 } },
|
||||||
|
{ key: 'qux-test', name: 'qux', params: { name: 'Jane' } },
|
||||||
|
],
|
||||||
|
stale: false,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('gets rehydrated state from partial state', () => {
|
||||||
|
const router = TabRouter({});
|
||||||
|
|
||||||
|
const options = {
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routeParamList: {
|
||||||
|
baz: { answer: 42 },
|
||||||
|
qux: { name: 'Jane' },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getRehydratedState(
|
||||||
|
{
|
||||||
|
routes: [{ key: 'bar-0', name: 'bar' }, { key: 'qux-1', name: 'qux' }],
|
||||||
|
},
|
||||||
|
options
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
index: 0,
|
||||||
|
key: 'tab-test',
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-0', name: 'bar' },
|
||||||
|
{ key: 'baz-test', name: 'baz', params: { answer: 42 } },
|
||||||
|
{ key: 'qux-1', name: 'qux', params: { name: 'Jane' } },
|
||||||
|
],
|
||||||
|
stale: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getRehydratedState(
|
||||||
|
{
|
||||||
|
index: 2,
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-0', name: 'bar' },
|
||||||
|
{ key: 'baz-1', name: 'baz' },
|
||||||
|
{ key: 'qux-2', name: 'qux' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
options
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
index: 2,
|
||||||
|
key: 'tab-test',
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-0', name: 'bar' },
|
||||||
|
{ key: 'baz-1', name: 'baz', params: { answer: 42 } },
|
||||||
|
{ key: 'qux-2', name: 'qux', params: { name: 'Jane' } },
|
||||||
|
],
|
||||||
|
stale: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getRehydratedState(
|
||||||
|
{
|
||||||
|
index: 4,
|
||||||
|
routes: [],
|
||||||
|
},
|
||||||
|
options
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
index: 0,
|
||||||
|
key: 'tab-test',
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-test', name: 'bar' },
|
||||||
|
{ key: 'baz-test', name: 'baz', params: { answer: 42 } },
|
||||||
|
{ key: 'qux-test', name: 'qux', params: { name: 'Jane' } },
|
||||||
|
],
|
||||||
|
stale: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getRehydratedState(
|
||||||
|
{
|
||||||
|
index: 1,
|
||||||
|
routeKeyHistory: ['bar-test', 'qux-test', 'foo-test'],
|
||||||
|
routes: [],
|
||||||
|
},
|
||||||
|
options
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
index: 1,
|
||||||
|
key: 'tab-test',
|
||||||
|
routeKeyHistory: ['bar-test', 'qux-test'],
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-test', name: 'bar' },
|
||||||
|
{ key: 'baz-test', name: 'baz', params: { answer: 42 } },
|
||||||
|
{ key: 'qux-test', name: 'qux', params: { name: 'Jane' } },
|
||||||
|
],
|
||||||
|
stale: false,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("doesn't rehydrate state if it's not stale", () => {
|
||||||
|
const router = TabRouter({});
|
||||||
|
|
||||||
|
const state = {
|
||||||
|
index: 0,
|
||||||
|
key: 'tab-test',
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-test', name: 'bar' },
|
||||||
|
{ key: 'baz-test', name: 'baz', params: { answer: 42 } },
|
||||||
|
{ key: 'qux-test', name: 'qux', params: { name: 'Jane' } },
|
||||||
|
],
|
||||||
|
stale: false as const,
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getRehydratedState(state, {
|
||||||
|
routeNames: [],
|
||||||
|
routeParamList: {},
|
||||||
|
})
|
||||||
|
).toBe(state);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('gets state on route names change', () => {
|
||||||
|
const router = TabRouter({});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForRouteNamesChange(
|
||||||
|
{
|
||||||
|
index: 0,
|
||||||
|
key: 'tab-test',
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-test', name: 'bar' },
|
||||||
|
{ key: 'baz-test', name: 'baz', params: { answer: 42 } },
|
||||||
|
{ key: 'qux-test', name: 'qux', params: { name: 'Jane' } },
|
||||||
|
],
|
||||||
|
stale: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
routeNames: ['qux', 'baz', 'foo', 'fiz'],
|
||||||
|
routeParamList: {
|
||||||
|
qux: { name: 'John' },
|
||||||
|
fiz: { fruit: 'apple' },
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
index: 0,
|
||||||
|
key: 'tab-test',
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routeNames: ['qux', 'baz', 'foo', 'fiz'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'qux-test', name: 'qux', params: { name: 'Jane' } },
|
||||||
|
{ key: 'baz-test', name: 'baz', params: { answer: 42 } },
|
||||||
|
{ key: 'foo-test', name: 'foo' },
|
||||||
|
{ key: 'fiz-test', name: 'fiz', params: { fruit: 'apple' } },
|
||||||
|
],
|
||||||
|
stale: false,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles navigate action', () => {
|
||||||
|
const router = TabRouter({});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 1,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routes: [{ key: 'baz-1', name: 'baz' }, { key: 'bar-1', name: 'bar' }],
|
||||||
|
},
|
||||||
|
CommonActions.navigate({ key: 'bar-1', params: { answer: 42 } })
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 1,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
routeKeyHistory: ['bar-1'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'baz-1', name: 'baz' },
|
||||||
|
{ key: 'bar-1', name: 'bar', params: { answer: 42 } },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 1,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
},
|
||||||
|
CommonActions.navigate('baz', { answer: 42 })
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 0,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
routeKeyHistory: ['bar'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'baz', name: 'baz', params: { answer: 42 } },
|
||||||
|
{ key: 'bar', name: 'bar' },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 1,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
},
|
||||||
|
CommonActions.navigate('non-existent')
|
||||||
|
)
|
||||||
|
).toBe(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles jump to action', () => {
|
||||||
|
const router = TabRouter({});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 0,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
},
|
||||||
|
TabActions.jumpTo('bar')
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 1,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
routeKeyHistory: ['baz'],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles back action with backBehavior: history', () => {
|
||||||
|
const router = TabRouter({ backBehavior: 'history' });
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 0,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
routeKeyHistory: ['bar'],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
},
|
||||||
|
CommonActions.goBack()
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 1,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 0,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
},
|
||||||
|
CommonActions.goBack()
|
||||||
|
)
|
||||||
|
).toBe(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles back action with backBehavior: order', () => {
|
||||||
|
const router = TabRouter({ backBehavior: 'order' });
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 1,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
},
|
||||||
|
CommonActions.goBack()
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 0,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 0,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
},
|
||||||
|
CommonActions.goBack()
|
||||||
|
)
|
||||||
|
).toBe(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles back action with backBehavior: initialRoute', () => {
|
||||||
|
const router = TabRouter({
|
||||||
|
backBehavior: 'initialRoute',
|
||||||
|
initialRouteName: 'bar',
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 0,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
},
|
||||||
|
CommonActions.goBack()
|
||||||
|
)
|
||||||
|
).toEqual({
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 1,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 1,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
},
|
||||||
|
CommonActions.goBack()
|
||||||
|
)
|
||||||
|
).toBe(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles back action with backBehavior: none', () => {
|
||||||
|
const router = TabRouter({ backBehavior: 'none' });
|
||||||
|
|
||||||
|
expect(
|
||||||
|
router.getStateForAction(
|
||||||
|
{
|
||||||
|
stale: false,
|
||||||
|
key: 'root',
|
||||||
|
index: 0,
|
||||||
|
routeNames: ['baz', 'bar'],
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
|
||||||
|
},
|
||||||
|
CommonActions.goBack()
|
||||||
|
)
|
||||||
|
).toEqual(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('updates route key history on navigate and jump to', () => {
|
||||||
|
const router = TabRouter({ backBehavior: 'history' });
|
||||||
|
|
||||||
|
let state: TabNavigationState = {
|
||||||
|
index: 1,
|
||||||
|
key: 'tab-test',
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-0', name: 'bar' },
|
||||||
|
{ key: 'baz-0', name: 'baz', params: { answer: 42 } },
|
||||||
|
{ key: 'qux-0', name: 'qux', params: { name: 'Jane' } },
|
||||||
|
],
|
||||||
|
stale: false as const,
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(state.routeKeyHistory).toEqual([]);
|
||||||
|
|
||||||
|
state = router.getStateForAction(
|
||||||
|
state,
|
||||||
|
TabActions.jumpTo('qux')
|
||||||
|
) as TabNavigationState;
|
||||||
|
|
||||||
|
expect(state.routeKeyHistory).toEqual(['baz-0']);
|
||||||
|
|
||||||
|
state = router.getStateForAction(
|
||||||
|
state,
|
||||||
|
CommonActions.navigate('bar')
|
||||||
|
) as TabNavigationState;
|
||||||
|
|
||||||
|
expect(state.routeKeyHistory).toEqual(['baz-0', 'qux-0']);
|
||||||
|
|
||||||
|
state = router.getStateForAction(
|
||||||
|
state,
|
||||||
|
TabActions.jumpTo('baz')
|
||||||
|
) as TabNavigationState;
|
||||||
|
|
||||||
|
expect(state.routeKeyHistory).toEqual(['qux-0', 'bar-0']);
|
||||||
|
|
||||||
|
state = router.getStateForAction(
|
||||||
|
state,
|
||||||
|
CommonActions.goBack()
|
||||||
|
) as TabNavigationState;
|
||||||
|
|
||||||
|
expect(state.routeKeyHistory).toEqual(['qux-0']);
|
||||||
|
|
||||||
|
state = router.getStateForAction(
|
||||||
|
state,
|
||||||
|
CommonActions.goBack()
|
||||||
|
) as TabNavigationState;
|
||||||
|
|
||||||
|
expect(state.routeKeyHistory).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('updates route key history on focus change', () => {
|
||||||
|
const router = TabRouter({ backBehavior: 'history' });
|
||||||
|
|
||||||
|
const state = {
|
||||||
|
index: 0,
|
||||||
|
key: 'tab-test',
|
||||||
|
routeKeyHistory: [],
|
||||||
|
routeNames: ['bar', 'baz', 'qux'],
|
||||||
|
routes: [
|
||||||
|
{ key: 'bar-0', name: 'bar' },
|
||||||
|
{ key: 'baz-0', name: 'baz', params: { answer: 42 } },
|
||||||
|
{ key: 'qux-0', name: 'qux', params: { name: 'Jane' } },
|
||||||
|
],
|
||||||
|
stale: false as const,
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(router.getStateForRouteFocus(state, 'bar-0').routeKeyHistory).toEqual(
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(router.getStateForRouteFocus(state, 'baz-0').routeKeyHistory).toEqual([
|
||||||
|
'bar-0',
|
||||||
|
]);
|
||||||
|
});
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
"react-native",
|
"react-native",
|
||||||
"react-navigation"
|
"react-navigation"
|
||||||
],
|
],
|
||||||
"version": "5.0.0-alpha.2",
|
"version": "5.0.0-alpha.5",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -16,7 +16,7 @@
|
|||||||
"main": "lib/commonjs/index.js",
|
"main": "lib/commonjs/index.js",
|
||||||
"react-native": "src/index.tsx",
|
"react-native": "src/index.tsx",
|
||||||
"module": "lib/module/index.js",
|
"module": "lib/module/index.js",
|
||||||
"types": "lib/typescript/src/index.d.ts",
|
"types": "lib/typescript/routers/src/index.d.ts",
|
||||||
"files": [
|
"files": [
|
||||||
"src",
|
"src",
|
||||||
"lib"
|
"lib"
|
||||||
|
|||||||
@@ -3,6 +3,46 @@
|
|||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
||||||
|
|
||||||
|
# [5.0.0-alpha.6](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/stack@5.0.0-alpha.5...@react-navigation/stack@5.0.0-alpha.6) (2019-08-28)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* disable gesture logic when no gesture stack ([38336b0](https://github.com/react-navigation/navigation-ex/commit/38336b0))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# [5.0.0-alpha.5](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/stack@5.0.0-alpha.4...@react-navigation/stack@5.0.0-alpha.5) (2019-08-27)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* link proper descriptor for StackView ([469ec31](https://github.com/react-navigation/navigation-ex/commit/469ec31))
|
||||||
|
* set correct pointer events when active prop changes ([1bbd6ac](https://github.com/react-navigation/navigation-ex/commit/1bbd6ac))
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* add hook to scroll to top on tab press ([9e1104c](https://github.com/react-navigation/navigation-ex/commit/9e1104c))
|
||||||
|
* add memoization of spring for stack ([7990cf2](https://github.com/react-navigation/navigation-ex/commit/7990cf2))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# [5.0.0-alpha.4](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/stack@5.0.0-alpha.3...@react-navigation/stack@5.0.0-alpha.4) (2019-08-22)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* fix path to typescript definitions ([f182315](https://github.com/react-navigation/navigation-ex/commit/f182315))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# [5.0.0-alpha.3](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/stack@5.0.0-alpha.2...@react-navigation/stack@5.0.0-alpha.3) (2019-08-22)
|
# [5.0.0-alpha.3](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/stack@5.0.0-alpha.2...@react-navigation/stack@5.0.0-alpha.3) (2019-08-22)
|
||||||
|
|
||||||
**Note:** Version bump only for package @react-navigation/stack
|
**Note:** Version bump only for package @react-navigation/stack
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
"android",
|
"android",
|
||||||
"stack"
|
"stack"
|
||||||
],
|
],
|
||||||
"version": "5.0.0-alpha.3",
|
"version": "5.0.0-alpha.6",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -20,7 +20,7 @@
|
|||||||
"main": "lib/commonjs/index.js",
|
"main": "lib/commonjs/index.js",
|
||||||
"react-native": "src/index.tsx",
|
"react-native": "src/index.tsx",
|
||||||
"module": "lib/module/index.js",
|
"module": "lib/module/index.js",
|
||||||
"types": "lib/typescript/src/index.d.ts",
|
"types": "lib/typescript/stack/src/index.d.ts",
|
||||||
"files": [
|
"files": [
|
||||||
"src",
|
"src",
|
||||||
"lib"
|
"lib"
|
||||||
@@ -33,7 +33,7 @@
|
|||||||
"clean": "del lib"
|
"clean": "del lib"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@react-navigation/routers": "^5.0.0-alpha.2",
|
"@react-navigation/routers": "^5.0.0-alpha.5",
|
||||||
"react-native-safe-area-view": "^0.14.6"
|
"react-native-safe-area-view": "^0.14.6"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@@ -13,7 +13,11 @@ import {
|
|||||||
} from '@react-navigation/routers';
|
} from '@react-navigation/routers';
|
||||||
import KeyboardManager from '../views/KeyboardManager';
|
import KeyboardManager from '../views/KeyboardManager';
|
||||||
import StackView from '../views/Stack/StackView';
|
import StackView from '../views/Stack/StackView';
|
||||||
import { StackNavigationConfig, StackNavigationOptions } from '../types';
|
import {
|
||||||
|
StackNavigationConfig,
|
||||||
|
StackNavigationOptions,
|
||||||
|
StackNavigationEventMap,
|
||||||
|
} from '../types';
|
||||||
|
|
||||||
type Props = DefaultNavigatorOptions<StackNavigationOptions> &
|
type Props = DefaultNavigatorOptions<StackNavigationOptions> &
|
||||||
StackRouterOptions &
|
StackRouterOptions &
|
||||||
@@ -28,8 +32,9 @@ function StackNavigator({
|
|||||||
}: Props) {
|
}: Props) {
|
||||||
const { state, descriptors, navigation } = useNavigationBuilder<
|
const { state, descriptors, navigation } = useNavigationBuilder<
|
||||||
StackNavigationState,
|
StackNavigationState,
|
||||||
|
StackRouterOptions,
|
||||||
StackNavigationOptions,
|
StackNavigationOptions,
|
||||||
StackRouterOptions
|
StackNavigationEventMap
|
||||||
>(StackRouter, {
|
>(StackRouter, {
|
||||||
initialRouteName,
|
initialRouteName,
|
||||||
children,
|
children,
|
||||||
@@ -39,13 +44,23 @@ function StackNavigator({
|
|||||||
React.useEffect(
|
React.useEffect(
|
||||||
() =>
|
() =>
|
||||||
navigation.addListener &&
|
navigation.addListener &&
|
||||||
navigation.addListener('refocus', (e: EventArg<'refocus', undefined>) => {
|
navigation.addListener('tabPress', (e: EventArg<'tabPress'>) => {
|
||||||
if (state.index > 0 && !e.defaultPrevented) {
|
// Run the operation in the next frame so we're sure all listeners have been run
|
||||||
navigation.dispatch({
|
// This is necessary to know if preventDefault() has been called
|
||||||
...StackActions.popToTop(),
|
requestAnimationFrame(() => {
|
||||||
target: state.key,
|
if (
|
||||||
});
|
state.index > 0 &&
|
||||||
}
|
navigation.isFocused() &&
|
||||||
|
!e.defaultPrevented
|
||||||
|
) {
|
||||||
|
// When user taps on already focused tab and we're inside the tab,
|
||||||
|
// reset the stack to replicate native behaviour
|
||||||
|
navigation.dispatch({
|
||||||
|
...StackActions.popToTop(),
|
||||||
|
target: state.key,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
}),
|
}),
|
||||||
[navigation, state.index, state.key]
|
[navigation, state.index, state.key]
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import {
|
|||||||
ParamListBase,
|
ParamListBase,
|
||||||
Descriptor,
|
Descriptor,
|
||||||
Route,
|
Route,
|
||||||
|
NavigationHelpers,
|
||||||
} from '@react-navigation/core';
|
} from '@react-navigation/core';
|
||||||
import { StackNavigationState } from '@react-navigation/routers';
|
import { StackNavigationState } from '@react-navigation/routers';
|
||||||
|
|
||||||
@@ -24,6 +25,11 @@ export type StackNavigationEventMap = {
|
|||||||
transitionEnd: { closing: boolean };
|
transitionEnd: { closing: boolean };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type StackNavigationHelpers = NavigationHelpers<
|
||||||
|
ParamListBase,
|
||||||
|
StackNavigationEventMap
|
||||||
|
>;
|
||||||
|
|
||||||
export type StackNavigationProp<
|
export type StackNavigationProp<
|
||||||
ParamList extends ParamListBase,
|
ParamList extends ParamListBase,
|
||||||
RouteName extends keyof ParamList = string
|
RouteName extends keyof ParamList = string
|
||||||
|
|||||||
@@ -94,6 +94,86 @@ const {
|
|||||||
Value,
|
Value,
|
||||||
} = Animated;
|
} = Animated;
|
||||||
|
|
||||||
|
// We need to be prepared for both version of reanimated. With and w/out proc
|
||||||
|
let memoizedSpring = spring;
|
||||||
|
// @ts-ignore
|
||||||
|
if (Animated.proc) {
|
||||||
|
// @ts-ignore
|
||||||
|
const springHelper = Animated.proc(
|
||||||
|
(
|
||||||
|
finished: Animated.Value<number>,
|
||||||
|
velocity: Animated.Value<number>,
|
||||||
|
position: Animated.Value<number>,
|
||||||
|
time: Animated.Value<number>,
|
||||||
|
prevPosition: Animated.Value<number>,
|
||||||
|
toValue: Animated.Adaptable<number>,
|
||||||
|
damping: Animated.Adaptable<number>,
|
||||||
|
mass: Animated.Adaptable<number>,
|
||||||
|
stiffness: Animated.Adaptable<number>,
|
||||||
|
overshootClamping: Animated.Adaptable<number>,
|
||||||
|
restSpeedThreshold: Animated.Adaptable<number>,
|
||||||
|
restDisplacementThreshold: Animated.Adaptable<number>,
|
||||||
|
clock: Animated.Clock
|
||||||
|
) =>
|
||||||
|
spring(
|
||||||
|
clock,
|
||||||
|
{
|
||||||
|
finished,
|
||||||
|
velocity,
|
||||||
|
position,
|
||||||
|
time,
|
||||||
|
// @ts-ignore
|
||||||
|
prevPosition,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
toValue,
|
||||||
|
damping,
|
||||||
|
mass,
|
||||||
|
stiffness,
|
||||||
|
overshootClamping,
|
||||||
|
restDisplacementThreshold,
|
||||||
|
restSpeedThreshold,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
|
memoizedSpring = function(
|
||||||
|
clock: Animated.Clock,
|
||||||
|
state: {
|
||||||
|
finished: Animated.Value<number>;
|
||||||
|
velocity: Animated.Value<number>;
|
||||||
|
position: Animated.Value<number>;
|
||||||
|
time: Animated.Value<number>;
|
||||||
|
},
|
||||||
|
config: {
|
||||||
|
toValue: Animated.Adaptable<number>;
|
||||||
|
damping: Animated.Adaptable<number>;
|
||||||
|
mass: Animated.Adaptable<number>;
|
||||||
|
stiffness: Animated.Adaptable<number>;
|
||||||
|
overshootClamping: Animated.Adaptable<boolean>;
|
||||||
|
restSpeedThreshold: Animated.Adaptable<number>;
|
||||||
|
restDisplacementThreshold: Animated.Adaptable<number>;
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
return springHelper(
|
||||||
|
state.finished,
|
||||||
|
state.velocity,
|
||||||
|
state.position,
|
||||||
|
state.time,
|
||||||
|
new Value(0),
|
||||||
|
config.toValue,
|
||||||
|
config.damping,
|
||||||
|
config.mass,
|
||||||
|
config.stiffness,
|
||||||
|
config.overshootClamping,
|
||||||
|
config.restSpeedThreshold,
|
||||||
|
config.restDisplacementThreshold,
|
||||||
|
clock
|
||||||
|
);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export default class Card extends React.Component<Props> {
|
export default class Card extends React.Component<Props> {
|
||||||
static defaultProps = {
|
static defaultProps = {
|
||||||
overlayEnabled: Platform.OS !== 'ios',
|
overlayEnabled: Platform.OS !== 'ios',
|
||||||
@@ -219,7 +299,7 @@ export default class Card extends React.Component<Props> {
|
|||||||
cond(
|
cond(
|
||||||
eq(isVisible, 1),
|
eq(isVisible, 1),
|
||||||
openingSpec.timing === 'spring'
|
openingSpec.timing === 'spring'
|
||||||
? spring(
|
? memoizedSpring(
|
||||||
this.clock,
|
this.clock,
|
||||||
{ ...this.transitionState, velocity: this.transitionVelocity },
|
{ ...this.transitionState, velocity: this.transitionVelocity },
|
||||||
{ ...openingSpec.config, toValue: this.toValue }
|
{ ...openingSpec.config, toValue: this.toValue }
|
||||||
@@ -230,7 +310,7 @@ export default class Card extends React.Component<Props> {
|
|||||||
{ ...openingSpec.config, toValue: this.toValue }
|
{ ...openingSpec.config, toValue: this.toValue }
|
||||||
),
|
),
|
||||||
closingSpec.timing === 'spring'
|
closingSpec.timing === 'spring'
|
||||||
? spring(
|
? memoizedSpring(
|
||||||
this.clock,
|
this.clock,
|
||||||
{ ...this.transitionState, velocity: this.transitionVelocity },
|
{ ...this.transitionState, velocity: this.transitionVelocity },
|
||||||
{ ...closingSpec.config, toValue: this.toValue }
|
{ ...closingSpec.config, toValue: this.toValue }
|
||||||
@@ -286,6 +366,15 @@ export default class Card extends React.Component<Props> {
|
|||||||
set(this.nextIsVisible, UNSET),
|
set(this.nextIsVisible, UNSET),
|
||||||
])
|
])
|
||||||
),
|
),
|
||||||
|
onChange(
|
||||||
|
this.isVisible,
|
||||||
|
call([this.isVisible], ([isVisible]) => (this.isVisibleValue = isVisible))
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
|
||||||
|
private execNoGesture = this.runTransition(this.isVisible);
|
||||||
|
|
||||||
|
private execWithGesture = block([
|
||||||
onChange(
|
onChange(
|
||||||
this.isSwiping,
|
this.isSwiping,
|
||||||
call(
|
call(
|
||||||
@@ -374,10 +463,6 @@ export default class Card extends React.Component<Props> {
|
|||||||
),
|
),
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
onChange(
|
|
||||||
this.isVisible,
|
|
||||||
call([this.isVisible], ([isVisible]) => (this.isVisibleValue = isVisible))
|
|
||||||
),
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
private handleGestureEventHorizontal = Animated.event([
|
private handleGestureEventHorizontal = Animated.event([
|
||||||
@@ -496,15 +581,21 @@ export default class Card extends React.Component<Props> {
|
|||||||
layout
|
layout
|
||||||
);
|
);
|
||||||
|
|
||||||
const handleGestureEvent =
|
const handleGestureEvent = gestureEnabled
|
||||||
gestureDirection === 'vertical'
|
? gestureDirection === 'vertical'
|
||||||
? this.handleGestureEventVertical
|
? this.handleGestureEventVertical
|
||||||
: this.handleGestureEventHorizontal;
|
: this.handleGestureEventHorizontal
|
||||||
|
: undefined;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<StackGestureContext.Provider value={this.gestureRef}>
|
<StackGestureContext.Provider value={this.gestureRef}>
|
||||||
<View pointerEvents="box-none" {...rest}>
|
<View pointerEvents="box-none" {...rest}>
|
||||||
<Animated.Code exec={this.exec} />
|
<Animated.Code exec={this.exec} />
|
||||||
|
{this.props.gestureEnabled ? (
|
||||||
|
<Animated.Code exec={this.execNoGesture} />
|
||||||
|
) : (
|
||||||
|
<Animated.Code exec={this.execWithGesture} />
|
||||||
|
)}
|
||||||
{overlayEnabled && overlayStyle ? (
|
{overlayEnabled && overlayStyle ? (
|
||||||
<Animated.View
|
<Animated.View
|
||||||
pointerEvents="none"
|
pointerEvents="none"
|
||||||
|
|||||||
@@ -23,6 +23,13 @@ const { block, greaterThan, cond, set, call, onChange } = Animated;
|
|||||||
* whenever position changes.
|
* whenever position changes.
|
||||||
*/
|
*/
|
||||||
export default class PointerEventsView extends React.Component<Props> {
|
export default class PointerEventsView extends React.Component<Props> {
|
||||||
|
componentDidUpdate(prevProps: Props) {
|
||||||
|
if (this.props.active !== prevProps.active) {
|
||||||
|
this.pointerEventsEnabled.setValue(this.props.active ? TRUE : FALSE);
|
||||||
|
this.setPointerEventsEnabled(this.props.active);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private pointerEventsEnabled = new Animated.Value<Binary>(
|
private pointerEventsEnabled = new Animated.Value<Binary>(
|
||||||
this.props.active ? TRUE : FALSE
|
this.props.active ? TRUE : FALSE
|
||||||
);
|
);
|
||||||
@@ -40,13 +47,17 @@ export default class PointerEventsView extends React.Component<Props> {
|
|||||||
onChange(
|
onChange(
|
||||||
this.pointerEventsEnabled,
|
this.pointerEventsEnabled,
|
||||||
call([this.pointerEventsEnabled], ([value]) => {
|
call([this.pointerEventsEnabled], ([value]) => {
|
||||||
const pointerEvents = this.props.active && value ? 'box-none' : 'none';
|
this.setPointerEventsEnabled(Boolean(this.props.active && value));
|
||||||
|
|
||||||
this.root && this.root.setNativeProps({ pointerEvents });
|
|
||||||
})
|
})
|
||||||
),
|
),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
private setPointerEventsEnabled = (enabled: boolean) => {
|
||||||
|
const pointerEvents = enabled ? 'box-none' : 'none';
|
||||||
|
|
||||||
|
this.root && this.root.setNativeProps({ pointerEvents });
|
||||||
|
};
|
||||||
|
|
||||||
private root: View | null = null;
|
private root: View | null = null;
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
|||||||
@@ -10,11 +10,7 @@ import {
|
|||||||
import Animated from 'react-native-reanimated';
|
import Animated from 'react-native-reanimated';
|
||||||
// eslint-disable-next-line import/no-unresolved
|
// eslint-disable-next-line import/no-unresolved
|
||||||
import * as Screens from 'react-native-screens'; // Import with * as to prevent getters being called
|
import * as Screens from 'react-native-screens'; // Import with * as to prevent getters being called
|
||||||
import {
|
import { Route } from '@react-navigation/core';
|
||||||
Route,
|
|
||||||
NavigationHelpers,
|
|
||||||
ParamListBase,
|
|
||||||
} from '@react-navigation/core';
|
|
||||||
import { StackNavigationState } from '@react-navigation/routers';
|
import { StackNavigationState } from '@react-navigation/routers';
|
||||||
|
|
||||||
import { getDefaultHeaderHeight } from '../Header/HeaderSegment';
|
import { getDefaultHeaderHeight } from '../Header/HeaderSegment';
|
||||||
@@ -31,6 +27,7 @@ import {
|
|||||||
HeaderScene,
|
HeaderScene,
|
||||||
StackDescriptorMap,
|
StackDescriptorMap,
|
||||||
StackNavigationOptions,
|
StackNavigationOptions,
|
||||||
|
StackNavigationHelpers,
|
||||||
} from '../../types';
|
} from '../../types';
|
||||||
|
|
||||||
type ProgressValues = {
|
type ProgressValues = {
|
||||||
@@ -40,7 +37,7 @@ type ProgressValues = {
|
|||||||
type Props = {
|
type Props = {
|
||||||
mode: 'card' | 'modal';
|
mode: 'card' | 'modal';
|
||||||
state: StackNavigationState;
|
state: StackNavigationState;
|
||||||
navigation: NavigationHelpers<ParamListBase>;
|
navigation: StackNavigationHelpers;
|
||||||
descriptors: StackDescriptorMap;
|
descriptors: StackDescriptorMap;
|
||||||
routes: Route<string>[];
|
routes: Route<string>[];
|
||||||
openingRoutes: string[];
|
openingRoutes: string[];
|
||||||
@@ -331,8 +328,8 @@ export default class Stack extends React.Component<Props, State> {
|
|||||||
{routes.map((route, index, self) => {
|
{routes.map((route, index, self) => {
|
||||||
const focused = focusedRoute.key === route.key;
|
const focused = focusedRoute.key === route.key;
|
||||||
const current = progress[route.key];
|
const current = progress[route.key];
|
||||||
const descriptor = descriptors[route.key];
|
|
||||||
const scene = scenes[index];
|
const scene = scenes[index];
|
||||||
|
const descriptor = scene.descriptor;
|
||||||
const next = self[index + 1]
|
const next = self[index + 1]
|
||||||
? progress[self[index + 1].key]
|
? progress[self[index + 1].key]
|
||||||
: ANIMATED_ONE;
|
: ANIMATED_ONE;
|
||||||
|
|||||||
@@ -2,14 +2,16 @@ import * as React from 'react';
|
|||||||
import { StyleSheet, Platform, StyleProp, ViewStyle } from 'react-native';
|
import { StyleSheet, Platform, StyleProp, ViewStyle } from 'react-native';
|
||||||
import Animated from 'react-native-reanimated';
|
import Animated from 'react-native-reanimated';
|
||||||
import { StackNavigationState } from '@react-navigation/routers';
|
import { StackNavigationState } from '@react-navigation/routers';
|
||||||
import {
|
import { Route } from '@react-navigation/core';
|
||||||
Route,
|
|
||||||
NavigationHelpers,
|
|
||||||
ParamListBase,
|
|
||||||
} from '@react-navigation/core';
|
|
||||||
import { Props as HeaderContainerProps } from '../Header/HeaderContainer';
|
import { Props as HeaderContainerProps } from '../Header/HeaderContainer';
|
||||||
import Card from './Card';
|
import Card from './Card';
|
||||||
import { HeaderScene, Layout, HeaderMode, TransitionPreset } from '../../types';
|
import {
|
||||||
|
StackNavigationHelpers,
|
||||||
|
HeaderScene,
|
||||||
|
Layout,
|
||||||
|
HeaderMode,
|
||||||
|
TransitionPreset,
|
||||||
|
} from '../../types';
|
||||||
|
|
||||||
type Props = TransitionPreset & {
|
type Props = TransitionPreset & {
|
||||||
index: number;
|
index: number;
|
||||||
@@ -21,7 +23,7 @@ type Props = TransitionPreset & {
|
|||||||
previousScene?: HeaderScene<Route<string>>;
|
previousScene?: HeaderScene<Route<string>>;
|
||||||
scene: HeaderScene<Route<string>>;
|
scene: HeaderScene<Route<string>>;
|
||||||
state: StackNavigationState;
|
state: StackNavigationState;
|
||||||
navigation: NavigationHelpers<ParamListBase>;
|
navigation: StackNavigationHelpers;
|
||||||
cardTransparent?: boolean;
|
cardTransparent?: boolean;
|
||||||
cardOverlayEnabled?: boolean;
|
cardOverlayEnabled?: boolean;
|
||||||
cardShadowEnabled?: boolean;
|
cardShadowEnabled?: boolean;
|
||||||
|
|||||||
@@ -1,21 +1,21 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { Platform } from 'react-native';
|
import { Platform } from 'react-native';
|
||||||
import {
|
import { Route } from '@react-navigation/core';
|
||||||
ParamListBase,
|
|
||||||
Route,
|
|
||||||
NavigationHelpers,
|
|
||||||
} from '@react-navigation/core';
|
|
||||||
import { StackActions, StackNavigationState } from '@react-navigation/routers';
|
import { StackActions, StackNavigationState } from '@react-navigation/routers';
|
||||||
|
|
||||||
import Stack from './Stack';
|
import Stack from './Stack';
|
||||||
import HeaderContainer, {
|
import HeaderContainer, {
|
||||||
Props as HeaderContainerProps,
|
Props as HeaderContainerProps,
|
||||||
} from '../Header/HeaderContainer';
|
} from '../Header/HeaderContainer';
|
||||||
import { StackNavigationConfig, StackDescriptorMap } from '../../types';
|
import {
|
||||||
|
StackNavigationHelpers,
|
||||||
|
StackNavigationConfig,
|
||||||
|
StackDescriptorMap,
|
||||||
|
} from '../../types';
|
||||||
|
|
||||||
type Props = StackNavigationConfig & {
|
type Props = StackNavigationConfig & {
|
||||||
state: StackNavigationState;
|
state: StackNavigationState;
|
||||||
navigation: NavigationHelpers<ParamListBase>;
|
navigation: StackNavigationHelpers;
|
||||||
descriptors: StackDescriptorMap;
|
descriptors: StackDescriptorMap;
|
||||||
onPageChangeStart?: () => void;
|
onPageChangeStart?: () => void;
|
||||||
onPageChangeConfirm?: () => void;
|
onPageChangeConfirm?: () => void;
|
||||||
|
|||||||
@@ -2,7 +2,10 @@
|
|||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"baseUrl": ".",
|
"baseUrl": ".",
|
||||||
"paths": {
|
"paths": {
|
||||||
"@react-navigation/*": ["./packages/*/src", "./packages/*/lib/typescript"],
|
"@react-navigation/*": [
|
||||||
|
"./packages/*/src",
|
||||||
|
"./packages/*/lib/typescript"
|
||||||
|
],
|
||||||
"use-subscription": ["./typings/use-subscription.d"]
|
"use-subscription": ["./typings/use-subscription.d"]
|
||||||
},
|
},
|
||||||
"allowUnreachableCode": false,
|
"allowUnreachableCode": false,
|
||||||
|
|||||||
Reference in New Issue
Block a user