React router guard (#41333)

* Add types react-router-guard package

* Add test for react-router-guard package

* Export for Loadable react-loadable

* Remove file react-router-guard-test.ts
This commit is contained in:
TSL 2020-01-03 01:07:54 +07:00 committed by Ryan Cavanaugh
parent bbeec088c3
commit c794e7209b
8 changed files with 142 additions and 0 deletions

47
types/react-router-guard/index.d.ts vendored Normal file
View File

@ -0,0 +1,47 @@
// Type definitions for react-router-guard 2.3
// Project: https://github.com/laptransang/react-router-guard#readme
// Definitions by: TSL <https://github.com/laptransang>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
import * as React from 'react';
import * as H from 'history';
export const history: H.History;
export {
Loadable
} from 'react-loadable';
export {
BrowserRouter,
Link,
NavLink,
Route,
Router,
Switch,
} from 'react-router-dom';
export {
Redirect,
} from 'react-router';
export function lazy<T extends React.ComponentType<any>>(
factory: () => Promise<{ default: T }>
): React.LazyExoticComponent<T>;
export interface RouterGuardConfigProps {
path: string;
component: React.LazyExoticComponent<any>;
canActivate?: Array<() => Promise<any>>;
redirect?: string;
exact?: boolean;
routes?: RouterGuardConfigProps[];
}
export interface RouterGuardProps {
config: RouterGuardConfigProps[];
history?: H.History;
loading?: boolean | React.ReactElement;
}
export class RouterGuard<T> extends React.Component<RouterGuardProps, any> {}

View File

@ -0,0 +1,12 @@
import * as React from 'react';
import { history } from 'react-router-guard';
const History = () => {
React.useEffect(() => {
history.push('/test');
}, []);
return null;
};
export default History;

View File

@ -0,0 +1,14 @@
import * as React from 'react';
interface Props {
children: any;
}
const Layout = ({ children }: Props) => (
<>
<h1>Layout Test</h1>
{children}
</>
);
export default Layout;

View File

@ -0,0 +1,7 @@
import * as React from 'react';
const Page = () => (
<><h1>Page Test</h1></>
);
export default Page;

View File

@ -0,0 +1,28 @@
import * as React from 'react';
import { RouterGuard, lazy, RouterGuardConfigProps } from 'react-router-guard';
import checkAuth from './checkAuth';
const config: RouterGuardConfigProps[] = [
{
path: '/',
component: lazy(() => import('./Layout')),
canActivate: [checkAuth],
routes: [
{
path: '/test',
component: lazy(() => import('./Page')),
},
],
},
];
const RouterGuardTest = () => (
<>
<RouterGuard
config={config}
/>
</>
);
export default RouterGuardTest;

View File

@ -0,0 +1,5 @@
export default function checkAuth(): Promise<any> {
return new Promise((resolve, reject) => {
resolve();
});
}

View File

@ -0,0 +1,28 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"jsx": "react",
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"test/RouterGuard.tsx",
"test/Page.tsx",
"test/Layout.tsx",
"test/History.tsx"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }