Skip to content

Commit f3e058d

Browse files
committed
feat: add utils export and add some utils
1 parent 049dd12 commit f3e058d

12 files changed

+1255
-1402
lines changed

jest.config.js

-5
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,4 @@ module.exports = {
99
coverageDirectory: './coverage',
1010
testTimeout: 30000,
1111
testEnvironment: 'jsdom',
12-
globals: {
13-
'ts-jest': {
14-
tsConfig: 'tsconfig.test.json',
15-
},
16-
},
1712
};

package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,16 @@
6969
},
7070
"dependencies": {
7171
"@babel/runtime": "^7.12.5",
72-
"@planjs/utils": "^1.11.7",
72+
"@planjs/utils": "^1.13.0",
73+
"@types/hoist-non-react-statics": "^3.3.1",
7374
"fast-deep-equal": "^3.1.3",
75+
"hoist-non-react-statics": "^3.3.2",
7476
"resize-observer-polyfill": "^1.5.1"
7577
},
7678
"devDependencies": {
7779
"@commitlint/cli": "^9.1.2",
7880
"@commitlint/config-conventional": "^11.0.0",
79-
"@planjs/fabric": "^0.0.76",
81+
"@planjs/fabric": "^0.0.90",
8082
"@testing-library/react": "^12.0.0",
8183
"@testing-library/react-hooks": "^7.0.1",
8284
"@types/jest": "^26.0.24",
@@ -88,7 +90,7 @@
8890
"lint-staged": "^10.2.13",
8991
"react": "16.14.0",
9092
"react-dom": "16.14.0",
91-
"stan-builder": "^0.11.3",
93+
"stan-builder": "^0.13.3",
9294
"standard-version": "^9.0.0",
9395
"ts-jest": "^27.0.3",
9496
"typescript": "4.1.5"

src/components/if/index.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ import { isFunction } from '@planjs/utils';
33

44
export type IfProps = {
55
condition?: (() => boolean) | boolean;
6-
render?: () => React.ReactNode | undefined;
6+
render?: () => React.ReactNode | undefined | null;
77
};
88

99
const If: React.FC<IfProps> = (props) => {
1010
let condition = !!props.condition;
1111
if (props.condition && isFunction(props.condition)) {
1212
condition = props.condition();
1313
}
14-
return (
15-
condition ? (props.render ? props.render() : props.children) : null
16-
) as React.ReactElement;
14+
return condition
15+
? ((props.render ? props.render() : props.children) as React.ReactElement)
16+
: null;
1717
};
1818

1919
export default If;

src/decorators/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as withSafeSetState } from './withSafeSetState';

src/decorators/withSafeSetState.tsx

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React from 'react';
2+
import hoistNonReactStatic from '../utils/hoistNonReactStatic';
3+
4+
function withSafeSetState() {
5+
return function enhanceWithSafeSetState<T extends typeof React.Component>(WrappedComponent: T) {
6+
let isMounted = false;
7+
const { componentDidMount, componentWillUnmount, setState } = WrappedComponent.prototype;
8+
9+
WrappedComponent.prototype.componentDidMount = function () {
10+
isMounted = true;
11+
if (componentDidMount) {
12+
// @ts-ignore
13+
// eslint-disable-next-line prefer-rest-params
14+
componentDidMount.apply(this, arguments);
15+
}
16+
};
17+
18+
WrappedComponent.prototype.componentWillUnmount = function () {
19+
isMounted = false;
20+
if (componentWillUnmount) {
21+
// @ts-ignore
22+
// eslint-disable-next-line prefer-rest-params
23+
componentWillUnmount.apply(this, arguments);
24+
}
25+
};
26+
27+
WrappedComponent.prototype.setState = function () {
28+
if (isMounted && setState) {
29+
// @ts-ignore
30+
// eslint-disable-next-line prefer-rest-params
31+
setState.apply(this, arguments);
32+
}
33+
};
34+
35+
const WithSafeSetState = (props: any) => {
36+
return <WrappedComponent {...props} />;
37+
};
38+
39+
return hoistNonReactStatic(WithSafeSetState, WrappedComponent);
40+
};
41+
}
42+
43+
export default withSafeSetState;

src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
export * from './components';
2+
export * from './decorators';
23
export * from './hooks';
4+
export * from './utils';

src/utils/hoistNonReactStatic.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import hoistNonReactStatic from 'hoist-non-react-statics';
2+
3+
export default hoistNonReactStatic;

src/utils/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export { default as getActionState } from './getActionState';
2+
export { default as hoistNonReactStatic } from './hoistNonReactStatic';
3+
export { default as isClassComponent } from './isClassComponent';
4+
export { default as isDeepEqual } from './isDeepEqual';
5+
export { default as isReactKey } from './isReactKey';
6+
export { default as renderChildren } from './renderChildren';

src/utils/isReactKey.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import React from 'react';
22
import { isNumber, isString } from '@planjs/utils';
33

4+
/**
5+
* check is react valid key
6+
* @param key
7+
*/
48
function isReactKey(key: any): key is React.Key {
59
return isString(key) || isNumber(key);
610
}

src/utils/merge.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@ import { isArray, isPlanObject, isSet, isMap } from '@planjs/utils';
33
function merge<T>(a: any, b: any): T {
44
if (isArray(a) && isArray(b)) {
55
return [...a, ...b] as unknown as T;
6-
} else if (isPlanObject(a) && isPlanObject(b)) {
6+
}
7+
if (isPlanObject(a) && isPlanObject(b)) {
78
return {
89
...a,
910
...b,
1011
} as T;
11-
} else if (isMap(a) && isMap(b)) {
12+
}
13+
if (isMap(a) && isMap(b)) {
1214
return new Map([...a, ...b]) as unknown as T;
13-
} else if (isSet(a) && isSet(b)) {
15+
}
16+
if (isSet(a) && isSet(b)) {
1417
return new Set([...a, ...b]) as unknown as T;
15-
} else {
16-
return a;
1718
}
19+
return a;
1820
}
1921

2022
export default merge;

tsconfig.test.json

-3
This file was deleted.

0 commit comments

Comments
 (0)