@julo-ui/otp-input
Version:
React Input Component for entering sequences of digits
69 lines (67 loc) • 2.12 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/usecase/use-handle-focus.ts
var use_handle_focus_exports = {};
__export(use_handle_focus_exports, {
useHandleFocus: () => useHandleFocus
});
module.exports = __toCommonJS(use_handle_focus_exports);
var import_react = require("react");
function useHandleFocus(options) {
const { autoFocus, manageFocus, descendants } = options;
const [focusedIndex, setFocusedIndex] = (0, import_react.useState)(-1);
const onFocusNext = (0, import_react.useCallback)(
(index) => {
if (!manageFocus)
return;
const next = descendants.next(index, false);
if (next)
requestAnimationFrame(() => {
next.node.focus();
});
},
[descendants, manageFocus]
);
const onFocus = (0, import_react.useCallback)((index) => {
setFocusedIndex(index);
}, []);
const onBlur = (0, import_react.useCallback)(() => {
setFocusedIndex(-1);
}, []);
(0, import_react.useEffect)(() => {
if (autoFocus) {
const first = descendants.first();
if (first) {
requestAnimationFrame(() => {
first.node.focus();
});
}
}
}, [descendants]);
return {
focusedIndex,
onFocusNext,
onFocus,
onBlur
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
useHandleFocus
});