UNPKG

@helpwave/hightide

Version:

helpwave's component and theming library

220 lines (216 loc) 8.05 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/components/date/TimePicker.tsx var TimePicker_exports = {}; __export(TimePicker_exports, { TimePicker: () => TimePicker, TimePickerUncontrolled: () => TimePickerUncontrolled }); module.exports = __toCommonJS(TimePicker_exports); var import_react = require("react"); var import_react_custom_scrollbars_2 = require("react-custom-scrollbars-2"); // src/util/noop.ts var noop = () => void 0; // src/util/array.ts var defaultRangeOptions = { allowEmptyRange: false, stepSize: 1, exclusiveStart: false, exclusiveEnd: true }; var range = (endOrRange, options) => { const { allowEmptyRange, stepSize, exclusiveStart, exclusiveEnd } = { ...defaultRangeOptions, ...options }; let start = 0; let end; if (typeof endOrRange === "number") { end = endOrRange; } else { start = endOrRange[0]; end = endOrRange[1]; } if (!exclusiveEnd) { end -= 1; } if (exclusiveStart) { start += 1; } if (end - 1 < start) { if (!allowEmptyRange) { console.warn(`range: end (${end}) < start (${start}) should be allowed explicitly, set options.allowEmptyRange to true`); } return []; } return Array.from({ length: end - start }, (_, index) => index * stepSize + start); }; var closestMatch = (list, firstCloser) => { return list.reduce((item1, item2) => { return firstCloser(item1, item2) ? item1 : item2; }); }; // src/components/date/TimePicker.tsx var import_clsx = __toESM(require("clsx")); var import_jsx_runtime = require("react/jsx-runtime"); var TimePicker = ({ time = /* @__PURE__ */ new Date(), onChange = noop, is24HourFormat = true, minuteIncrement = "5min", maxHeight = 300, className = "" }) => { const minuteRef = (0, import_react.useRef)(null); const hourRef = (0, import_react.useRef)(null); const isPM = time.getHours() >= 11; const hours = is24HourFormat ? range(24) : range([1, 12], { exclusiveEnd: false }); let minutes = range(60); (0, import_react.useEffect)(() => { const scrollToItem = () => { if (minuteRef.current) { const container = minuteRef.current.parentElement; const hasOverflow = container.scrollHeight > maxHeight; if (hasOverflow) { minuteRef.current.scrollIntoView({ behavior: "instant", block: "nearest" }); } } }; scrollToItem(); }, [minuteRef, minuteRef.current]); (0, import_react.useEffect)(() => { const scrollToItem = () => { if (hourRef.current) { const container = hourRef.current.parentElement; const hasOverflow = container.scrollHeight > maxHeight; if (hasOverflow) { hourRef.current.scrollIntoView({ behavior: "instant", block: "nearest" }); } } }; scrollToItem(); }, [hourRef, hourRef.current]); switch (minuteIncrement) { case "5min": minutes = minutes.filter((value) => value % 5 === 0); break; case "10min": minutes = minutes.filter((value) => value % 10 === 0); break; case "15min": minutes = minutes.filter((value) => value % 15 === 0); break; case "30min": minutes = minutes.filter((value) => value % 30 === 0); break; } const closestMinute = closestMatch(minutes, (item1, item2) => Math.abs(item1 - time.getMinutes()) < Math.abs(item2 - time.getMinutes())); const style = (selected) => (0, import_clsx.default)( "chip-full hover:brightness-90 hover:bg-primary hover:text-on-primary rounded-md mr-3", { "bg-primary text-on-primary": selected, "bg-white text-black": !selected } ); const onChangeWrapper = (transformer) => { const newDate = new Date(time); transformer(newDate); onChange(newDate); }; return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: (0, import_clsx.default)("flex-row-2 w-fit min-w-[150px] select-none", className), children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_custom_scrollbars_2.Scrollbars, { autoHeight: true, autoHeightMax: maxHeight, style: { height: "100%" }, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "flex-col-1 h-full", children: hours.map((hour) => { const currentHour = hour === time.getHours() - (!is24HourFormat && isPM ? 12 : 0); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( "button", { ref: currentHour ? hourRef : void 0, className: style(currentHour), onClick: () => onChangeWrapper((newDate) => newDate.setHours(hour + (!is24HourFormat && isPM ? 12 : 0))), children: hour.toString().padStart(2, "0") }, hour ); }) }) }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_custom_scrollbars_2.Scrollbars, { autoHeight: true, autoHeightMax: maxHeight, style: { height: "100%" }, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "flex-col-1 h-full", children: minutes.map((minute) => { const currentMinute = minute === closestMinute; return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( "button", { ref: currentMinute ? minuteRef : void 0, className: style(currentMinute), onClick: () => onChangeWrapper((newDate) => newDate.setMinutes(minute)), children: minute.toString().padStart(2, "0") }, minute + minuteIncrement ); }) }) }), !is24HourFormat && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "flex-col-1", children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)( "button", { className: style(!isPM), onClick: () => onChangeWrapper((newDate) => isPM && newDate.setHours(newDate.getHours() - 12)), children: "AM" } ), /* @__PURE__ */ (0, import_jsx_runtime.jsx)( "button", { className: style(isPM), onClick: () => onChangeWrapper((newDate) => !isPM && newDate.setHours(newDate.getHours() + 12)), children: "PM" } ) ] }) ] }); }; var TimePickerUncontrolled = ({ time, onChange = noop, ...props }) => { const [value, setValue] = (0, import_react.useState)(time); (0, import_react.useEffect)(() => setValue(time), [time]); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( TimePicker, { ...props, time: value, onChange: (time1) => { setValue(time1); onChange(time1); } } ); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { TimePicker, TimePickerUncontrolled }); //# sourceMappingURL=TimePicker.js.map