@konstructio/ui
Version:
A set of reusable and customizable React components built for konstruct.io
47 lines (46 loc) • 1.27 kB
JavaScript
import { jsx as S } from "react/jsx-runtime";
import { useState as i, useMemo as T, useCallback as c, useEffect as d } from "react";
import { getFormattedTime as P } from "../utils/index.js";
import { TimePickerContext as p } from "./time-picker.context.js";
const x = ({ children: m, time: u, format: a }) => {
const [n, l] = i(a), [t, r] = i(() => u ?? /* @__PURE__ */ new Date()), s = T(() => t.getHours() < 12, [t]), f = c(
(e) => {
const o = new Date(t);
n === "12" ? e === 12 ? o.setHours(s ? 0 : 12) : o.setHours(s ? e : e + 12) : o.setHours(e), r(o);
},
[n, s, t]
), H = c(
(e) => {
const o = new Date(t);
o.setMinutes(e), r(o);
},
[t]
), w = c(() => {
const e = new Date(t), o = e.getHours();
s || e.setHours(o - 12), r(e);
}, [s, t]), M = c(() => {
const e = new Date(t), o = e.getHours();
s && e.setHours(o + 12), r(e);
}, [s, t]);
return d(() => {
l(n);
}, [n]), /* @__PURE__ */ S(
p.Provider,
{
value: {
time: t,
format: n,
formattedTime: P(t, n),
isAM: s,
onSelectHour: f,
onSelectMinute: H,
onSelectAM: w,
onSelectPM: M
},
children: m
}
);
};
export {
x as TimePickerProvider
};