mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-03-06 22:39:41 +08:00
Query string parsing capabilities for deep linking (#510)
* adds query string parsing capabilities to URI handling * improves query params test case * adds test with empty query value
This commit is contained in:
committed by
Eric Vicenti
parent
5006076352
commit
d211492a4c
@@ -251,7 +251,9 @@ export default (
|
||||
});
|
||||
}
|
||||
|
||||
// Attempt to match `pathToResolve` with a route in this router's
|
||||
const [pathNameToResolve, queryString] = pathToResolve.split('?');
|
||||
|
||||
// Attempt to match `pathNameToResolve` with a route in this router's
|
||||
// routeConfigs
|
||||
let matchedRouteName;
|
||||
let pathMatch;
|
||||
@@ -260,7 +262,7 @@ export default (
|
||||
for (const routeName in paths) {
|
||||
/* $FlowFixMe */
|
||||
const { re, keys } = paths[routeName];
|
||||
pathMatch = re.exec(pathToResolve);
|
||||
pathMatch = re.exec(pathNameToResolve);
|
||||
if (pathMatch && pathMatch.length) {
|
||||
pathMatchKeys = keys;
|
||||
matchedRouteName = routeName;
|
||||
@@ -285,6 +287,18 @@ export default (
|
||||
);
|
||||
}
|
||||
|
||||
// reduce the items of the query string. any query params may
|
||||
// be overridden by path params
|
||||
const queryParams = (queryString || '').split('&').reduce((result: *, item: string) => {
|
||||
if (item !== '') {
|
||||
const nextResult = result || {};
|
||||
const [key, value] = item.split('=');
|
||||
nextResult[key] = value;
|
||||
return nextResult;
|
||||
}
|
||||
return result;
|
||||
}, null);
|
||||
|
||||
// reduce the matched pieces of the path into the params
|
||||
// of the route. `params` is null if there are no params.
|
||||
/* $FlowFixMe */
|
||||
@@ -297,7 +311,8 @@ export default (
|
||||
const paramName = key.name;
|
||||
nextResult[paramName] = matchResult;
|
||||
return nextResult;
|
||||
}, null);
|
||||
}, queryParams);
|
||||
|
||||
|
||||
return NavigationActions.navigate({
|
||||
routeName: matchedRouteName,
|
||||
|
||||
@@ -185,6 +185,29 @@ describe('StackRouter', () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('Parses paths with a query', () => {
|
||||
expect(TestStackRouter.getActionForPathAndParams('people/foo?code=test&foo=bar')).toEqual({
|
||||
type: NavigationActions.NAVIGATE,
|
||||
routeName: 'person',
|
||||
params: {
|
||||
id: 'foo',
|
||||
code: 'test',
|
||||
foo: 'bar',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test('Parses paths with an empty query value', () => {
|
||||
expect(TestStackRouter.getActionForPathAndParams('people/foo?code=&foo=bar')).toEqual({
|
||||
type: NavigationActions.NAVIGATE,
|
||||
routeName: 'person',
|
||||
params: {
|
||||
id: 'foo',
|
||||
code: '',
|
||||
foo: 'bar',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test('Correctly parses a path without arguments into an action chain', () => {
|
||||
const uri = 'auth/login';
|
||||
|
||||
Reference in New Issue
Block a user