mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-04-29 12:55:21 +08:00
refactor: add types to action creators/invariant (#65)
* refactor: add types to `NavigationActions` * refactor: add types to `invariant` * refactor: add types to `getNavigationActionCreators` * Update invariant.ts
This commit is contained in:
@@ -1,46 +0,0 @@
|
||||
// Action constants
|
||||
export const BACK = 'Navigation/BACK';
|
||||
export const INIT = 'Navigation/INIT';
|
||||
export const NAVIGATE = 'Navigation/NAVIGATE';
|
||||
export const SET_PARAMS = 'Navigation/SET_PARAMS';
|
||||
|
||||
// Action creators
|
||||
export const back = (payload = {}) => ({
|
||||
type: BACK,
|
||||
key: payload.key,
|
||||
immediate: payload.immediate,
|
||||
});
|
||||
|
||||
export const init = (payload = {}) => {
|
||||
const action = {
|
||||
type: INIT,
|
||||
};
|
||||
if (payload.params) {
|
||||
action.params = payload.params;
|
||||
}
|
||||
return action;
|
||||
};
|
||||
|
||||
export const navigate = payload => {
|
||||
const action = {
|
||||
type: NAVIGATE,
|
||||
routeName: payload.routeName,
|
||||
};
|
||||
if (payload.params) {
|
||||
action.params = payload.params;
|
||||
}
|
||||
if (payload.action) {
|
||||
action.action = payload.action;
|
||||
}
|
||||
if (payload.key) {
|
||||
action.key = payload.key;
|
||||
}
|
||||
return action;
|
||||
};
|
||||
|
||||
export const setParams = payload => ({
|
||||
type: SET_PARAMS,
|
||||
key: payload.key,
|
||||
params: payload.params,
|
||||
preserveFocus: true,
|
||||
});
|
||||
102
packages/core/src/NavigationActions.ts
Normal file
102
packages/core/src/NavigationActions.ts
Normal file
@@ -0,0 +1,102 @@
|
||||
export interface NavigationParams {
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
export interface NavigationNavigateActionPayload {
|
||||
routeName: string;
|
||||
params?: NavigationParams;
|
||||
|
||||
// The action to run inside the sub-router
|
||||
action?: NavigationNavigateAction;
|
||||
|
||||
key?: string;
|
||||
}
|
||||
|
||||
export interface NavigationNavigateAction
|
||||
extends NavigationNavigateActionPayload {
|
||||
type: 'Navigation/NAVIGATE';
|
||||
}
|
||||
|
||||
export interface NavigationBackActionPayload {
|
||||
key?: string | null;
|
||||
immediate?: boolean;
|
||||
}
|
||||
|
||||
export interface NavigationBackAction extends NavigationBackActionPayload {
|
||||
type: 'Navigation/BACK';
|
||||
}
|
||||
|
||||
export interface NavigationInitActionPayload {
|
||||
params?: NavigationParams;
|
||||
}
|
||||
|
||||
export interface NavigationInitAction extends NavigationInitActionPayload {
|
||||
type: 'Navigation/INIT';
|
||||
}
|
||||
|
||||
export interface NavigationSetParamsActionPayload {
|
||||
// The key of the route where the params should be set
|
||||
key: string;
|
||||
|
||||
// The new params to merge into the existing route params
|
||||
params?: NavigationParams;
|
||||
}
|
||||
|
||||
export interface NavigationSetParamsAction
|
||||
extends NavigationSetParamsActionPayload {
|
||||
type: 'Navigation/SET_PARAMS';
|
||||
preserveFocus: true;
|
||||
}
|
||||
|
||||
// Action constants
|
||||
export const BACK = 'Navigation/BACK';
|
||||
export const INIT = 'Navigation/INIT';
|
||||
export const NAVIGATE = 'Navigation/NAVIGATE';
|
||||
export const SET_PARAMS = 'Navigation/SET_PARAMS';
|
||||
|
||||
// Action creators
|
||||
export const back = (
|
||||
payload: NavigationBackActionPayload = {}
|
||||
): NavigationBackAction => ({
|
||||
type: BACK,
|
||||
key: payload.key,
|
||||
immediate: payload.immediate,
|
||||
});
|
||||
|
||||
export const init = (payload: NavigationInitActionPayload = {}) => {
|
||||
const action: NavigationInitAction = {
|
||||
type: INIT,
|
||||
};
|
||||
if (payload.params) {
|
||||
action.params = payload.params;
|
||||
}
|
||||
return action;
|
||||
};
|
||||
|
||||
export const navigate = (
|
||||
payload: NavigationNavigateActionPayload
|
||||
): NavigationNavigateAction => {
|
||||
const action: NavigationNavigateAction = {
|
||||
type: NAVIGATE,
|
||||
routeName: payload.routeName,
|
||||
};
|
||||
if (payload.params) {
|
||||
action.params = payload.params;
|
||||
}
|
||||
if (payload.action) {
|
||||
action.action = payload.action;
|
||||
}
|
||||
if (payload.key) {
|
||||
action.key = payload.key;
|
||||
}
|
||||
return action;
|
||||
};
|
||||
|
||||
export const setParams = (
|
||||
payload: NavigationSetParamsActionPayload
|
||||
): NavigationSetParamsAction => ({
|
||||
type: SET_PARAMS,
|
||||
key: payload.key,
|
||||
params: payload.params,
|
||||
preserveFocus: true,
|
||||
});
|
||||
@@ -1,9 +1,14 @@
|
||||
import * as NavigationActions from '../NavigationActions';
|
||||
import invariant from '../utils/invariant';
|
||||
|
||||
const getNavigationActionCreators = route => {
|
||||
interface NavigationParams {
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
// TODO: Type `route`
|
||||
const getNavigationActionCreators = (route: any) => {
|
||||
return {
|
||||
goBack: key => {
|
||||
goBack: (key?: string | null) => {
|
||||
let actualizedKey = key;
|
||||
if (key === undefined && route.key) {
|
||||
invariant(typeof route.key === 'string', 'key should be a string');
|
||||
@@ -11,7 +16,11 @@ const getNavigationActionCreators = route => {
|
||||
}
|
||||
return NavigationActions.back({ key: actualizedKey });
|
||||
},
|
||||
navigate: (navigateTo, params, action) => {
|
||||
navigate: (
|
||||
navigateTo: string | NavigationActions.NavigationNavigateActionPayload,
|
||||
params?: NavigationParams,
|
||||
action?: NavigationActions.NavigationNavigateAction
|
||||
) => {
|
||||
if (typeof navigateTo === 'string') {
|
||||
return NavigationActions.navigate({
|
||||
routeName: navigateTo,
|
||||
@@ -33,7 +42,7 @@ const getNavigationActionCreators = route => {
|
||||
);
|
||||
return NavigationActions.navigate(navigateTo);
|
||||
},
|
||||
setParams: params => {
|
||||
setParams: (params?: NavigationParams) => {
|
||||
invariant(
|
||||
route.key && typeof route.key === 'string',
|
||||
'setParams cannot be called by root navigator'
|
||||
@@ -1,5 +1,3 @@
|
||||
/* eslint-disable import/no-commonjs */
|
||||
|
||||
/**
|
||||
* Use invariant() to assert state which your program assumes to be true.
|
||||
*
|
||||
@@ -11,33 +9,28 @@
|
||||
* will remain to ensure logic does not differ in production.
|
||||
*/
|
||||
|
||||
var validateFormat = function() {};
|
||||
let validateFormat: (format?: string) => void = function() {};
|
||||
|
||||
if (process.env !== 'production') {
|
||||
validateFormat = function(format) {
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
validateFormat = function(format?: string) {
|
||||
if (format === undefined) {
|
||||
throw new Error('invariant requires an error message argument');
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function invariant(condition, format, a, b, c, d, e, f) {
|
||||
function invariant(condition: boolean, format?: string, ...args: any[]) {
|
||||
validateFormat(format);
|
||||
|
||||
if (!condition) {
|
||||
var error;
|
||||
let error: Error & { framesToPop?: number };
|
||||
if (format === undefined) {
|
||||
error = new Error(
|
||||
'Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.'
|
||||
);
|
||||
} else {
|
||||
var args = [a, b, c, d, e, f];
|
||||
var argIndex = 0;
|
||||
error = new Error(
|
||||
format.replace(/%s/g, function() {
|
||||
return args[argIndex++];
|
||||
})
|
||||
);
|
||||
let argIndex = 0;
|
||||
error = new Error(format.replace(/%s/g, () => args[argIndex++]));
|
||||
error.name = 'Invariant Violation';
|
||||
}
|
||||
|
||||
@@ -46,4 +39,4 @@ function invariant(condition, format, a, b, c, d, e, f) {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = invariant;
|
||||
export default invariant;
|
||||
Reference in New Issue
Block a user