@salutejs/plasma-new-hope 0.336.0-canary.2208.17465207747.0 → 0.336.0-canary.2210.17487143389.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/components/CodeField/CodeField.css +7 -6
- package/cjs/components/CodeField/CodeField.js +54 -23
- package/cjs/components/CodeField/CodeField.js.map +1 -1
- package/cjs/components/CodeField/CodeField.styles.js +7 -1
- package/cjs/components/CodeField/CodeField.styles.js.map +1 -1
- package/cjs/components/CodeField/{CodeField.styles_ebdpd0.css → CodeField.styles_14p25hi.css} +1 -0
- package/cjs/components/CodeField/hooks/useWebOTP.js +103 -0
- package/cjs/components/CodeField/hooks/useWebOTP.js.map +1 -0
- package/cjs/components/CodeField/utils/handleCodeError.js +2 -2
- package/cjs/components/CodeField/utils/handleCodeError.js.map +1 -1
- package/cjs/components/CodeField/utils/handleItemError.js +14 -4
- package/cjs/components/CodeField/utils/handleItemError.js.map +1 -1
- package/cjs/components/CodeField/utils/isWebOTPSupported.js +10 -0
- package/cjs/components/CodeField/utils/isWebOTPSupported.js.map +1 -0
- package/cjs/components/Drawer/hooks/useDrawer.js +0 -12
- package/cjs/components/Drawer/hooks/useDrawer.js.map +1 -1
- package/cjs/index.css +7 -6
- package/emotion/cjs/components/CodeField/CodeField.js +47 -19
- package/emotion/cjs/components/CodeField/CodeField.styles.js +18 -11
- package/emotion/cjs/components/CodeField/hooks/useWebOTP.js +254 -0
- package/emotion/cjs/components/CodeField/utils/handleCodeError.js +2 -2
- package/emotion/cjs/components/CodeField/utils/handleItemError.js +12 -3
- package/emotion/cjs/components/CodeField/utils/index.js +4 -0
- package/emotion/cjs/components/CodeField/utils/isWebOTPSupported.js +13 -0
- package/emotion/cjs/components/Drawer/hooks/useDrawer.js +0 -11
- package/emotion/es/components/CodeField/CodeField.js +49 -21
- package/emotion/es/components/CodeField/CodeField.styles.js +15 -11
- package/emotion/es/components/CodeField/hooks/useWebOTP.js +244 -0
- package/emotion/es/components/CodeField/utils/handleCodeError.js +2 -2
- package/emotion/es/components/CodeField/utils/handleItemError.js +12 -3
- package/emotion/es/components/CodeField/utils/index.js +1 -0
- package/emotion/es/components/CodeField/utils/isWebOTPSupported.js +3 -0
- package/emotion/es/components/Drawer/hooks/useDrawer.js +0 -11
- package/es/components/CodeField/CodeField.css +7 -6
- package/es/components/CodeField/CodeField.js +56 -25
- package/es/components/CodeField/CodeField.js.map +1 -1
- package/es/components/CodeField/CodeField.styles.js +7 -2
- package/es/components/CodeField/CodeField.styles.js.map +1 -1
- package/es/components/CodeField/{CodeField.styles_ebdpd0.css → CodeField.styles_14p25hi.css} +1 -0
- package/es/components/CodeField/hooks/useWebOTP.js +99 -0
- package/es/components/CodeField/hooks/useWebOTP.js.map +1 -0
- package/es/components/CodeField/utils/handleCodeError.js +2 -2
- package/es/components/CodeField/utils/handleCodeError.js.map +1 -1
- package/es/components/CodeField/utils/handleItemError.js +14 -4
- package/es/components/CodeField/utils/handleItemError.js.map +1 -1
- package/es/components/CodeField/utils/isWebOTPSupported.js +6 -0
- package/es/components/CodeField/utils/isWebOTPSupported.js.map +1 -0
- package/es/components/Drawer/hooks/useDrawer.js +0 -12
- package/es/components/Drawer/hooks/useDrawer.js.map +1 -1
- package/es/index.css +7 -6
- package/package.json +2 -2
- package/styled-components/cjs/components/CodeField/CodeField.js +47 -19
- package/styled-components/cjs/components/CodeField/CodeField.styles.js +14 -5
- package/styled-components/cjs/components/CodeField/hooks/useWebOTP.js +254 -0
- package/styled-components/cjs/components/CodeField/utils/handleCodeError.js +2 -2
- package/styled-components/cjs/components/CodeField/utils/handleItemError.js +12 -3
- package/styled-components/cjs/components/CodeField/utils/index.js +4 -0
- package/styled-components/cjs/components/CodeField/utils/isWebOTPSupported.js +13 -0
- package/styled-components/cjs/components/Drawer/hooks/useDrawer.js +0 -11
- package/styled-components/es/components/CodeField/CodeField.js +49 -21
- package/styled-components/es/components/CodeField/CodeField.styles.js +11 -5
- package/styled-components/es/components/CodeField/hooks/useWebOTP.js +244 -0
- package/styled-components/es/components/CodeField/utils/handleCodeError.js +2 -2
- package/styled-components/es/components/CodeField/utils/handleItemError.js +12 -3
- package/styled-components/es/components/CodeField/utils/index.js +1 -0
- package/styled-components/es/components/CodeField/utils/isWebOTPSupported.js +3 -0
- package/styled-components/es/components/Drawer/hooks/useDrawer.js +0 -11
- package/types/components/CodeField/CodeField.d.ts.map +1 -1
- package/types/components/CodeField/CodeField.styles.d.ts +1 -0
- package/types/components/CodeField/CodeField.styles.d.ts.map +1 -1
- package/types/components/CodeField/hooks/useWebOTP.d.ts +23 -0
- package/types/components/CodeField/hooks/useWebOTP.d.ts.map +1 -0
- package/types/components/CodeField/utils/handleCodeError.d.ts +2 -2
- package/types/components/CodeField/utils/handleCodeError.d.ts.map +1 -1
- package/types/components/CodeField/utils/handleItemError.d.ts +3 -2
- package/types/components/CodeField/utils/handleItemError.d.ts.map +1 -1
- package/types/components/CodeField/utils/index.d.ts +1 -0
- package/types/components/CodeField/utils/index.d.ts.map +1 -1
- package/types/components/CodeField/utils/isWebOTPSupported.d.ts +2 -0
- package/types/components/CodeField/utils/isWebOTPSupported.d.ts.map +1 -0
- package/types/components/Drawer/hooks/useDrawer.d.ts.map +1 -1
@@ -0,0 +1,254 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
3
|
+
value: true
|
4
|
+
});
|
5
|
+
Object.defineProperty(exports, "useWebOTP", {
|
6
|
+
enumerable: true,
|
7
|
+
get: function() {
|
8
|
+
return useWebOTP;
|
9
|
+
}
|
10
|
+
});
|
11
|
+
var _react = require("react");
|
12
|
+
var _utils = require("../utils");
|
13
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
14
|
+
try {
|
15
|
+
var info = gen[key](arg);
|
16
|
+
var value = info.value;
|
17
|
+
} catch (error) {
|
18
|
+
reject(error);
|
19
|
+
return;
|
20
|
+
}
|
21
|
+
if (info.done) {
|
22
|
+
resolve(value);
|
23
|
+
} else {
|
24
|
+
Promise.resolve(value).then(_next, _throw);
|
25
|
+
}
|
26
|
+
}
|
27
|
+
function _async_to_generator(fn) {
|
28
|
+
return function() {
|
29
|
+
var self = this, args = arguments;
|
30
|
+
return new Promise(function(resolve, reject) {
|
31
|
+
var gen = fn.apply(self, args);
|
32
|
+
function _next(value) {
|
33
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
34
|
+
}
|
35
|
+
function _throw(err) {
|
36
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
37
|
+
}
|
38
|
+
_next(undefined);
|
39
|
+
});
|
40
|
+
};
|
41
|
+
}
|
42
|
+
function _instanceof(left, right) {
|
43
|
+
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
44
|
+
return !!right[Symbol.hasInstance](left);
|
45
|
+
} else {
|
46
|
+
return left instanceof right;
|
47
|
+
}
|
48
|
+
}
|
49
|
+
function _ts_generator(thisArg, body) {
|
50
|
+
var f, y, t, _ = {
|
51
|
+
label: 0,
|
52
|
+
sent: function() {
|
53
|
+
if (t[0] & 1) throw t[1];
|
54
|
+
return t[1];
|
55
|
+
},
|
56
|
+
trys: [],
|
57
|
+
ops: []
|
58
|
+
}, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
59
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
60
|
+
return this;
|
61
|
+
}), g;
|
62
|
+
function verb(n) {
|
63
|
+
return function(v) {
|
64
|
+
return step([
|
65
|
+
n,
|
66
|
+
v
|
67
|
+
]);
|
68
|
+
};
|
69
|
+
}
|
70
|
+
function step(op) {
|
71
|
+
if (f) throw new TypeError("Generator is already executing.");
|
72
|
+
while(g && (g = 0, op[0] && (_ = 0)), _)try {
|
73
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
74
|
+
if (y = 0, t) op = [
|
75
|
+
op[0] & 2,
|
76
|
+
t.value
|
77
|
+
];
|
78
|
+
switch(op[0]){
|
79
|
+
case 0:
|
80
|
+
case 1:
|
81
|
+
t = op;
|
82
|
+
break;
|
83
|
+
case 4:
|
84
|
+
_.label++;
|
85
|
+
return {
|
86
|
+
value: op[1],
|
87
|
+
done: false
|
88
|
+
};
|
89
|
+
case 5:
|
90
|
+
_.label++;
|
91
|
+
y = op[1];
|
92
|
+
op = [
|
93
|
+
0
|
94
|
+
];
|
95
|
+
continue;
|
96
|
+
case 7:
|
97
|
+
op = _.ops.pop();
|
98
|
+
_.trys.pop();
|
99
|
+
continue;
|
100
|
+
default:
|
101
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
102
|
+
_ = 0;
|
103
|
+
continue;
|
104
|
+
}
|
105
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
106
|
+
_.label = op[1];
|
107
|
+
break;
|
108
|
+
}
|
109
|
+
if (op[0] === 6 && _.label < t[1]) {
|
110
|
+
_.label = t[1];
|
111
|
+
t = op;
|
112
|
+
break;
|
113
|
+
}
|
114
|
+
if (t && _.label < t[2]) {
|
115
|
+
_.label = t[2];
|
116
|
+
_.ops.push(op);
|
117
|
+
break;
|
118
|
+
}
|
119
|
+
if (t[2]) _.ops.pop();
|
120
|
+
_.trys.pop();
|
121
|
+
continue;
|
122
|
+
}
|
123
|
+
op = body.call(thisArg, _);
|
124
|
+
} catch (e) {
|
125
|
+
op = [
|
126
|
+
6,
|
127
|
+
e
|
128
|
+
];
|
129
|
+
y = 0;
|
130
|
+
} finally{
|
131
|
+
f = t = 0;
|
132
|
+
}
|
133
|
+
if (op[0] & 5) throw op[1];
|
134
|
+
return {
|
135
|
+
value: op[0] ? op[1] : void 0,
|
136
|
+
done: true
|
137
|
+
};
|
138
|
+
}
|
139
|
+
}
|
140
|
+
// Type guard function to check if credential is OTPCredential
|
141
|
+
var isOTPCredential = function(credential) {
|
142
|
+
return credential !== null && 'code' in credential;
|
143
|
+
};
|
144
|
+
var useWebOTP = function(param) {
|
145
|
+
var codeString = param.codeString, enableSMSAutoRead = param.enableSMSAutoRead, disabled = param.disabled, codeLength = param.codeLength, codeSetter = param.codeSetter, onFullCodeEnter = param.onFullCodeEnter;
|
146
|
+
var abortControllerRef = (0, _react.useRef)(null);
|
147
|
+
var startWebOTPListener = (0, _react.useCallback)(function() {
|
148
|
+
return _async_to_generator(function() {
|
149
|
+
var otp, otpCode, newCode, err;
|
150
|
+
return _ts_generator(this, function(_state) {
|
151
|
+
switch(_state.label){
|
152
|
+
case 0:
|
153
|
+
if (!enableSMSAutoRead || disabled || !(0, _utils.isWebOTPSupported)()) {
|
154
|
+
return [
|
155
|
+
2
|
156
|
+
];
|
157
|
+
}
|
158
|
+
if (abortControllerRef.current) {
|
159
|
+
abortControllerRef.current.abort();
|
160
|
+
}
|
161
|
+
abortControllerRef.current = new AbortController();
|
162
|
+
_state.label = 1;
|
163
|
+
case 1:
|
164
|
+
_state.trys.push([
|
165
|
+
1,
|
166
|
+
3,
|
167
|
+
,
|
168
|
+
4
|
169
|
+
]);
|
170
|
+
return [
|
171
|
+
4,
|
172
|
+
navigator.credentials.get({
|
173
|
+
otp: {
|
174
|
+
transport: [
|
175
|
+
'sms'
|
176
|
+
]
|
177
|
+
},
|
178
|
+
signal: abortControllerRef.current.signal
|
179
|
+
})
|
180
|
+
];
|
181
|
+
case 2:
|
182
|
+
otp = _state.sent();
|
183
|
+
// Type guard to check if it's an OTP credential
|
184
|
+
if (otp && isOTPCredential(otp) && otp.code) {
|
185
|
+
otpCode = otp.code;
|
186
|
+
// Validate the OTP code length matches our expected length
|
187
|
+
if (otpCode.length === codeLength) {
|
188
|
+
newCode = (0, _utils.getCodeValue)(codeLength, otpCode);
|
189
|
+
codeSetter(newCode);
|
190
|
+
// Trigger full code enter callback
|
191
|
+
if (onFullCodeEnter) {
|
192
|
+
onFullCodeEnter(otpCode);
|
193
|
+
}
|
194
|
+
}
|
195
|
+
}
|
196
|
+
return [
|
197
|
+
3,
|
198
|
+
4
|
199
|
+
];
|
200
|
+
case 3:
|
201
|
+
err = _state.sent();
|
202
|
+
if (_instanceof(err, DOMException)) {
|
203
|
+
if (err.name !== 'AbortError' && err.name !== 'NotAllowedError') {
|
204
|
+
console.warn('Web OTP API error:', err);
|
205
|
+
}
|
206
|
+
} else if (_instanceof(err, Error)) {
|
207
|
+
console.warn('Web OTP API error:', err);
|
208
|
+
} else {
|
209
|
+
console.warn('Unknown Web OTP API error:', err);
|
210
|
+
}
|
211
|
+
return [
|
212
|
+
3,
|
213
|
+
4
|
214
|
+
];
|
215
|
+
case 4:
|
216
|
+
return [
|
217
|
+
2
|
218
|
+
];
|
219
|
+
}
|
220
|
+
});
|
221
|
+
})();
|
222
|
+
}, [
|
223
|
+
enableSMSAutoRead,
|
224
|
+
disabled,
|
225
|
+
codeLength,
|
226
|
+
onFullCodeEnter
|
227
|
+
]);
|
228
|
+
var stopWebOTPListener = (0, _react.useCallback)(function() {
|
229
|
+
if (abortControllerRef.current) {
|
230
|
+
abortControllerRef.current.abort();
|
231
|
+
abortControllerRef.current = null;
|
232
|
+
}
|
233
|
+
}, []);
|
234
|
+
(0, _react.useEffect)(function() {
|
235
|
+
if ((0, _utils.isWebOTPSupported)() && codeString === '' && enableSMSAutoRead && !disabled) {
|
236
|
+
startWebOTPListener();
|
237
|
+
} else {
|
238
|
+
stopWebOTPListener();
|
239
|
+
}
|
240
|
+
return function() {
|
241
|
+
stopWebOTPListener();
|
242
|
+
};
|
243
|
+
}, [
|
244
|
+
codeString,
|
245
|
+
enableSMSAutoRead,
|
246
|
+
disabled,
|
247
|
+
startWebOTPListener,
|
248
|
+
stopWebOTPListener
|
249
|
+
]);
|
250
|
+
return {
|
251
|
+
startWebOTPListener: startWebOTPListener,
|
252
|
+
stopWebOTPListener: stopWebOTPListener
|
253
|
+
};
|
254
|
+
};
|
@@ -37,7 +37,7 @@ function _unsupported_iterable_to_array(o, minLen) {
|
|
37
37
|
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
|
38
38
|
}
|
39
39
|
var handleCodeError = function(param) {
|
40
|
-
var codeLength = param.codeLength, codeErrorBehavior = param.codeErrorBehavior, currentCode = param.currentCode, inputRefs = param.inputRefs, inputContainerRef = param.inputContainerRef, captionRef = param.captionRef,
|
40
|
+
var codeLength = param.codeLength, codeErrorBehavior = param.codeErrorBehavior, currentCode = param.currentCode, inputRefs = param.inputRefs, inputContainerRef = param.inputContainerRef, captionRef = param.captionRef, setInnerValue = param.setInnerValue, codeSetter = param.codeSetter;
|
41
41
|
if (!inputContainerRef.current) {
|
42
42
|
return;
|
43
43
|
}
|
@@ -51,7 +51,7 @@ var handleCodeError = function(param) {
|
|
51
51
|
var newCode = _to_consumable_array(currentCode);
|
52
52
|
newCode.pop();
|
53
53
|
codeSetter(newCode);
|
54
|
-
|
54
|
+
setInnerValue(currentCode);
|
55
55
|
(_inputContainerRef_current = inputContainerRef.current) === null || _inputContainerRef_current === void 0 ? void 0 : _inputContainerRef_current.classList.remove(_CodeFieldtokens.classes.codeErrorAnimation);
|
56
56
|
setTimeout(function() {
|
57
57
|
var _inputRefs_current_, _inputRefs_current_1;
|
@@ -36,14 +36,17 @@ function _unsupported_iterable_to_array(o, minLen) {
|
|
36
36
|
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
|
37
37
|
}
|
38
38
|
var handleItemError = function(param) {
|
39
|
-
var currentSymbol = param.currentSymbol, itemErrorBehavior = param.itemErrorBehavior, index = param.index, newCode = param.newCode, inputRefs = param.inputRefs,
|
39
|
+
var currentSymbol = param.currentSymbol, itemErrorBehavior = param.itemErrorBehavior, index = param.index, newCode = param.newCode, inputRefs = param.inputRefs, setInnerValue = param.setInnerValue, codeSetter = param.codeSetter, onChange = param.onChange;
|
40
40
|
if (!inputRefs.current[index] || currentSymbol === ' ') {
|
41
41
|
return;
|
42
42
|
}
|
43
43
|
switch(itemErrorBehavior){
|
44
44
|
case 'keep':
|
45
45
|
var _inputRefs_current_index;
|
46
|
-
|
46
|
+
setInnerValue(newCode);
|
47
|
+
if (onChange) {
|
48
|
+
onChange(newCode.join(''));
|
49
|
+
}
|
47
50
|
(_inputRefs_current_index = inputRefs.current[index]) === null || _inputRefs_current_index === void 0 ? void 0 : _inputRefs_current_index.classList.add(_CodeFieldtokens.classes.itemError, _CodeFieldtokens.classes.itemErrorAnimation);
|
48
51
|
setTimeout(function() {
|
49
52
|
var _inputRefs_current_index;
|
@@ -57,11 +60,17 @@ var handleItemError = function(param) {
|
|
57
60
|
case 'forbid-enter':
|
58
61
|
newCode[index] = '';
|
59
62
|
codeSetter(newCode);
|
63
|
+
if (onChange) {
|
64
|
+
onChange(newCode.join(''));
|
65
|
+
}
|
60
66
|
break;
|
61
67
|
case 'remove-symbol':
|
62
68
|
default:
|
63
69
|
var _inputRefs_current_index1;
|
64
|
-
|
70
|
+
setInnerValue(newCode);
|
71
|
+
if (onChange) {
|
72
|
+
onChange(newCode.join(''));
|
73
|
+
}
|
65
74
|
(_inputRefs_current_index1 = inputRefs.current[index]) === null || _inputRefs_current_index1 === void 0 ? void 0 : _inputRefs_current_index1.classList.add(_CodeFieldtokens.classes.itemError, _CodeFieldtokens.classes.itemErrorFade, _CodeFieldtokens.classes.itemErrorAnimation);
|
66
75
|
setTimeout(function() {
|
67
76
|
var _inputRefs_current_index;
|
@@ -23,8 +23,12 @@ _export(exports, {
|
|
23
23
|
},
|
24
24
|
get handleItemError () {
|
25
25
|
return _handleItemError.handleItemError;
|
26
|
+
},
|
27
|
+
get isWebOTPSupported () {
|
28
|
+
return _isWebOTPSupported.isWebOTPSupported;
|
26
29
|
}
|
27
30
|
});
|
28
31
|
var _initialValuesHelper = require("./initialValuesHelper");
|
29
32
|
var _handleItemError = require("./handleItemError");
|
30
33
|
var _handleCodeError = require("./handleCodeError");
|
34
|
+
var _isWebOTPSupported = require("./isWebOTPSupported");
|
@@ -0,0 +1,13 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
3
|
+
value: true
|
4
|
+
});
|
5
|
+
Object.defineProperty(exports, "isWebOTPSupported", {
|
6
|
+
enumerable: true,
|
7
|
+
get: function() {
|
8
|
+
return isWebOTPSupported;
|
9
|
+
}
|
10
|
+
});
|
11
|
+
var isWebOTPSupported = function() {
|
12
|
+
return typeof window !== 'undefined' && 'OTPCredential' in window && navigator.credentials && typeof navigator.credentials.get === 'function';
|
13
|
+
};
|
@@ -97,17 +97,6 @@ var useDrawer = function(param) {
|
|
97
97
|
isOpen,
|
98
98
|
popupController.items
|
99
99
|
]);
|
100
|
-
// При анмаунте компонента нужно обновлять overflow у body.
|
101
|
-
(0, _react.useEffect)(function() {
|
102
|
-
return function() {
|
103
|
-
if (!_utils.canUseDOM) {
|
104
|
-
return;
|
105
|
-
}
|
106
|
-
if (!(0, _DrawerContext.hasDrawers)(Array.from(popupController.items.values()))) {
|
107
|
-
document.body.style.overflow = overflow.current;
|
108
|
-
}
|
109
|
-
};
|
110
|
-
}, []);
|
111
100
|
var drawerInfo = _object_spread({
|
112
101
|
id: id,
|
113
102
|
info: {
|
@@ -117,13 +117,14 @@ import { useDidMountEffect } from "../../hooks";
|
|
117
117
|
import { getSizeValueFromProp } from "../../utils";
|
118
118
|
import { useCodeHook } from "../../hooks";
|
119
119
|
import { BACKSPACE_KEY, FORBIDDEN_KEYS, ONLY_DIGITS_PATTERN } from "./utils/constants";
|
120
|
-
import { getCodeValue, getFieldPattern, getPlaceholderValue, handleCodeError, handleItemError } from "./utils";
|
120
|
+
import { getCodeValue, getFieldPattern, getPlaceholderValue, handleCodeError, handleItemError, isWebOTPSupported } from "./utils";
|
121
121
|
import { classes } from "./CodeField.tokens";
|
122
|
-
import { base, CaptionWrapper, CodeGroup, CodeWrapper, ItemInput, Separator } from "./CodeField.styles";
|
122
|
+
import { base, CaptionWrapper, CodeGroup, CodeWrapper, HiddenInput, ItemInput, Separator } from "./CodeField.styles";
|
123
123
|
import { base as viewCSS } from "./variations/_view/base";
|
124
124
|
import { base as sizeCSS } from "./variations/_size/base";
|
125
125
|
import { base as shapeCSS } from "./variations/_shape/base";
|
126
126
|
import { base as disabledCSS } from "./variations/_disabled/base";
|
127
|
+
import { useWebOTP } from "./hooks/useWebOTP";
|
127
128
|
export var codeFieldRoot = function(Root) {
|
128
129
|
return /*#__PURE__*/ forwardRef(function(_param, ref) {
|
129
130
|
var className = _param.className, outerValue = _param.value, placeholder = _param.placeholder, autoFocus = _param.autoFocus, _param_codeLength = _param.codeLength, codeLength = _param_codeLength === void 0 ? 6 : _param_codeLength, caption = _param.caption, _param_captionAlign = _param.captionAlign, captionAlign = _param_captionAlign === void 0 ? 'left' : _param_captionAlign, width = _param.width, _param_shape = _param.shape, shape = _param_shape === void 0 ? 'default' : _param_shape, view = _param.view, size = _param.size, disabled = _param.disabled, isError = _param.isError, _param_allowedSymbols = _param.allowedSymbols, allowedSymbols = _param_allowedSymbols === void 0 ? ONLY_DIGITS_PATTERN : _param_allowedSymbols, _param_itemErrorBehavior = _param.itemErrorBehavior, itemErrorBehavior = _param_itemErrorBehavior === void 0 ? 'remove-symbol' : _param_itemErrorBehavior, _param_codeErrorBehavior = _param.codeErrorBehavior, codeErrorBehavior = _param_codeErrorBehavior === void 0 ? 'remove-code' : _param_codeErrorBehavior, _param_autoComplete = _param.autoComplete, autoComplete = _param_autoComplete === void 0 ? 'one-time-code' : _param_autoComplete, setIsError = _param.setIsError, onChange = _param.onChange, onFullCodeEnter = _param.onFullCodeEnter, rest = _object_without_properties(_param, [
|
@@ -148,7 +149,8 @@ export var codeFieldRoot = function(Root) {
|
|
148
149
|
"onChange",
|
149
150
|
"onFullCodeEnter"
|
150
151
|
]);
|
151
|
-
var _useState = _sliced_to_array(useState(getCodeValue(codeLength,
|
152
|
+
var _useState = _sliced_to_array(useState(getCodeValue(codeLength, '')), 2), innerValue = _useState[0], setInnerValue = _useState[1];
|
153
|
+
var code = (outerValue === null || outerValue === void 0 ? void 0 : outerValue.length) ? getCodeValue(codeLength, outerValue) : innerValue;
|
152
154
|
var _useState1 = _sliced_to_array(useState(code.join('')), 2), originalValue = _useState1[0], setOriginalValue = _useState1[1];
|
153
155
|
var inputRefs = useRef([]);
|
154
156
|
var inputContainerRef = useRef(null);
|
@@ -157,6 +159,7 @@ export var codeFieldRoot = function(Root) {
|
|
157
159
|
var placeholderValue = getPlaceholderValue(codeLength, placeholder);
|
158
160
|
var parts = codeLength === 6 ? 2 : 1;
|
159
161
|
var widthValue = width ? getSizeValueFromProp(width, 'rem') : undefined;
|
162
|
+
var isWebOTPEnabled = autoComplete === 'one-time-code' && !disabled && !isWebOTPSupported();
|
160
163
|
var getLastActiveIndex = function() {
|
161
164
|
if (code.length && code.length < codeLength) {
|
162
165
|
return code.length;
|
@@ -166,6 +169,25 @@ export var codeFieldRoot = function(Root) {
|
|
166
169
|
});
|
167
170
|
return lastEmptyIndex >= 0 ? lastEmptyIndex : codeLength - 1;
|
168
171
|
};
|
172
|
+
var codeSetter = function(newCode) {
|
173
|
+
setInnerValue(newCode);
|
174
|
+
var originalCode = newCode.join('');
|
175
|
+
setOriginalValue(originalCode);
|
176
|
+
if (onChange) {
|
177
|
+
onChange(originalCode);
|
178
|
+
}
|
179
|
+
if (originalCode.length > 0) {
|
180
|
+
stopWebOTPListener();
|
181
|
+
}
|
182
|
+
};
|
183
|
+
var _useWebOTP = useWebOTP({
|
184
|
+
codeString: originalValue,
|
185
|
+
enableSMSAutoRead: autoComplete === 'one-time-code',
|
186
|
+
disabled: Boolean(disabled),
|
187
|
+
codeLength: codeLength,
|
188
|
+
codeSetter: codeSetter,
|
189
|
+
onFullCodeEnter: onFullCodeEnter
|
190
|
+
}), startWebOTPListener = _useWebOTP.startWebOTPListener, stopWebOTPListener = _useWebOTP.stopWebOTPListener;
|
169
191
|
var handleClick = function() {
|
170
192
|
if (disabled) {
|
171
193
|
return;
|
@@ -185,19 +207,24 @@ export var codeFieldRoot = function(Root) {
|
|
185
207
|
return;
|
186
208
|
}
|
187
209
|
if (key === BACKSPACE_KEY) {
|
188
|
-
if (index > 0
|
189
|
-
var
|
190
|
-
|
210
|
+
if (index > 0) {
|
211
|
+
var _inputRefs_current_index;
|
212
|
+
var newCode = _to_consumable_array(code);
|
213
|
+
newCode[index] = '';
|
214
|
+
if (index >= codeLength - 1 && code[index]) {
|
215
|
+
codeSetter(newCode);
|
216
|
+
return;
|
217
|
+
}
|
218
|
+
if (!code[index]) {
|
219
|
+
var _inputRefs_current_;
|
220
|
+
newCode[index - 1] = '';
|
221
|
+
(_inputRefs_current_ = inputRefs.current[index - 1]) === null || _inputRefs_current_ === void 0 ? void 0 : _inputRefs_current_.focus();
|
222
|
+
}
|
223
|
+
(_inputRefs_current_index = inputRefs.current[index]) === null || _inputRefs_current_index === void 0 ? void 0 : _inputRefs_current_index.classList.remove(classes.itemError);
|
224
|
+
codeSetter(newCode);
|
191
225
|
}
|
192
226
|
}
|
193
227
|
};
|
194
|
-
var codeSetter = function(newCode) {
|
195
|
-
setCode(newCode);
|
196
|
-
setOriginalValue(newCode.join(''));
|
197
|
-
if (onChange) {
|
198
|
-
onChange(newCode.join(''));
|
199
|
-
}
|
200
|
-
};
|
201
228
|
var handleChange = function(event, index) {
|
202
229
|
var _inputRefs_current_index;
|
203
230
|
if (disabled) {
|
@@ -228,8 +255,6 @@ export var codeFieldRoot = function(Root) {
|
|
228
255
|
return;
|
229
256
|
}
|
230
257
|
if (!symbol) {
|
231
|
-
newCode[index] = '';
|
232
|
-
codeSetter(newCode);
|
233
258
|
return;
|
234
259
|
}
|
235
260
|
if (fieldPattern.test(symbol)) {
|
@@ -247,8 +272,9 @@ export var codeFieldRoot = function(Root) {
|
|
247
272
|
index: index,
|
248
273
|
newCode: newCode,
|
249
274
|
inputRefs: inputRefs,
|
250
|
-
|
251
|
-
codeSetter: codeSetter
|
275
|
+
setInnerValue: setInnerValue,
|
276
|
+
codeSetter: codeSetter,
|
277
|
+
onChange: onChange
|
252
278
|
});
|
253
279
|
}
|
254
280
|
};
|
@@ -275,6 +301,7 @@ export var codeFieldRoot = function(Root) {
|
|
275
301
|
if (onFullCodeEnter) {
|
276
302
|
onFullCodeEnter(fullCode);
|
277
303
|
}
|
304
|
+
startWebOTPListener();
|
278
305
|
}, []);
|
279
306
|
useCodeHook({
|
280
307
|
inputRefs: inputRefs,
|
@@ -294,7 +321,7 @@ export var codeFieldRoot = function(Root) {
|
|
294
321
|
inputRefs: inputRefs,
|
295
322
|
inputContainerRef: inputContainerRef,
|
296
323
|
captionRef: captionRef,
|
297
|
-
|
324
|
+
setInnerValue: setInnerValue,
|
298
325
|
codeSetter: codeSetter
|
299
326
|
});
|
300
327
|
}
|
@@ -309,7 +336,7 @@ export var codeFieldRoot = function(Root) {
|
|
309
336
|
disabled: disabled,
|
310
337
|
onClick: handleClick,
|
311
338
|
className: cls(className, _define_property({}, classes.captionAlignLeft, captionAlign === 'left'))
|
312
|
-
}, rest), /*#__PURE__*/ React.createElement(CodeWrapper, {
|
339
|
+
}, !isWebOTPEnabled && _object_spread({}, rest)), /*#__PURE__*/ React.createElement(CodeWrapper, {
|
313
340
|
ref: inputContainerRef
|
314
341
|
}, _to_consumable_array(Array(parts)).map(function(_, partIndex) {
|
315
342
|
return /*#__PURE__*/ React.createElement(Fragment, {
|
@@ -326,7 +353,6 @@ export var codeFieldRoot = function(Root) {
|
|
326
353
|
},
|
327
354
|
className: cls((_obj = {}, _define_property(_obj, classes.segmented, shape === 'segmented'), _define_property(_obj, classes.hoverEnabled, !disabled && inputCorrectIndex >= originalValue.length), _obj)),
|
328
355
|
value: code[inputCorrectIndex] || '',
|
329
|
-
autoComplete: autoComplete,
|
330
356
|
onChange: function(e) {
|
331
357
|
handleChange(e, inputCorrectIndex);
|
332
358
|
},
|
@@ -343,7 +369,9 @@ export var codeFieldRoot = function(Root) {
|
|
343
369
|
ref: captionRef,
|
344
370
|
captionAlign: captionAlign,
|
345
371
|
widthValue: widthValue
|
346
|
-
}, caption)
|
372
|
+
}, caption), isWebOTPEnabled && /*#__PURE__*/ React.createElement(HiddenInput, _object_spread({
|
373
|
+
tabIndex: -1
|
374
|
+
}, rest)));
|
347
375
|
});
|
348
376
|
};
|
349
377
|
export var codeFieldConfig = {
|
@@ -7,32 +7,32 @@ export var base = css([
|
|
7
7
|
], classes.captionAlignLeft);
|
8
8
|
export var CodeWrapper = styled.div.withConfig({
|
9
9
|
displayName: "CodeField.styles__CodeWrapper",
|
10
|
-
componentId: "sc-
|
10
|
+
componentId: "sc-ff3d9c22-0"
|
11
11
|
})([
|
12
12
|
"display:flex;align-items:center;"
|
13
13
|
]);
|
14
14
|
export var CodeGroup = styled.div.withConfig({
|
15
15
|
displayName: "CodeField.styles__CodeGroup",
|
16
|
-
componentId: "sc-
|
16
|
+
componentId: "sc-ff3d9c22-1"
|
17
17
|
})([
|
18
18
|
"display:flex;align-items:center;"
|
19
19
|
]);
|
20
20
|
export var Separator = styled.div.withConfig({
|
21
21
|
displayName: "CodeField.styles__Separator",
|
22
|
-
componentId: "sc-
|
22
|
+
componentId: "sc-ff3d9c22-2"
|
23
23
|
})([
|
24
24
|
"width:var(",
|
25
25
|
");"
|
26
26
|
], tokens.separatorWidth);
|
27
27
|
export var ItemInput = styled.input.withConfig({
|
28
28
|
displayName: "CodeField.styles__ItemInput",
|
29
|
-
componentId: "sc-
|
29
|
+
componentId: "sc-ff3d9c22-3"
|
30
30
|
})([
|
31
31
|
"box-sizing:border-box;appearance:none;border:0;padding:0;outline:none;pointer-events:none;text-align:center;"
|
32
32
|
]);
|
33
33
|
export var CaptionWrapper = styled.div.withConfig({
|
34
34
|
displayName: "CodeField.styles__CaptionWrapper",
|
35
|
-
componentId: "sc-
|
35
|
+
componentId: "sc-ff3d9c22-4"
|
36
36
|
})([
|
37
37
|
"text-align:",
|
38
38
|
";align-self:",
|
@@ -48,3 +48,9 @@ export var CaptionWrapper = styled.div.withConfig({
|
|
48
48
|
var widthValue = param.widthValue;
|
49
49
|
return widthValue || 'auto';
|
50
50
|
});
|
51
|
+
export var HiddenInput = styled.input.withConfig({
|
52
|
+
displayName: "CodeField.styles__HiddenInput",
|
53
|
+
componentId: "sc-ff3d9c22-5"
|
54
|
+
})([
|
55
|
+
"display:none;"
|
56
|
+
]);
|