Skip to content

Commit 7e6d92e

Browse files
committed
feat: add faccToHoc()
1 parent 741b65f commit 7e6d92e

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

src/BatterySensor/__story__/story.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const Print = (props) => h('pre', {
1313
JSON.stringify(props, null, 4)
1414
);
1515

16-
const PrintBattery = withBattery(Print);
16+
const PrintBattery = withBattery(Print, 'overwritten');
1717

1818
@withBattery
1919
class Printer extends Component<any, any> {

src/BatterySensor/index.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Component} from 'react';
22
import {h, on, off, isClient} from '../util';
3-
import addClassDecoratorSupport from '../util/addClassDecoratorSupport';
3+
import faccToHoc from '../util/faccToHoc';
44

55
export interface IBatterySensorProps {
66
children?: (INetworkState) => React.ReactElement<any>;
@@ -70,14 +70,4 @@ export class BatterySensor extends Component<IBatterySensorProps, IBatterySensor
7070
}
7171
}
7272

73-
export const withBattery = (Comp) => {
74-
const Enhanced = (props) =>
75-
h(BatterySensor, null, (battery) =>
76-
h(Comp, {
77-
...props,
78-
battery
79-
})
80-
);
81-
82-
return addClassDecoratorSupport(Enhanced);
83-
};
73+
export const withBattery = faccToHoc(BatterySensor, 'battery');

src/util/faccToHoc.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {h} from '../util';
2+
import addClassDecoratorSupport from './addClassDecoratorSupport';
3+
4+
const faccToHoc = (Facc, prop?: string) =>
5+
(Comp, propName: string = prop) => {
6+
const Enhanced = (props) =>
7+
h(Facc, null, (state) =>
8+
h(Comp,
9+
propName ?
10+
{[propName]: state, ...props} :
11+
{...state, ...props}
12+
)
13+
);
14+
15+
return addClassDecoratorSupport(Enhanced);
16+
};
17+
18+
export default faccToHoc;

0 commit comments

Comments
 (0)