-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathBarcodeScanner.tsx
More file actions
66 lines (58 loc) · 2.48 KB
/
BarcodeScanner.tsx
File metadata and controls
66 lines (58 loc) · 2.48 KB
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
import { flattenStyles } from "@mendix/piw-native-utils-internal";
import { ValueStatus } from "mendix";
import { Component, createElement } from "react";
import { View } from "react-native";
import { RNCamera } from "react-native-camera";
import BarcodeMask from "react-native-barcode-mask";
import { BarcodeScannerProps } from "../typings/BarcodeScannerProps";
import { BarcodeScannerStyle, defaultBarcodeScannerStyle } from "./ui/styles";
import { executeAction } from "@mendix/piw-utils-internal";
export type Props = BarcodeScannerProps<BarcodeScannerStyle>;
export class BarcodeScanner extends Component<Props> {
private readonly styles = flattenStyles(defaultBarcodeScannerStyle, this.props.style);
private readonly onBarCodeReadHandler = throttle(this.onBarCodeRead.bind(this), 2000);
render(): JSX.Element {
return (
<View style={this.styles.container}>
<RNCamera
testID={this.props.name}
style={{ flex: 1, justifyContent: "center", alignItems: "center" }}
type={this.props.cameraType === 'front' ? RNCamera.Constants.Type.front : RNCamera.Constants.Type.back}
captureAudio={false}
onBarCodeRead={this.onBarCodeReadHandler}
>
{this.props.showMask && (
<BarcodeMask
edgeColor={this.styles.mask.color}
width={this.styles.mask.width}
height={this.styles.mask.height}
backgroundColor={this.styles.mask.backgroundColor}
showAnimatedLine={this.props.showAnimatedLine}
/>
)}
</RNCamera>
</View>
);
}
private onBarCodeRead(event: { data: string }): void {
if (this.props.barcode.status !== ValueStatus.Available || !event.data) {
return;
}
if (event.data !== this.props.barcode.value) {
this.props.barcode.setValue(event.data);
}
executeAction(this.props.onDetect);
}
}
export function throttle<F extends (...params: any[]) => void>(fn: F, threshold: number): F {
let wait = false;
return function invokeFn(this: any, ...args: any[]) {
if (!wait) {
fn(...args);
wait = true;
setTimeout(() => {
wait = false;
}, threshold);
}
} as F;
}