UNPKG

react-dynamic-otp-inputs

Version:

react OTP input

108 lines (97 loc) 4.63 kB
'use strict'; var jsxRuntime = require('react/jsx-runtime'); var React = require('react'); /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */ var __assign = function() { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; function __spreadArray(to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); } typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; var OtpInput = function (_a) { var _b = _a.numberOfInputFields, numberOfInputFields = _b === void 0 ? 4 : _b, inputBoxStyle = _a.inputBoxStyle, _c = _a.onchange, onchange = _c === void 0 ? function () { } : _c; var firstInputRef = React.useRef(null); var initialState = Array.from({ length: numberOfInputFields }, function (_, index) { return ({ value: '', ref: index === 0 ? firstInputRef : React.createRef(), }); }); var reducer = function (state, action) { var _a; switch (action.type) { case 'onchange': { var newState = __spreadArray([], state, true); newState[action.payload.index].value = action.payload.value; if (action.payload.value && action.payload.index < numberOfInputFields - 1) { (_a = newState[action.payload.index + 1].ref.current) === null || _a === void 0 ? void 0 : _a.focus(); } return newState; } default: return state; } }; var _d = React.useReducer(reducer, initialState), state = _d[0], dispatch = _d[1]; var InputBoxStyle = { width: '50px', height: '50px', marginRight: '10px', borderRadius: '4px', border: '1px solid #ccc', textAlign: 'center', fontSize: '24px', color: 'black', boxShadow: '0 0 3px #00f, 0 0 3px #00f, 0 0 3px #00f', animation: 'glow 1.5s infinite alternate', outline: 'none', }; var inputHandler = function (e, index) { dispatch({ type: 'onchange', payload: { value: e.target.value, index: index } }); onchange(e, index); }; var keyDownHandler = function (e, index) { var _a; if (e.key === 'Backspace' && !state[index].value && index > 0) { (_a = state[index - 1].ref.current) === null || _a === void 0 ? void 0 : _a.focus(); } if (!/^[0-9]$/.test(e.key) && e.key !== 'Backspace' && e.key !== 'Delete' && e.key !== 'ArrowLeft' && e.key !== 'ArrowRight' && e.key !== 'Tab') { e.preventDefault(); } }; return (jsxRuntime.jsx("div", { children: state === null || state === void 0 ? void 0 : state.map(function (input, index) { return (jsxRuntime.jsx("input", { ref: input.ref, maxLength: 1, style: __assign(__assign({}, InputBoxStyle), inputBoxStyle), type: "text", value: input.value, onChange: function (e) { return inputHandler(e, index); }, onKeyDown: function (e) { return keyDownHandler(e, index); } }, index)); }) })); }; module.exports = OtpInput; //# sourceMappingURL=index.js.map