forked from shendel/react-qr-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
story.js
115 lines (107 loc) · 3.21 KB
/
story.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import React, { Component } from "react";
import { storiesOf } from "@storybook/react";
import { action } from '@storybook/addon-actions';
import Reader from "./lib";
class Wrapper extends Component {
constructor(props) {
super(props)
this.state = { cameraId: undefined, delay: 500, devices: [], loading: false }
}
componentWillMount() {
const { selectFacingMode } = this.props
if (navigator && selectFacingMode) {
this.setState({
loading: true,
})
navigator.mediaDevices.enumerateDevices()
.then((devices) => {
const videoSelect = []
devices.forEach((device) => {
if (device.kind === 'videoinput') {
videoSelect.push(device)
}
})
return videoSelect
})
.then((devices) => {
this.setState({
cameraId: devices[0].deviceId,
devices,
loading: false,
})
})
.catch((error) => {
console.log(error)
})
}
}
selectCamera = () => {
return this.state.cameraId
}
render() {
const { selectFacingMode, selectDelay, legacyMode } = this.props
const { cameraId, devices } = this.state
const previewStyle = { width: 320 }
return (
<div>
{
selectFacingMode && devices.length && (
<select
onChange={e => {
const value = e.target.value
this.setState({ cameraId: undefined }, () => {
this.setState({ cameraId: value })
})
}}
>
{devices.map((deviceInfo, index) => (
<React.Fragment key={deviceInfo.deviceId}><option value={deviceInfo.deviceId}>{deviceInfo.label || `camera ${index}`}</option></React.Fragment>
))}
</select>
)
}
{
selectDelay && (
<div>
<button onClick={() => this.setState({ delay: false })}>
Disable Delay
</button>
<input
placeholder="Delay in ms"
type="number"
value={this.state.delay}
onChange={e =>
this.setState({ delay: parseInt(e.target.value) })}
/>
</div>
)
}
{(cameraId || !selectFacingMode) && (<Reader
chooseDeviceId={this.selectCamera}
style={previewStyle}
onError={action('Error')}
onScan={action('Scan')}
onLoad={action('Load')}
onImageLoad={action('ImageLoad')}
ref="reader"
legacyMode={legacyMode}
maxImageSize={1000}
delay={this.state.delay}
className="reader-container"
/>)}
{
legacyMode && (
<button onClick={() => this.refs.reader.openImageDialog()}>
Open Image Dialog
</button>
)
}
</div>
)
}
}
storiesOf("QR Reader", module)
.add('Camera not specified', () => <Wrapper />)
.add('Choose camera', () => <Wrapper selectFacingMode />)
.add('Legacy mode', () => <Wrapper legacyMode />)
.add('Choose delay', () => <Wrapper selectDelay />)