diff --git a/types/react-loadable/babel.d.ts b/types/react-loadable/babel.d.ts new file mode 100644 index 0000000000..a9f18c2e00 --- /dev/null +++ b/types/react-loadable/babel.d.ts @@ -0,0 +1,3 @@ +import * as babel from 'babel-core'; + +export default function(context: Pick): { visitor: any }; diff --git a/types/react-loadable/index.d.ts b/types/react-loadable/index.d.ts new file mode 100644 index 0000000000..bbe7d1aac4 --- /dev/null +++ b/types/react-loadable/index.d.ts @@ -0,0 +1,117 @@ +// Type definitions for react-loadable 3.3 +// Project: https://github.com/thejameskyle/react-loadable#readme +// Definitions by: Diogo Franco +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +import * as React from 'react'; + +export type LoadedComponent = React.ComponentClass | React.SFC; + +export interface LoadingComponentProps { + isLoading: boolean; + pastDelay: boolean; + error: any; +} + +export type Options = OptionsWithoutResolve | OptionsWithResolve; + +export interface CommonOptions { + /** + * React component displayed after delay until loader() succeeds. Also responsible for displaying errors. + * + * If you don't want to render anything you can pass a function that returns null + * (this is considered a valid React component). + */ + // NOTE: () => null is only needed until React.SFC supports components returning null + LoadingComponent: React.ComponentClass | React.SFC | (() => null); + /** + * Defaults to 200, in milliseconds + * + * Only show the LoadingComponent if the loader() has taken this long to succeed or error. + */ + delay?: number; + /** + * When rendering server-side, require() this path to load the component instead, this way it happens + * synchronously. If you are rendering server-side you should use this option. + * + * If you are using Babel, you might want to use the Babel plugin to add this option automatically. + */ + serverSideRequirePath?: string; + /** + * In order for Loadable to require() a component synchronously (when possible) instead of waiting for + * the promise returned by import() to resolve. If you are using Webpack you should use this option. + * + * ```ts + * Loadable({ + * // ... + * webpackRequireWeakId: () => require.resolveWeak('./MyComponent') + * }); + * ``` + * + * If you are using Babel, you might want to use the Babel plugin to add this option automatically. + */ + webpackRequireWeakId?(): number|string; +} + +export interface OptionsWithoutResolve extends CommonOptions { + /** + * Function returning promise returning a React component displayed on success. + * + * Resulting React component receives all the props passed to the generated component. + */ + loader(): Promise | { default: LoadedComponent }>; +} + +export interface OptionsWithResolve extends CommonOptions { + /** + * Function returning promise returning a React component displayed on success. + * + * Resulting React component receives all the props passed to the generated component. + */ + loader(): Promise; + /** + * If the component that you want to load is not the default exported from a module you can use this + * function to resolve it. + * + * ```ts + * Loadable({ + * // ... + * resolveModule: module => module.MyComponent + * }); + * ``` + */ + resolveModule(obj: T): LoadedComponent; +} + +export interface LoadableComponent { + /** + * The generated component has a static method preload() for calling the loader function ahead of time. + * This is useful for scenarios where you think the user might do something next and want to load the + * next component eagerly. + * + * Note: preload() intentionally does not return a promise. You should not be depending on the timing of + * preload(). It's meant as a performance optimization, not for creating UI logic. + */ + preload(): void; +} + +export default function Loadable(options: Options): LoadedComponent & LoadableComponent; + +/** + * In case you are rendering server-side and want to find out after a render cycle which + * serverSideRequirePath's and webpackRequireWeakId's were actually rendered, you can use + * flushServerSideRequirePaths or flushWebpackRequireWeakIds to get an array of them. + * + * Note: These are flushed individually, one does not affect the other. + */ +export function flushServerSideRequirePaths(): string[]; + +/** + * In case you are rendering server-side and want to find out after a render cycle which + * serverSideRequirePath's and webpackRequireWeakId's were actually rendered, you can use + * flushServerSideRequirePaths or flushWebpackRequireWeakIds to get an array of them. + * + * Note: These are flushed individually, one does not affect the other. + */ +export function flushWebpackRequireWeakIds(): string[]|number[]; diff --git a/types/react-loadable/react-loadable-tests.tsx b/types/react-loadable/react-loadable-tests.tsx new file mode 100644 index 0000000000..3455a7b23a --- /dev/null +++ b/types/react-loadable/react-loadable-tests.tsx @@ -0,0 +1,51 @@ +import * as React from 'react'; +import Loadable, { LoadingComponentProps } from 'react-loadable'; + +class LoadingComponent extends React.Component { + render() { + return
{this.props.isLoading}
; + } +} + +interface ComponentProps { + text: string; +} + +const Component: React.SFC = ({ text }) =>
{text}
; + +const Loadable100 = Loadable({ + // a module shape with 'export = Component' / 'module.exports = Component' + loader: () => Promise.resolve(Component), + LoadingComponent, + delay: 100 +}); + +const Loadable200 = Loadable({ + // a module shape with 'export default Component' + loader: () => Promise.resolve({ default: Component }), + LoadingComponent, + delay: 200 +}); + +const Loadable300 = Loadable({ + // a module shape with 'export { Component }' + loader: () => Promise.resolve({ Component }), + LoadingComponent, + delay: 300, + resolveModule: shape => shape.Component +}); + +const Loadable400 = Loadable({ + // a module shape with both 'export default Component' and 'export { Component }' + loader: () => Promise.resolve({ default: Component, Component }), + LoadingComponent: () => null, + delay: 300, + resolveModule: shape => shape.Component +}); + +const used100 = ; +const used200 = ; +const used300 = ; +const used400 = ; + +Loadable100.preload(); diff --git a/types/react-loadable/tsconfig.json b/types/react-loadable/tsconfig.json new file mode 100644 index 0000000000..bf26cfdbc5 --- /dev/null +++ b/types/react-loadable/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "jsx": "react", + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "babel.d.ts", + "react-loadable-tests.tsx" + ] +} diff --git a/types/react-loadable/tslint.json b/types/react-loadable/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/react-loadable/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }