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