Skip to content

Commit 2f1f802

Browse files
committed
feat: add withMedia() and @withmedia
1 parent 1e95196 commit 2f1f802

File tree

5 files changed

+75
-34
lines changed

5 files changed

+75
-34
lines changed

src/BatterySensor/__story__/story.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ storiesOf('Sensors/BatterySensor', module)
4141
)
4242
.add('Render prop', () =>
4343
h(BatterySensor, {
44-
render: (state) => h('pre', {style: {
45-
fontFamily: 'monospace'
46-
}},
47-
JSON.stringify(state, null, 4)
48-
)
44+
render: (state) =>
45+
h('pre', {style: {
46+
fontFamily: 'monospace'
47+
}},
48+
JSON.stringify(state, null, 4)
49+
)
4950
})
5051
)
5152
.add('HOC', () => h(PrintBattery))

src/MediaSensor/__story__/story.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import {Component, createElement as h} from 'react';
2+
import {storiesOf} from '@storybook/react';
3+
import {action} from '@storybook/addon-actions';
4+
import {linkTo} from '@storybook/addon-links';
5+
import {MediaSensor, withMedia} from '..';
6+
import ShowDocs from '../../../.storybook/ShowDocs'
7+
8+
const IsBig = ({isBig}) => h('div', null, `WIDTH IS GREATED THAN 480PX: ${isBig}`);
9+
10+
const IsBigWithMedia = withMedia(IsBig, 'isBig', {
11+
query: '(min-width: 480px)'
12+
});
13+
14+
@withMedia('isBig', {
15+
query: '(min-width: 480px)'
16+
})
17+
class IsBigClass extends Component<any, any> {
18+
render () {
19+
return h('div', null, `WIDTH IS GREATED THAN 480PX: ${this.props.isBig}`);
20+
}
21+
}
22+
23+
storiesOf('Sensors/MediaSensor', module)
24+
.add('Documentation', () => h(ShowDocs, {name: 'MediaSensor'}))
25+
.add('FaCC', () =>
26+
h(MediaSensor, {
27+
query: '(min-width: 480px)'
28+
}, (matches) =>
29+
h('div', {
30+
style: {
31+
border: '1px solid red'
32+
}
33+
},
34+
`WIDTH IS GREATED THAN 480PX: ${matches}`
35+
)
36+
)
37+
)
38+
.add('Render prop', () =>
39+
h(MediaSensor, {
40+
query: '(min-width: 480px)',
41+
render: (matches) =>
42+
h('div', {
43+
style: {
44+
border: '1px solid red'
45+
}
46+
},
47+
`WIDTH IS GREATED THAN 480PX: ${matches}`
48+
)
49+
})
50+
)
51+
.add('HOC', () => h(IsBigWithMedia))
52+
.add('Decorator', () => h(IsBigClass));

src/MediaSensor/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import {Component} from 'react';
2+
import faccToHoc from '../util/faccToHoc';
3+
import renderProp from '../util/renderProp';
24

35
export interface IMediaSensorProps {
46
matches?: boolean;
@@ -67,9 +69,8 @@ export class MediaSensor extends Component<IMediaSensorProps, IMediaSensorState>
6769
}
6870

6971
render () {
70-
const {children} = this.props;
71-
const {matches} = this.state;
72-
73-
return children(matches);
72+
return renderProp(this.props, this.state.matches);
7473
}
7574
}
75+
76+
export const withMedia = faccToHoc(MediaSensor);

src/MediaSensor/story.ts

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

src/util/faccToHoc.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import {h} from '../util';
22
import addClassDecoratorSupport from './addClassDecoratorSupport';
33

4-
const faccToHoc = (Facc, prop?: string) =>
5-
(Comp, propName: string = prop) => {
4+
const faccToHoc = (Facc, prop?: string) => {
5+
const hoc = (Comp, propName: any = prop, faccProps: object = null) => {
6+
const isClassDecoratorMethodCall = typeof Comp === 'string';
7+
8+
if (isClassDecoratorMethodCall) {
9+
return (Klass) => hoc(Klass, Comp as any, propName as any);
10+
}
11+
612
const Enhanced = (props) =>
7-
h(Facc, null, (state) =>
13+
h(Facc, faccProps, (state) =>
814
h(Comp,
915
propName ?
1016
{[propName]: state, ...props} :
@@ -15,4 +21,7 @@ const faccToHoc = (Facc, prop?: string) =>
1521
return addClassDecoratorSupport(Enhanced);
1622
};
1723

24+
return hoc;
25+
}
26+
1827
export default faccToHoc;

0 commit comments

Comments
 (0)