Types for rrc 0.10 (#16996)

* rrc types files added.
rrc types definitions.

* TS config updated.
Types definitions updated.
Tests added.

* withScroll return type adjusted to `react-router` RouteProps.component type.

* Test values updated.

* rrc types file lint errors fixed.

* rrc tests file updated.
This commit is contained in:
Deividas Bakanas
2017-06-06 17:22:10 -07:00
committed by Mohamed Hegazy
parent 99d808a93f
commit 879a6039ac
4 changed files with 222 additions and 0 deletions
+75
View File
@@ -0,0 +1,75 @@
// Type definitions for rrc 0.10
// Project: https://github.com/pshrmn/rrc#readme
// Definitions by: Deividas Bakanas <https://github.com/DeividasBakanas>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
import * as React from "react";
import * as H from "history";
import { RouteProps, RouteComponentProps, match as MatchObject } from "react-router-dom";
export interface ScrollIntoViewProps {
alignToTop?: boolean;
id: string;
}
export class ScrollIntoView extends React.Component<ScrollIntoViewProps, void> { }
export type PropIdCallback = () => string;
export interface WithScrollOptions {
propId?: PropIdCallback;
alignToTop?: boolean;
}
export type ComponentConstructor<Props> = React.ComponentClass<Props> | React.SFC<Props>;
export function withScroll(component: ComponentConstructor<RouteComponentProps<any> | undefined>, options?: WithScrollOptions)
: ComponentConstructor<RouteComponentProps<any> | undefined>;
export type RouteConfiguration = RouteProps & { inject?: { [key: string]: any } };
export interface SwitchProps {
routes: RouteConfiguration[];
location: H.LocationDescriptorObject & { pathname: H.Pathname };
}
export class ConfigSwitch extends React.Component<SwitchProps, void> { }
export type OnUpdateCall = (location: H.Location) => void;
export interface OnUpdateProps {
call: OnUpdateCall;
immediate?: boolean;
}
export class OnUpdate extends React.Component<OnUpdateProps, {}> { }
export type IsActiveCallback = () => boolean;
export interface WhenActiveOptions {
exact?: boolean;
strict?: boolean;
pathProp?: string;
className?: string;
style?: React.CSSProperties;
isActive?: IsActiveCallback;
}
export type WhenActiveReturnType<Props> = (component: ComponentConstructor<Props>) => ComponentConstructor<Props>;
export function whenActive<Props>(options?: WhenActiveOptions): WhenActiveReturnType<Props>;
export interface StatusProps {
code: string;
}
export class Status extends React.Component<StatusProps, void> { }
export type GetKeyFunction<Params> = (match: MatchObject<Params>, route: RouteConfiguration, location: H.Location) => string;
export interface WrapSwitchProps<Params> extends SwitchProps {
getKey?: GetKeyFunction<Params>;
}
export function wrapSwitch<WrapperProps, Params>(Wrapper: ComponentConstructor<WrapperProps>):
ComponentConstructor<WrapSwitchProps<Params> & WrapperProps>;
+122
View File
@@ -0,0 +1,122 @@
import * as React from "react";
import { Switch, Route, RouteComponentProps, BrowserRouter, Link } from "react-router-dom";
import {
ScrollIntoView,
ConfigSwitch,
withScroll,
OnUpdate,
whenActive,
Status,
wrapSwitch,
RouteConfiguration,
GetKeyFunction,
OnUpdateCall
} from "rrc";
class RouteOne extends React.Component<RouteComponentProps<{}>, {}> {
render() {
return <div>
<ConfigSwitch location={this.props.location} routes={[
{
path: "/one/one",
render: () => <ScrollIntoView alignToTop id="id">
<div>Main view</div>
</ScrollIntoView>
},
{ path: "/one/two", render: () => <div>One two route</div> }
]} />
</div>;
}
}
class RouteTwo extends React.Component<RouteComponentProps<{}>, {}> {
private onUpdate: OnUpdateCall = (location) => { console.log("update"); };
render() {
return <div>
Route 2
<Link to={{ pathname: "/one" }}>Go to Route 1</Link>
<OnUpdate call={this.onUpdate} />
</div >;
}
}
interface LayoutProps {
title: string;
subtitle?: string;
}
class Layout extends React.Component<LayoutProps, {}> {
render() {
return <div>
<div>{`Layout ${this.props.title}`}</div>
<div>
<span>Content</span>
{this.props.children}
</div>
</div>;
}
}
const WrappedLayout = wrapSwitch(Layout);
interface Params {
page: number;
}
class RouteFour extends React.Component<RouteComponentProps<{}>, {}> {
private routes: RouteConfiguration[] = [
{ path: "/four/something/:page", component: RouteTwo }
];
private getKey: GetKeyFunction<Params> = (match, route, location) => {
return "my-key-" + match.url;
}
render() {
return <div>
<div>
Route four
</div>
<div>
<WrappedLayout
getKey={this.getKey}
routes={this.routes}
location={this.props.location}
title="wrapped layout title"
/>
</div>
</div>;
}
}
interface MyContainerProps {
className?: string;
color: number;
}
class MyContainer extends React.Component<MyContainerProps, {}> {
render() {
return <div className={this.props.className}>
{this.props.children}
</div>;
}
}
const ExtendedContainer = whenActive<MyContainerProps>({ className: "active" })(MyContainer);
const RouteTwoWithScroll = withScroll(RouteTwo);
export const Routes =
<BrowserRouter>
<div>
<div>My page</div>
<Switch >
<Route path="/one" component={RouteOne} />
<Route path="/two" component={RouteTwoWithScroll} />
<Route path="/four" component={RouteFour} />
<Route strict path="/" render={() => <ExtendedContainer className="extended-container" color={3}>Route 3</ExtendedContainer>} />
</Switch>
<Status code="200" />
</div>
</BrowserRouter>;
+24
View File
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"jsx": "react",
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"rrc-tests.tsx"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }