@tuv-indo/hooks
Version:
Usefull react hooks for Tuv App
234 lines (233 loc) • 5.19 kB
JavaScript
import E, { useLayoutEffect as i, useEffect as w, useState as h, useCallback as l, useRef as J, useMemo as j, useSyncExternalStore as R } from "react";
function M(e = !1) {
if (typeof e != "boolean")
throw new Error("defaultValue must be `true` or `false`");
const [n, r] = E.useState(e), s = E.useCallback(() => {
r(!0);
}, []), c = E.useCallback(() => {
r(!1);
}, []), o = E.useCallback(() => {
r((t) => !t);
}, []);
return { value: n, setValue: r, setTrue: s, setFalse: c, toggle: o };
}
const F = typeof window < "u" ? i : w;
function D(e) {
const [n, r] = h({
show: !1
}), s = (u) => {
r({ data: u, show: !0 });
}, c = () => {
r({ show: !1 });
}, o = l(() => {
if (e != null && e.onCancelAsync) {
e.onCancelAsync().then(() => {
r({ show: !1 });
}).catch((u) => {
e.onError && e.onError(u);
});
return;
}
e != null && e.onCancel && e.onCancel(), r({ show: !1 });
}, [e]), t = l(() => {
if (e != null && e.onConfirmAsync) {
e.onConfirmAsync(n.data).then(() => {
r({ show: !1 });
}).catch((u) => {
e.onError && e.onError(u);
});
return;
}
e != null && e.onConfirm && e.onConfirm(n.data), r({ show: !1 });
}, [e, n.data]);
return {
...n,
open: s,
close: c,
save: (u) => {
r((d) => ({ ...d, data: u }));
},
confirm: t,
cancel: o
};
}
function N(e) {
const [n, r] = h(e ?? 0), s = l(() => {
r((t) => t + 1);
}, []), c = l(() => {
r((t) => t - 1);
}, []), o = l(() => {
r(e ?? 0);
}, [e]);
return {
count: n,
increment: s,
decrement: c,
reset: o,
setCount: r
};
}
function U(e, n) {
const r = J(e);
F(() => {
r.current = e;
}, [e]), w(() => {
if (n === null)
return;
const s = setInterval(() => {
r.current();
}, n);
return () => {
clearInterval(s);
};
}, [n]);
}
function I({
countStart: e,
countStop: n = 0,
intervalMs: r = 1e3,
isIncrement: s = !1
}) {
const [c, o] = h(!1), {
count: t,
increment: m,
decrement: u,
reset: d,
setCount: g
} = N(e), {
value: v,
setTrue: S,
setFalse: a
} = M(!1), O = l(() => {
a(), d();
}, [a, d]), b = l(() => {
if (t === n) {
a();
return;
}
s ? m() : u();
}, [t, n, u, m, s, a]);
return w(() => {
o(!t);
}, [t]), U(b, v ? r : null), {
count: t,
startCountdown: S,
stopCountdown: a,
resetCountdown: O,
completed: c,
setCompleted: o,
setCount: g
};
}
function Q(e, n) {
const [r, s] = h(e);
return w(() => {
const c = setTimeout(() => {
s(e);
}, n);
return () => {
clearTimeout(c);
};
}, [e, n]), r;
}
var L = /* @__PURE__ */ ((e) => (e.sm = "640px", e.md = "768px", e.lg = "1024px", e.xl = "1280px", e["2xl"] = "1536px", e))(L || {});
function k(e) {
const n = L[e];
return n ? `(min-width: ${n})` : e;
}
function V(e) {
const n = j(() => k(e), [e]), r = l(
(o) => {
const t = window.matchMedia(n);
return t.addEventListener("change", o), () => {
t.removeEventListener("change", o);
};
},
[n]
);
return R(r, () => window.matchMedia(n).matches, () => {
throw Error("useMediaQuery is a client-only hook");
});
}
function q(e) {
const n = J(e);
n.current = e, w(
() => () => {
n.current();
},
[]
);
}
function A(e) {
const n = Object.keys(e), r = {};
return n.forEach((s) => {
r[s] = "";
}), r;
}
function $(e, n) {
const r = (n == null ? void 0 : n.validationState) ?? A(e), [s, c] = h(e), [o, t] = h(r), m = e && JSON.stringify(e), u = s && JSON.stringify(s), d = r && JSON.stringify(r), g = Object.values(o).every((f) => !f), v = (f, C) => {
c((x) => ({
...x,
[f]: C
}));
}, S = (f, C) => {
t((x) => ({
...x,
[f]: C
}));
}, a = l(
() => t(r),
[r]
), O = () => {
c(e), a();
}, b = l(() => {
if (n != null && n.validationCallback) {
const f = n.validationCallback(s, {
...r
});
return Object.values(f).every((C) => !C) ? (a(), !0) : (t(f), !1);
}
return a(), !0;
}, [s, r, n, a]), y = J();
return y.current = b, w(() => {
var f;
(f = y.current) == null || f.call(y);
}, [u, d]), w(() => {
m && (c(JSON.parse(m)), t(JSON.parse(d)));
}, [m, d]), {
values: s,
errors: o,
setValue: v,
setValues: c,
reset: O,
setErrors: t,
valid: g,
setError: S,
resetErrors: a,
validate: b
};
}
function z() {
return {
downloadFile: l(
(n, r, s) => {
const c = new Blob([n], { type: s }), o = window.URL.createObjectURL(c), t = document.createElement("a");
t.href = o, t.download = r, document.body.appendChild(t), t.click(), window.URL.revokeObjectURL(o), document.body.removeChild(t);
},
[]
)
};
}
export {
M as useBoolean,
D as useConfirmation,
I as useCountdown,
N as useCounter,
Q as useDebounce,
z as useDownload,
$ as useForm,
U as useInterval,
F as useIsomorphicLayoutEffect,
V as useMediaQuery,
q as useUnmount
};