diff --git a/types/react-lifecycle-component/index.d.ts b/types/react-lifecycle-component/index.d.ts new file mode 100644 index 0000000000..315d04f7fb --- /dev/null +++ b/types/react-lifecycle-component/index.d.ts @@ -0,0 +1,24 @@ +// Type definitions for react-lifecycle-component 2.0 +// Project: https://github.com/JamieDixon/react-lifecycle-component +// Definitions by: Alexander Fisher +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.8 + +import { ComponentLifecycle, Component, ComponentClass } from 'react'; +import { Connect } from 'react-redux'; + +export interface LifecycleStateProps

{ + component: ComponentClass; +} + +export interface LifecycleDispatchProps

extends ComponentLifecycle {} + +export interface Props

extends LifecycleStateProps, LifecycleDispatchProps {} + +export class LifecycleComponent extends Component, any> {} + +export function applyLifecycle

( + component: ComponentClass, +): ComponentClass

>; + +export const connectWithLifecycle: Connect; diff --git a/types/react-lifecycle-component/package.json b/types/react-lifecycle-component/package.json new file mode 100644 index 0000000000..bb0e2b57d2 --- /dev/null +++ b/types/react-lifecycle-component/package.json @@ -0,0 +1,6 @@ +{ + "private": true, + "dependencies": { + "redux": "^4.0.0" + } +} \ No newline at end of file diff --git a/types/react-lifecycle-component/react-lifecycle-component-tests.tsx b/types/react-lifecycle-component/react-lifecycle-component-tests.tsx new file mode 100644 index 0000000000..6c4a7407d4 --- /dev/null +++ b/types/react-lifecycle-component/react-lifecycle-component-tests.tsx @@ -0,0 +1,98 @@ +import * as React from 'react'; +import { Dispatch } from 'redux'; +import { connect } from 'react-redux'; +import { + connectWithLifecycle, + LifecycleDispatchProps, + applyLifecycle, + LifecycleStateProps, + LifecycleComponent, +} from 'react-lifecycle-component'; + +interface State { + stateFoo: number; +} + +interface Props extends StateProps, DispatchProps { +} + +interface StateProps { + propsFoo: number; +} + +interface DispatchProps { + bar(): void; +} + +class ComponentFoo extends React.Component { + constructor(props: Props) { + super(props); + this.state = { + stateFoo: 0, + }; + } + render() { + return ( +

+
Props Foo: {this.props.propsFoo}
+
State Foo: {this.state.stateFoo}
+ +
+ ); + } +} + +type MapStateProps = StateProps & LifecycleStateProps; + +function mapStateToProps(): MapStateProps { + return { + component: ComponentFoo, + propsFoo: 8675309, + }; +} + +type MapDispatchProps = DispatchProps & LifecycleDispatchProps; + +function mapDispatchToProps(dispatch: Dispatch): MapDispatchProps { + return { + bar() { + dispatch({ type: 'Bar'}); + }, + componentWillMount() { + dispatch({ type: 'ComponentWillMount'}); + }, + componentDidMount() { + dispatch({ type: 'ComponentDidMount'}); + }, + componentWillUpdate(nextProps, nextState, nextContext) { + const fooIsEqual: boolean = nextProps.propsFoo === nextState.stateFoo; + const hasNextContext: boolean = !!nextContext; + dispatch({ type: 'ComponentWillUpdate'}); + }, + componentDidUpdate(nextProps, nextState, nextContext) { + const fooIsEqual: boolean = nextProps.propsFoo === nextState.stateFoo; + const hasNextContext: boolean = !!nextContext; + dispatch({ type: 'ComponentDidUpdate'}); + }, + componentWillReceiveProps(nextProps, nextContext) { + const fooIsGreaterThanZero: boolean = nextProps.propsFoo > 0; + const hasNextContext: boolean = !!nextContext; + dispatch({ type: 'ComponentWillReceiveProps'}); + }, + componentWillUnmount() { + dispatch({ type: 'ComponentWillUnmount'}); + }, + shouldComponentUpdate(nextProps, nextState, nextContext) { + const fooIsEqual: boolean = nextProps.propsFoo === nextState.stateFoo; + const hasNextContext: boolean = !!nextContext; + return !fooIsEqual; + }, + }; +} + +const connectWithLifecylceContainer = + connectWithLifecycle(mapStateToProps, mapDispatchToProps)(ComponentFoo); +const applyLifecycleContainer = + connect(mapStateToProps, mapDispatchToProps)(applyLifecycle(ComponentFoo)); +const lifecycleContainer = + connect(mapStateToProps, mapDispatchToProps)(LifecycleComponent); diff --git a/types/react-lifecycle-component/tsconfig.json b/types/react-lifecycle-component/tsconfig.json new file mode 100644 index 0000000000..d9aad16e53 --- /dev/null +++ b/types/react-lifecycle-component/tsconfig.json @@ -0,0 +1,25 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "jsx": "react", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "react-lifecycle-component-tests.tsx" + ] +} diff --git a/types/react-lifecycle-component/tslint.json b/types/react-lifecycle-component/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/react-lifecycle-component/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }