Skip to content

Commit 20c9ee6

Browse files
committed
feat: add various util methods
1 parent 4f91f47 commit 20c9ee6

File tree

5 files changed

+65
-0
lines changed

5 files changed

+65
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import isStatelessComponent from './isStatelessComponent';
2+
import wrapInStatefulComponent from './wrapInStatefulComponent';
3+
4+
const addClassDecoratorSupport = (Comp) => {
5+
return isStatelessComponent(Comp) ?
6+
Comp :
7+
wrapInStatefulComponent(Comp);
8+
};
9+
10+
export default addClassDecoratorSupport;

src/util/isStatelessComponent.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const isStatelessComponent = (Comp) => {
2+
if (typeof Comp !== 'function') {
3+
return false;
4+
}
5+
6+
return !Comp.prototype || (Comp instanceof Function);
7+
};
8+
9+
export default isStatelessComponent;

src/util/mapProps.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import {h} from '../util';
2+
3+
export const mapProps = (mapper) => (Comp) => (props) => h(Comp, mapper(props));

src/util/renderProp.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {isFn} from '../util';
2+
3+
const renderProp = (props, ...args) => {
4+
if (process.env.NODE_ENV !== 'production') {
5+
if (typeof props !== 'object') {
6+
throw new TypeError('renderProp(props, data) first argument must be a prop object.');
7+
}
8+
9+
const {children, render} = props;
10+
11+
if (isFn(children) && isFn(render)) {
12+
console.warn(
13+
'Both "render" and "children" are specified for a render-prop component. ' +
14+
'Children will be used.'
15+
);
16+
console.trace();
17+
}
18+
}
19+
20+
const {children, render} = props;
21+
22+
return isFn(children) ?
23+
children(...args) :
24+
isFn(render) ?
25+
render(...args) :
26+
(children || null);
27+
};
28+
29+
export default renderProp;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {Component} from 'react';
2+
import {h} from '../util';
3+
4+
const wrapInStatefulComponent = (Comp) => {
5+
const Decorated = class Decorated extends Component<any, any> {
6+
render () {
7+
return h(Comp, this.props);
8+
}
9+
};
10+
11+
return Decorated;
12+
};
13+
14+
export default wrapInStatefulComponent;

0 commit comments

Comments
 (0)