Expand NamedLink to handle location descriptors

This commit is contained in:
Vesa Luusua 2017-01-25 13:42:49 +02:00
parent d7f9cba179
commit 3ae765b75a

View file

@ -1,20 +1,31 @@
/**
* This component wraps React-Router's Link by providing name-based routing.
* This is also special component that gets routes from context.
* (Helps to narrow down the scope of possible format changes to routes.)
*/
import React, { PropTypes } from 'react';
import { Link } from 'react-router';
import { pathByRouteName } from '../../routesConfiguration';
const NamedLink = (props, context) => {
const { name, params, ...rest } = props;
const path = pathByRouteName(name, context.routes, params);
return <Link to={path} {...rest} />;
const { name, params, query, hash, state, ...rest } = props;
const pathname = pathByRouteName(name, context.routes, params);
const locationDescriptor = { pathname, query, hash, state };
return <Link to={locationDescriptor} {...rest} />;
};
const { array, object, string } = PropTypes;
NamedLink.contextTypes = { routes: array };
NamedLink.defaultProps = { params: {} };
NamedLink.defaultProps = { hash: '', params: {}, query: {}, state: {} };
NamedLink.propTypes = { name: string.isRequired, params: object };
NamedLink.propTypes = {
hash: string,
name: string.isRequired,
params: object,
query: object,
state: object,
};
export default NamedLink;