Guest User

Untitled

a guest
Jul 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. export * from "./Test";
  2. export * from "./TestProps";
  3. export * from "./TestState";
  4.  
  5. import * as PropTypes from "prop-types";
  6.  
  7. export interface TestProps {
  8. header: string,
  9. count: number;
  10. onIncrement: () => any;
  11. }
  12.  
  13. export const TestPropTypes = {
  14. header: PropTypes.string
  15. };
  16.  
  17. export interface TestState {
  18. name: string
  19. }
  20.  
  21. import React, { Component } from "react";
  22. import {TestProps, TestPropTypes} from "./TestProps";
  23. import {TestState} from "./TestState";
  24. import { HOC } from "../HOC/";
  25.  
  26. @HOC
  27. export class Test extends Component<TestProps, TestState> {
  28. public static propTypes = TestPropTypes;
  29. public render(): JSX.Element {
  30. const { header } = this.props;
  31. return <div>{header}</div>
  32. }
  33. }
  34.  
  35. export * from "./HOC";
  36.  
  37. import * as React from "react";
  38. import { Subtract } from "utility-types";
  39.  
  40. interface HOCProps {
  41. count: number;
  42. onIncrement: () => any;
  43. }
  44.  
  45. export const HOC = <WrappedProps extends HOCProps>(
  46. WrappedComponent: React.ComponentType<WrappedProps>
  47. ) => {
  48. type HocProps = Subtract<WrappedProps, HOCProps> & {
  49. initialCount?: number;
  50. };
  51. interface HocState {
  52. readonly count: number;
  53. };
  54.  
  55. return class WithState extends React.Component<HocProps, HocState> {
  56. public static displayName = `withState(${WrappedComponent.name})`;
  57. public static readonly WrappedComponent = WrappedComponent;
  58. public readonly state: HocState = {
  59. count: Number(this.props.initialCount) || 0,
  60. };
  61.  
  62. public handleIncrement = () => {
  63. this.setState({ count: this.state.count + 1 });
  64. };
  65.  
  66. public render() {
  67. const { ...restProps } = this.props as {};
  68. const { count } = this.state;
  69.  
  70. return (
  71. <WrappedComponent
  72. {...restProps}
  73. count={count}
  74. onIncrement={this.handleIncrement}
  75. />
  76. );
  77. }
  78. };
  79. };
Add Comment
Please, Sign In to add comment