otp-gen-ui
Version:
Generate OTP and render customizable input boxes for React
114 lines (110 loc) • 4.08 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
OtpBox: () => OtpBox_default,
generateOtp: () => generateOtp
});
module.exports = __toCommonJS(index_exports);
// src/generateOtp.ts
var generateOtp = (length = 6) => {
if (length <= 0 || !Number.isInteger(length)) {
throw new Error("OTP length must be a positive integer");
}
return Array.from({ length }, () => Math.floor(Math.random() * 10)).join("");
};
// src/OtpBox.tsx
var import_react = __toESM(require("react"));
var import_jsx_runtime = require("react/jsx-runtime");
var OtpBox = ({
length = 6,
onChange,
style,
inputStyle
}) => {
const [otp, setOtp] = import_react.default.useState(Array(length).fill(""));
const inputsRef = (0, import_react.useRef)([]);
const handleChange = (value, index) => {
var _a;
const newOtp = [...otp];
newOtp[index] = value.slice(-1);
setOtp(newOtp);
onChange == null ? void 0 : onChange(newOtp.join(""));
if (value && index < length - 1) {
(_a = inputsRef.current[index + 1]) == null ? void 0 : _a.focus();
}
};
const handleKeyDown = (e, index) => {
var _a;
if (e.key === "Backspace" && !otp[index] && index > 0) {
(_a = inputsRef.current[index - 1]) == null ? void 0 : _a.focus();
}
};
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: __spreadValues({ display: "flex", gap: "8px" }, style), children: Array.from({ length }).map((_, i) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
"input",
{
ref: (el) => {
inputsRef.current[i] = el;
},
value: otp[i],
onChange: (e) => handleChange(e.target.value, i),
onKeyDown: (e) => handleKeyDown(e, i),
maxLength: 1,
style: __spreadValues({
width: "40px",
height: "40px",
textAlign: "center",
fontSize: "18px",
border: "1px solid #ccc",
borderRadius: "5px"
}, inputStyle)
},
i
)) });
};
var OtpBox_default = OtpBox;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
OtpBox,
generateOtp
});