@julo-ui/otp-input
Version:
React Input Component for entering sequences of digits
197 lines (190 loc) • 6.05 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/index.ts
var usecase_exports = {};
__export(usecase_exports, {
useHandleFocus: () => useHandleFocus,
useHandleInputEvent: () => useHandleInputEvent,
useHandleValues: () => useHandleValues
});
module.exports = __toCommonJS(usecase_exports);
// src/usecase/use-handle-focus.ts
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
};
}
// src/usecase/use-handle-values.ts
var import_react2 = require("react");
var import_function_utils = require("@julo-ui/function-utils");
var import_use_controllable_state = require("@julo-ui/use-controllable-state");
// src/utils.ts
var strToArray = (value) => value == null ? void 0 : value.split("");
var validateValue = (value, type) => {
const NUMERIC_REGEX = /^[0-9]+$/;
const ALPHA_NUMERIC_REGEX = /^[a-zA-Z0-9]+$/i;
const regex = type === "alphanumeric" ? ALPHA_NUMERIC_REGEX : NUMERIC_REGEX;
return regex.test(value);
};
// src/usecase/use-handle-values.ts
function useHandleValues(options) {
const {
onChange,
defaultValue,
value,
descendants,
onComplete,
onFocusNext
} = options;
const [values, setValues] = (0, import_use_controllable_state.useControllableState)({
defaultValue: strToArray(defaultValue) || [],
value: strToArray(value),
onChange: (values2) => onChange(values2.join(""))
});
const setValue = (0, import_react2.useCallback)(
(value2, index, handleFocus = true) => {
const nextValues = [...values];
nextValues[index] = value2;
setValues(nextValues);
const isComplete = value2 !== "" && nextValues.length === descendants.count() && nextValues.every(
(inputValue) => !(0, import_function_utils.isTrullyEmpty)(inputValue) && inputValue !== ""
);
if (!isComplete && handleFocus)
return onFocusNext(index);
onComplete(nextValues.join(""));
},
[descendants, onComplete, onFocusNext, setValues, values]
);
const clearValue = (0, import_react2.useCallback)(() => {
const values2 = Array(descendants.count()).fill("");
setValues(values2);
onFocusNext(0);
}, [descendants, onFocusNext, setValues]);
const getNextValue = (0, import_react2.useCallback)((value2, eventValue) => {
if (!value2 || (value2 == null ? void 0 : value2.length) === 0)
return eventValue;
if (value2[0] === eventValue.charAt(0))
return eventValue.charAt(1);
return eventValue.charAt(0);
}, []);
return { values, setValues, setValue, clearValue, getNextValue };
}
// src/usecase/use-handle-input-event.ts
var import_react3 = require("react");
function useHandleInputEvent(options) {
const {
values,
getNextValue,
setValue,
type,
descendants,
setValues,
onComplete,
manageFocus,
onFocusNext
} = options;
const onChange = (0, import_react3.useCallback)(
(event, index) => {
const eventValue = event.target.value;
const currentValue = values[index];
const nextValue = getNextValue(currentValue, eventValue);
if (nextValue === "")
return setValue("", index);
if (eventValue.length > 2) {
if (!validateValue(eventValue, type))
return;
const nextValue2 = eventValue.split("").filter((_, index2) => index2 < descendants.count());
setValues(nextValue2);
onFocusNext(eventValue.length - 2);
if (nextValue2.length === descendants.count()) {
onComplete(nextValue2.join(""));
}
return;
}
if (validateValue(nextValue, type)) {
setValue(nextValue, index);
}
},
[
descendants,
getNextValue,
onComplete,
onFocusNext,
setValue,
setValues,
type,
values
]
);
const onKeyDown = (0, import_react3.useCallback)(
(event, index) => {
if (event.key !== "Backspace" || !manageFocus)
return;
if (event.target.value !== "")
return;
const prevInput = descendants.prev(index, false);
if (!prevInput)
return;
setValue("", index - 1, false);
prevInput.node.focus();
},
[descendants, manageFocus, setValue]
);
return { onChange, onKeyDown };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
useHandleFocus,
useHandleInputEvent,
useHandleValues
});