Files
react-native-paper/docs/templates/Link.js
Satyajit Sahoo 01850e1bb7 docs: improvements to docs generation
- Generate a single bundle
- Implement routing which works with/without JS
- Enable HMR for function components as well as documentation
2016-11-23 16:56:53 +05:30

49 lines
1.0 KiB
JavaScript

/* @flow */
import React, { Component } from 'react';
import { history } from './Router';
type Props = {
to: string;
}
export default class Router extends Component<void, Props, void> {
props: Props;
_handleClick = event => {
event.preventDefault();
const path = `${this.props.to}.html`;
try {
if (history) {
history.push(path);
} else {
throw new Error('');
}
} catch (e) {
if (!e.message.startsWith("Failed to execute 'pushState' on 'History'")) {
// Google Chrome throws for file URLs
throw e;
}
const { pathname } = window.location;
if (pathname.endsWith('/')) {
window.location.pathname = `${pathname}/${path}`;
} else {
const parts = pathname.split('/');
parts.pop();
window.location.pathname = `${parts.join('/')}/${path}`;
}
}
}
render() {
const { to, ...rest } = this.props;
return (
<a
{...rest}
href={`${to}.html`}
onClick={this._handleClick}
/>
);
}
}