forked from thebylito/react-native-scratch-card
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (42 loc) · 1.26 KB
/
index.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
import React, { Component } from 'react'
import { requireNativeComponent, ViewPropTypes, NativeModules, DeviceEventEmitter } from 'react-native';
import PropTypes from 'prop-types'
const { RNScratchCard } = NativeModules;
const iface = {
name: 'RNScratchCardManager',
propTypes: {
brushSize: PropTypes.number,
color: PropTypes.string,
maxPercent: PropTypes.number,
...ViewPropTypes
}
};
const RCTRNScratchCard = requireNativeComponent('RNScratchCardManager', iface);
class RNScratchCardView extends Component {
state = { ready: true }
componentDidMount = () => {
DeviceEventEmitter.addListener('percentEv', this.listem)
}
listem = (data) => {
const { ready } = this.state;
this.props.getPercent(data.value)
if (data.value >= this.props.maxPercent) {
if (ready) {
this.setState({ ready: false }, () => {
this.props.onEnd()
})
}
}
if (data.value >= 100) {
DeviceEventEmitter.removeListener('percentEv')
}
}
componentWillUnmount = () => {
DeviceEventEmitter.removeListener('percentEv')
}
render() {
return <RCTRNScratchCard ref={myView => { this.myView = myView }} {...this.props} />
}
}
export default RNScratchCardView;
export { RNScratchCard };