-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathRfidInput.jsx
196 lines (169 loc) · 5.12 KB
/
RfidInput.jsx
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* @component
*/
import classnames from 'clsx';
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import Button from '../../react-chayns-button/component/Button';
import Input from '../../react-chayns-input/component/Input';
import { RFID_CONTENT, SPLIT_RFID, VALID_RFID } from '../constants/regex';
/**
* A component to take in an RFID signal.
*/
export default class RfidInput extends Component {
static pretifyRfid(rfid) {
return rfid ? rfid.match(SPLIT_RFID).join(' ') : '';
}
static isNfcAvailable() {
return (
(chayns.env.isMyChaynsApp &&
(chayns.env.isAndroid ||
(chayns.env.isIOS &&
chayns.env.myChaynsAppVersion >= 5764))) ||
(chayns.env.isApp && chayns.env.isAndroid)
);
}
constructor(props) {
super(props);
this.state = {
isScanning: false,
};
}
onConfirm = () => {
const { onConfirm, value } = this.props;
onConfirm(value);
};
onInput = (newRfid) => {
const { onInput } = this.props;
const newValue = newRfid.toUpperCase().replace(/\s/g, '');
if (!RFID_CONTENT.test(newValue)) {
return;
}
onInput(newValue);
};
onScan = (rfid) => {
this.endScan();
if (VALID_RFID.test(rfid)) {
const newRfid = rfid.toUpperCase();
const { onConfirm, onInput } = this.props;
onInput(newRfid);
onConfirm(newRfid);
}
};
startScan = () => {
this.setState({ isScanning: true });
chayns.addNfcListener(this.onScan);
chayns.showWaitCursor();
};
endScan = () => {
const { isScanning } = this.state;
if (!isScanning) {
return;
}
chayns.removeNfcListener();
chayns.hideWaitCursor();
this.setState({ isScanning: false });
};
render() {
const {
className,
style,
placeholder,
confirmNode,
enableScan,
scanText,
value,
} = this.props;
const { isScanning } = this.state;
const classNames = classnames(className, 'cc__rfid-input', {
'cc__rfid-input--enable-scan': enableScan,
});
const disabled = !VALID_RFID.test(value);
return (
<div className={classNames} style={style}>
<div className="cc__rfid-input__wrapper">
<Input
className="cc__rfid-input__input"
placeholder={placeholder}
onChange={this.onInput}
value={RfidInput.pretifyRfid(value)}
autoCorrect="off"
autoCapitalize="off"
spellCheck="false"
/>
{enableScan && !value && (
<Button
secondary
onClick={isScanning ? this.endScan : this.startScan}
className="cc__rfid-input__scan"
>
{scanText}
</Button>
)}
{(!enableScan || value) && (
<Button
secondary
onClick={this.onConfirm}
disabled={disabled}
className="cc__rfid-input__confirm"
>
{confirmNode}
</Button>
)}
</div>
</div>
);
}
}
RfidInput.propTypes = {
/**
* A classname string that will be applied to the container element.
*/
className: PropTypes.string,
/**
* A React style object that will be applied to the container element.
*/
style: PropTypes.objectOf(
PropTypes.oneOfType([PropTypes.string, PropTypes.number])
),
/**
* A string that will be shown as the placeholder.
*/
placeholder: PropTypes.string,
/**
* A string or `ReactNode` that will be the content of the confirm button.
*/
confirmNode: PropTypes.oneOfType([
PropTypes.node,
PropTypes.arrayOf(PropTypes.node),
]),
/**
* Wether to enable the scan-button for scanning a card.
*/
enableScan: PropTypes.bool,
/**
* A string that will be the content of the scan-button.
*/
scanText: PropTypes.string,
/**
* The input value.
*/
value: PropTypes.string.isRequired,
/**
* A callback for when the `<input>`-elements content changes.
*/
onInput: PropTypes.func.isRequired,
/**
* This will be called when the RFID input is completed and validated.
*/
onConfirm: PropTypes.func.isRequired,
};
RfidInput.defaultProps = {
className: null,
style: null,
placeholder: 'Kartennummer',
confirmNode: 'OK',
enableScan: false,
scanText: 'Scannen',
};
RfidInput.displayName = 'RfidInput';