fui-fancyui
Version:
FancyUI Libary
36 lines (35 loc) • 1.35 kB
JavaScript
import { useState as y, useEffect as N } from "react";
const A = (w) => {
var m;
const { value: o, onChange: c, onKeyDown: p, min: i, max: s, decimalPlaces: V, step: r = 1 } = w, [g, d] = y(o ? o.toString() : null), u = V || ((m = r.toString().split(".")[1]) == null ? void 0 : m.length) || 0;
N(() => {
o !== void 0 && d(o.toString());
}, [o]);
const f = (e, t) => {
let n = e;
s !== void 0 && Number(e) > Number(s) ? n = s.toString() : i !== void 0 && Number(e) < Number(i) && (n = i.toString()), d(n), c && t && (t.target.value = n, c(t));
};
return { inputValue: g, handleChange: (e) => {
var h;
const a = e.target.value.split(".").slice(0, 2).join(".").replace(/[^\d.]/g, ""), l = a.split(".");
((h = l[1]) == null ? void 0 : h.length) > u && (l[1] = l[1].slice(0, u));
const v = Array.isArray(l) && l.length === 2 ? l.join(".") : a;
f(v, e);
}, handleKeyDown: (e) => {
if (e.key === "ArrowUp" || e.key === "ArrowDown") {
e.preventDefault();
let t = parseFloat(g || "0");
isNaN(t) && (t = 0);
let n = e.key === "ArrowUp" ? t + r : t - r;
n = Number(n.toFixed(u));
const a = {
...e
// eslint-disable-next-line @typescript-eslint/no-explicit-any
};
f(n.toString(), a), p && p(e);
}
} };
};
export {
A as useNumberInput
};