Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.

Commit a5a1666

Browse files
hocs sorted
1 parent c400e6f commit a5a1666

File tree

7 files changed

+67
-37
lines changed

7 files changed

+67
-37
lines changed
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
import React from 'react';
1+
import React, { ReactNode, StatelessComponent } from 'react';
22
import getDisplayName from './getDisplayName';
33

4-
const { Consumer, Provider } = React.createContext();
4+
const { Consumer, Provider } = React.createContext<string | undefined>(
5+
undefined,
6+
);
57

6-
const AuthorizationProvider = ({ authorization, children }) => (
8+
const AuthorizationProvider: StatelessComponent<{
9+
authorization: string | undefined;
10+
children: ReactNode;
11+
}> = ({ authorization, children }) => (
712
<Provider value={authorization}>{children}</Provider>
813
);
914

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1-
import React from 'react';
1+
import React, { ReactNode, StatelessComponent } from 'react';
2+
import { NavigationHandler } from '../types';
23
import getDisplayName from './getDisplayName';
3-
const { Consumer, Provider } = React.createContext();
44

5-
const NavigationProvider = ({ onNavigate, children }) => (
5+
const defaultNavigationHandler: NavigationHandler = (link, authorization) => {
6+
return;
7+
};
8+
9+
const { Consumer, Provider } = React.createContext<NavigationHandler>(
10+
defaultNavigationHandler,
11+
);
12+
13+
const NavigationProvider: StatelessComponent<{
14+
onNavigate: NavigationHandler;
15+
children: ReactNode;
16+
}> = ({ onNavigate, children }) => (
617
<Provider value={onNavigate}>{children}</Provider>
718
);
819

src/components/getDisplayName.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/components/getDisplayName.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { ComponentType } from 'react';
2+
3+
export default <T>(
4+
hocName: string,
5+
WrappedComponent: ComponentType<T>,
6+
): string =>
7+
`${hocName}(${WrappedComponent.displayName ||
8+
WrappedComponent.name ||
9+
'Component'})`;

src/components/mount.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/components/mount.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import React, { ComponentType, PureComponent } from 'react';
2+
import getDisplayName from './getDisplayName';
3+
4+
type DidMount<T> = (props: T) => void;
5+
type WillUnmount<T> = (props: T) => void;
6+
7+
const mount = <T extends object>(
8+
didMount: DidMount<T>,
9+
willUnmount?: WillUnmount<T>,
10+
) => (WrappedComponent: ComponentType<T>) => {
11+
class Mount extends PureComponent<T> {
12+
componentDidMount() {
13+
didMount(this.props);
14+
}
15+
16+
componentWillUnmount() {
17+
if (!willUnmount) {
18+
return;
19+
}
20+
willUnmount(this.props);
21+
}
22+
23+
render() {
24+
return React.createElement<T>(WrappedComponent, this.props);
25+
}
26+
}
27+
28+
return Mount;
29+
};
30+
31+
mount.displayName = 'Mount';
32+
33+
export default mount;

src/types/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ export interface EmbeddedResources {
2121
[rel: string]: HalResource | HalResource[];
2222
}
2323

24+
export type NavigationHandler = (link: HalLink, authorization?: string) => void;
25+
2426
export interface NavigatableProps {
25-
onNavigate: (link: HalLink, authorization?: string) => void;
27+
onNavigate: NavigationHandler;
2628
authorization?: string;
2729
}

0 commit comments

Comments
 (0)