Skip to content

Commit 7e0011a

Browse files
committed
feat: add withMediaDevices()
1 parent cd94a9d commit 7e0011a

File tree

6 files changed

+68
-22
lines changed

6 files changed

+68
-22
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ const MyComponent = mock();
3535
- [`delayed()`](./docs/delayed.md)
3636
- [`invert()`](./docs/invert.md)
3737
- Sensors
38-
- [`<BatterySensor>`](./docs/BatterySensor.md), [`withBattery()`](./docs/BatterySensor.md#withbattery), and [`@withBattery`](./docs/BatterySensor.md#@withbattery)
39-
- [`<MediaDeviceSensor>`](./docs/MediaDeviceSensor.md)
38+
- [`<BatterySensor>`](./docs/BatterySensor.md), [`withBattery()`](./docs/BatterySensor.md#withbattery), and [`@withBattery`](./docs/BatterySensor.md#withbattery-1)
39+
- [`<MediaDeviceSensor>`](./docs/MediaDeviceSensor.md), [`withMediaDevices`](./docs/MediaDeviceSensor.md#withmediadevices), and [`@withMediaDevices`](./docs/MediaDeviceSensor.md#withmediadevices-1)
4040
- [`<MediaSensor>`](./docs/MediaSensor.md)
4141
- [`<NetworkSensor>`](./docs/NetworkSensor.md) and [`withNetwork()`](./docs/NetworkSensor.md#withnetwork)
4242
- [`<LightSensor>`](./docs/LightSensor.md)

src/BatterySensor/__story__/story.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,14 @@ storiesOf('Sensors/BatterySensor', module)
3939
)
4040
)
4141
)
42+
.add('Render prop', () =>
43+
h(BatterySensor, {
44+
render: (state) => h('pre', {style: {
45+
fontFamily: 'monospace'
46+
}},
47+
JSON.stringify(state, null, 4)
48+
)
49+
})
50+
)
4251
.add('HOC', () => h(PrintBattery))
4352
.add('Decorator', () => h(Printer));

src/BatterySensor/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import {Component} from 'react';
22
import {h, on, off, isClient} from '../util';
33
import faccToHoc from '../util/faccToHoc';
4+
import renderProp from '../util/renderProp';
45

56
export interface IBatterySensorProps {
67
children?: (INetworkState) => React.ReactElement<any>;
8+
render?: (INetworkState) => React.ReactElement<any>;
79
}
810

911
export interface IBatterySensorState {
@@ -66,7 +68,7 @@ export class BatterySensor extends Component<IBatterySensorProps, IBatterySensor
6668
};
6769

6870
render () {
69-
return this.props.children(this.state);
71+
return renderProp(this.props, this.state);
7072
}
7173
}
7274

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 {MediaDeviceSensor, withMediaDevices} from '..';
6+
import ShowDocs from '../../../.storybook/ShowDocs'
7+
8+
const Print = (props) => h('pre', {
9+
style: {fontFamily: 'monospace'}
10+
},
11+
JSON.stringify(props, null, 4)
12+
);
13+
14+
const PrintWithDevices = withMediaDevices(Print);
15+
16+
@withMediaDevices
17+
class Printer extends Component<any, any> {
18+
render () {
19+
return h('pre', {
20+
style: {fontFamily: 'monospace'}
21+
},
22+
JSON.stringify(this.props, null, 4)
23+
);
24+
}
25+
}
26+
27+
storiesOf('Sensors/MediaDeviceSensor', module)
28+
.add('Documentation', () => h(ShowDocs, {name: 'MediaDeviceSensor'}))
29+
.add('FaCC', () =>
30+
<div>
31+
<MediaDeviceSensor>{(state) =>
32+
<pre style={{fontFamily: 'monospace'}}>
33+
{JSON.stringify(state, null, 4)}
34+
</pre>
35+
}</MediaDeviceSensor>
36+
</div>
37+
)
38+
.add('Render prop', () =>
39+
<div>
40+
<MediaDeviceSensor render={(state) =>
41+
<pre style={{fontFamily: 'monospace'}}>
42+
{JSON.stringify(state, null, 4)}
43+
</pre>
44+
} />
45+
</div>
46+
)
47+
.add('HOC', () => <PrintWithDevices />)
48+
.add('Decorator', () => <Printer />);

src/MediaDeviceSensor/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {Component} from 'react';
22
import {on, off, noop} from '../util';
3+
import faccToHoc from '../util/faccToHoc';
4+
import renderProp from '../util/renderProp';
35

46
export interface IMediaDevice {
57
deviceId: string;
@@ -10,6 +12,7 @@ export interface IMediaDevice {
1012

1113
export interface IMediaDeviceSensorProps {
1214
children?: (state: IMediaDeviceSensorState) => React.ReactElement<any>;
15+
render?: (state: IMediaDeviceSensorState) => React.ReactElement<any>;
1316
}
1417

1518
export interface IMediaDeviceSensorState {
@@ -49,6 +52,8 @@ export class MediaDeviceSensor extends Component<IMediaDeviceSensorProps, IMedia
4952
};
5053

5154
render () {
52-
return this.props.children(this.state);
55+
return renderProp(this.props, this.state);
5356
}
5457
}
58+
59+
export const withMediaDevices = faccToHoc(MediaDeviceSensor);

src/MediaDeviceSensor/story.tsx

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

0 commit comments

Comments
 (0)