sauron-verification-code
Version:
A description for the module
173 lines (147 loc) • 4.33 kB
JavaScript
/**
* Bundle of @sauron/verification-code
* Generated: 2019-10-31
* Version: 1.0.0
* License: MIT
* Author: sauron
*/
import React, { useRef, useState, useEffect, useCallback } from 'react';
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
function _iterableToArrayLimit(arr, i) {
if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) {
return;
}
var _arr = [];
var _n = true;
var _d = false;
var _e = undefined;
try {
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally {
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally {
if (_d) throw _e;
}
}
return _arr;
}
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
}
function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
}
var Input = function Input(_ref) {
var onChange = _ref.onChange,
isFocus = _ref.isFocus,
index = _ref.index;
var InputRef = useRef(null);
var _useState = useState(''),
_useState2 = _slicedToArray(_useState, 2),
oldVal = _useState2[0],
setOldVal = _useState2[1];
var setVal = function setVal(i, e) {
InputRef.current && InputRef.current.blur();
var eVal = e.target.value;
var value = e.target.value = eVal.replace(oldVal, '').replace(/[\W]/, '').substring(0, 1);
if (value) {
onChange({
type: 'next',
index: i,
value: value
});
}
setOldVal(value);
};
useEffect(function () {
if (isFocus) {
InputRef.current && InputRef.current.focus();
}
});
var keydown = function keydown(e) {
if (e.key !== 'Backspace') return;
onChange({
type: 'clear',
index: index
});
};
var setFocus = function setFocus(i, e) {
var value = e.target.value.replace(/[\W]/, '');
onChange({
type: 'focus',
index: i,
value: value
});
};
return React.createElement(React.Fragment, null, React.createElement("input", {
autoComplete: "new-password",
autoFocus: isFocus,
ref: InputRef,
className: "active-code-input",
type: "text",
onFocus: setFocus.bind(null, index),
onChange: setVal.bind(null, index),
onKeyDown: keydown,
onPaste: function onPaste(e) {
e.preventDefault();
}
}));
};
function getInitArr(len) {
var temp = [];
temp.length = len;
return temp.fill(' ').join('');
}
var VerifationCode = function VerifationCode(_ref) {
var onGetVerifationCode = _ref.onGetVerifationCode,
_ref$len = _ref.len,
len = _ref$len === void 0 ? 4 : _ref$len,
_ref$autoFocus = _ref.autoFocus,
autoFocus = _ref$autoFocus === void 0 ? true : _ref$autoFocus;
var initArr = getInitArr(len);
var _useState = useState(''),
_useState2 = _slicedToArray(_useState, 2),
verifationCode = _useState2[0],
setVerifationCode = _useState2[1];
var _useState3 = useState(autoFocus ? 0 : -1),
_useState4 = _slicedToArray(_useState3, 2),
currentIndex = _useState4[0],
setCurrentIndex = _useState4[1];
var setVal = useCallback(function (val) {
var index = val.index,
type = val.type,
value = val.value;
if (type === 'clear' && index !== 0) {
setCurrentIndex(index - 1);
setVerifationCode(verifationCode.substr(0, index));
} else if (type === 'focus') {
setCurrentIndex(index);
} else if (type === 'next') {
setCurrentIndex(index + 1);
setVerifationCode(verifationCode.substr(0, index) + value + verifationCode.substr(index + 1));
}
}, [verifationCode]);
useEffect(function () {
!!verifationCode && onGetVerifationCode(verifationCode);
}, [onGetVerifationCode, verifationCode]);
return React.createElement("div", {
className: "active-store"
}, initArr.split('').map(function (item, index) {
return React.createElement(Input, {
isFocus: currentIndex === index,
index: index,
key: index,
onChange: setVal
});
}));
};
export default VerifationCode;