-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (27 loc) · 1.16 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
import React, { useRef, useState, useEffect } from 'react'
import { TextInput } from 'react-native'
function extractSerial(data) {
if (!data) return ''
return parseInt(data).toString(16).toUpperCase().match(/.{1,2}/g).reverse().join("")
}
const NFCTextInputScanner = ({ callback }) => {
const inputRef = useRef()
const [ tagInputText, setTagInputText ] = useState(() => '')
const [ inputFocusInterval, setInputFocusInterval ] = useState(() => 0)
useEffect(() => {
setInputFocusInterval(setInterval(function () {
if (inputRef.current) inputRef.current.focus()
}, 3000))
return () => clearInterval(inputFocusInterval)
}, [])
useEffect(() => {
if (tagInputText.length >= 10) {
callback(extractSerial(tagInputText))
setTagInputText('')
}
}, [tagInputText])
return (
<TextInput value={tagInputText} onChangeText={setTagInputText} ref={input => inputRef.current = input} showSoftInputOnFocus={false} autoFocus={true} style={{ width: 0, height: 0, zIndex: -100, position: 'absolute', top: 0, left: 0 }} />
)
}
export default NFCTextInputScanner;