diff --git a/packages/client/src/components/IWithRouterProps.ts b/packages/client/src/components/IWithRouterProps.ts new file mode 100644 index 0000000..8d84b0d --- /dev/null +++ b/packages/client/src/components/IWithRouterProps.ts @@ -0,0 +1,8 @@ +import {match as Match} from 'react-router' +import {History, Location} from 'history' + +export interface IWithRouterProps { + history: History + location: Location + match: Match +} diff --git a/packages/client/src/components/Link.test.tsx b/packages/client/src/components/Link.test.tsx new file mode 100644 index 0000000..aa1bacc --- /dev/null +++ b/packages/client/src/components/Link.test.tsx @@ -0,0 +1,49 @@ +import React from 'react' +import {MemoryRouter} from 'react-router' +import {Route} from 'react-router-dom' +import {Link} from './Link' +import {TestUtils} from '../test-utils' + +describe('Link', () => { + + const t = new TestUtils() + + function render( + to: string, + routePath = '/test', + routerEntry = '/test', + ) { + return t.render( + + +
+ Link Text +
} + /> +
, + ) + } + + it('should set href to value', () => { + const {node} = render('/my/link') + const a = node.querySelector('a') as HTMLElement + expect(a.tagName).toEqual('A') + expect(a.getAttribute('href')).toEqual('/my/link') + expect(a.textContent).toEqual('Link Text') + }) + + it('should format url to matched props', () => { + const {node} = render( + '/my/:oneId/link/:twoId', + '/one/:oneId/two/:twoId', + '/one/1/two/2', + ) + const a = node.querySelector('a') as HTMLElement + expect(a.tagName).toEqual('A') + expect(a.getAttribute('href')).toEqual('/my/1/link/2') + expect(a.textContent).toEqual('Link Text') + }) + +}) diff --git a/packages/client/src/components/Link.tsx b/packages/client/src/components/Link.tsx new file mode 100644 index 0000000..bc721b1 --- /dev/null +++ b/packages/client/src/components/Link.tsx @@ -0,0 +1,35 @@ +import React from 'react' +import {History, Location} from 'history' +import {IWithRouterProps} from './IWithRouterProps' +import {Link as RouterLink, LinkProps} from 'react-router-dom' +import {URLFormatter} from '@rondo/common' +import {withRouter} from 'react-router' + +interface ILinkProps +extends IWithRouterProps> { + readonly to: string +} + +class ContextLink extends React.PureComponent { + protected readonly urlFormatter = new URLFormatter() + + render() { + const { + history, + location, + match, + to, + children, + } = this.props + + const href = this.urlFormatter.format(to, match.params) + + return ( + + {children} + + ) + } +} + +export const Link = withRouter(ContextLink) diff --git a/packages/client/src/components/index.ts b/packages/client/src/components/index.ts index 0444255..96006b2 100644 --- a/packages/client/src/components/index.ts +++ b/packages/client/src/components/index.ts @@ -1,5 +1,7 @@ -export * from './Button' // export * from './Component' +export * from './Button' +export * from './IWithRouterProps' export * from './Input' +export * from './Link' export * from './Modal' export * from './Redirect'