time-picker-input 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
package/README.md ADDED
@@ -0,0 +1,162 @@
1
+ # Time Picker Input
2
+
3
+ A customizable React time input component that enforces 12-hour or 24-hour format, bypassing OS and browser settings for consistent time input across all platforms.
4
+
5
+ ## Features
6
+
7
+ - 🎯 **Format Enforcement**: Enforce 12-hour or 24-hour format regardless of OS/browser settings
8
+ - 🎨 **Customizable**: Built with Tailwind CSS and fully customizable styling
9
+ - ♿ **Accessible**: Built on Radix UI primitives for full accessibility support
10
+ - ⌨️ **Keyboard Friendly**: Full keyboard navigation support
11
+ - 📱 **Mobile Friendly**: Touch-friendly time picker with scrollable lists
12
+ - 🎭 **TypeScript**: Full TypeScript support with exported types
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ npm install time-picker-input
18
+ ```
19
+
20
+ ### Peer Dependencies
21
+
22
+ This package requires the following peer dependencies:
23
+
24
+ ```bash
25
+ npm install react react-dom tailwindcss
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ ### Basic Example
31
+
32
+ ```tsx
33
+ import { useState } from "react";
34
+ import { TimeInput } from "time-picker-input";
35
+
36
+ function App() {
37
+ const [time, setTime] = useState("");
38
+
39
+ return (
40
+ <TimeInput
41
+ value={time}
42
+ onChange={setTime}
43
+ format={24} // or 12 for 12-hour format
44
+ />
45
+ );
46
+ }
47
+ ```
48
+
49
+ ### 12-Hour Format
50
+
51
+ ```tsx
52
+ import { TimeInput } from "time-picker-input";
53
+
54
+ <TimeInput
55
+ value={time}
56
+ onChange={setTime}
57
+ format={12}
58
+ />
59
+ ```
60
+
61
+ ### With Form Integration
62
+
63
+ ```tsx
64
+ import { TimeInput, TimeInputProps } from "time-picker-input";
65
+
66
+ function MyForm() {
67
+ const [time, setTime] = useState("14:30");
68
+
69
+ return (
70
+ <form>
71
+ <label>
72
+ Select Time:
73
+ <TimeInput
74
+ value={time}
75
+ onChange={(value) => setTime(value)}
76
+ onBlur={() => console.log("Time input blurred")}
77
+ format={24}
78
+ className="w-full"
79
+ />
80
+ </label>
81
+ </form>
82
+ );
83
+ }
84
+ ```
85
+
86
+ ### With Error Styling
87
+
88
+ ```tsx
89
+ <TimeInput
90
+ value={time}
91
+ onChange={setTime}
92
+ className="border-red-500 focus-within:ring-red-500"
93
+ />
94
+ ```
95
+
96
+ ## Props
97
+
98
+ | Prop | Type | Default | Description |
99
+ |------|------|---------|-------------|
100
+ | `value` | `string` | **required** | Time value in `HH:mm` format (24-hour format internally) |
101
+ | `onChange` | `(value: string) => void` | **required** | Callback fired when time changes |
102
+ | `onBlur` | `() => void` | `undefined` | Callback fired when input loses focus |
103
+ | `disabled` | `boolean` | `false` | Disables the input |
104
+ | `className` | `string` | `undefined` | Additional CSS classes |
105
+ | `format` | `12 \| 24` | `24` | Time format to display (12-hour or 24-hour) |
106
+
107
+ ## Value Format
108
+
109
+ The component always stores and returns time values in **24-hour format** (`HH:mm`), regardless of the display format:
110
+
111
+ - `"00:00"` - Midnight (12:00 AM in 12-hour format)
112
+ - `"12:00"` - Noon (12:00 PM in 12-hour format)
113
+ - `"14:30"` - 2:30 PM (2:30 PM in 12-hour format)
114
+ - `"23:59"` - 11:59 PM
115
+
116
+ ## Styling
117
+
118
+ The component uses Tailwind CSS and follows shadcn/ui design patterns. You can customize the appearance by:
119
+
120
+ 1. **Passing className prop**: Add custom classes to override default styles
121
+ 2. **CSS Variables**: The component uses CSS variables for theming. Ensure your Tailwind config includes the necessary color variables:
122
+
123
+ ```css
124
+ :root {
125
+ --background: 0 0% 100%;
126
+ --foreground: 220 39% 11%;
127
+ --primary: 218 91% 59%;
128
+ --primary-foreground: 0 0% 98%;
129
+ --muted: 220 14% 96%;
130
+ --muted-foreground: 220 8.9% 46.1%;
131
+ --accent: 220 14% 96%;
132
+ --accent-foreground: 220 39% 11%;
133
+ --border: 220 13% 91%;
134
+ --input: 220 13% 91%;
135
+ --ring: 218 91% 59%;
136
+ --popover: 0 0% 100%;
137
+ --popover-foreground: 220 39% 11%;
138
+ }
139
+ ```
140
+
141
+ ## Keyboard Navigation
142
+
143
+ - **Tab**: Navigate between hours and minutes inputs
144
+ - **Arrow Right / Colon (`:`)** : Move from hours to minutes
145
+ - **Arrow Left**: Move from minutes to hours
146
+ - **Enter**: Move to minutes when hours input is complete
147
+ - **Numbers**: Direct input of time values
148
+
149
+ ## Browser Support
150
+
151
+ Works in all modern browsers that support:
152
+ - React 18+
153
+ - CSS Grid and Flexbox
154
+ - ES6+ JavaScript
155
+
156
+ ## License
157
+
158
+ MIT
159
+
160
+ ## Contributing
161
+
162
+ Contributions are welcome! Please feel free to submit a Pull Request.
@@ -0,0 +1,6 @@
1
+ import { default as React } from 'react';
2
+ import * as PopoverPrimitive from "@radix-ui/react-popover";
3
+ declare const Popover: React.FC<PopoverPrimitive.PopoverProps>;
4
+ declare const PopoverTrigger: React.ForwardRefExoticComponent<PopoverPrimitive.PopoverTriggerProps & React.RefAttributes<HTMLButtonElement>>;
5
+ declare const PopoverContent: React.ForwardRefExoticComponent<Omit<PopoverPrimitive.PopoverContentProps & React.RefAttributes<HTMLDivElement>, "ref"> & React.RefAttributes<HTMLDivElement>>;
6
+ export { Popover, PopoverTrigger, PopoverContent };
@@ -0,0 +1,5 @@
1
+ import { default as React } from 'react';
2
+ import * as ScrollAreaPrimitive from "@radix-ui/react-scroll-area";
3
+ declare const ScrollArea: React.ForwardRefExoticComponent<Omit<ScrollAreaPrimitive.ScrollAreaProps & React.RefAttributes<HTMLDivElement>, "ref"> & React.RefAttributes<HTMLDivElement>>;
4
+ declare const ScrollBar: React.ForwardRefExoticComponent<Omit<ScrollAreaPrimitive.ScrollAreaScrollbarProps & React.RefAttributes<HTMLDivElement>, "ref"> & React.RefAttributes<HTMLDivElement>>;
5
+ export { ScrollArea, ScrollBar };
@@ -0,0 +1,2 @@
1
+ export { TimeInput } from './time-input';
2
+ export type { TimeInputProps } from './time-input';
package/dist/index.esm ADDED
@@ -0,0 +1,274 @@
1
+ import { jsx as n, jsxs as $ } from "react/jsx-runtime";
2
+ import H, { useRef as k, useState as I } from "react";
3
+ import { clsx as ee } from "clsx";
4
+ import { twMerge as te } from "tailwind-merge";
5
+ import { Clock as re } from "lucide-react";
6
+ import * as b from "@radix-ui/react-popover";
7
+ import * as h from "@radix-ui/react-scroll-area";
8
+ function f(...a) {
9
+ return te(ee(a));
10
+ }
11
+ const ne = b.Root, oe = b.Trigger, O = H.forwardRef(({ className: a, align: i = "center", sideOffset: u = 4, ...l }, m) => /* @__PURE__ */ n(b.Portal, { children: /* @__PURE__ */ n(
12
+ b.Content,
13
+ {
14
+ ref: m,
15
+ align: i,
16
+ sideOffset: u,
17
+ className: f(
18
+ "z-50 w-72 rounded-md border bg-popover p-4 text-popover-foreground shadow-md outline-none data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",
19
+ a
20
+ ),
21
+ ...l
22
+ }
23
+ ) }));
24
+ O.displayName = b.Content.displayName;
25
+ const V = H.forwardRef(({ className: a, children: i, ...u }, l) => /* @__PURE__ */ $(
26
+ h.Root,
27
+ {
28
+ ref: l,
29
+ className: f("relative overflow-hidden", a),
30
+ ...u,
31
+ children: [
32
+ /* @__PURE__ */ n(h.Viewport, { className: "h-full w-full rounded-[inherit]", children: i }),
33
+ /* @__PURE__ */ n(j, {}),
34
+ /* @__PURE__ */ n(h.Corner, {})
35
+ ]
36
+ }
37
+ ));
38
+ V.displayName = h.Root.displayName;
39
+ const j = H.forwardRef(({ className: a, orientation: i = "vertical", ...u }, l) => /* @__PURE__ */ n(
40
+ h.ScrollAreaScrollbar,
41
+ {
42
+ ref: l,
43
+ orientation: i,
44
+ className: f(
45
+ "flex touch-none select-none transition-colors",
46
+ i === "vertical" && "h-full w-2.5 border-l border-l-transparent p-[1px]",
47
+ i === "horizontal" && "h-2.5 flex-col border-t border-t-transparent p-[1px]",
48
+ a
49
+ ),
50
+ ...u,
51
+ children: /* @__PURE__ */ n(h.ScrollAreaThumb, { className: "relative flex-1 rounded-full bg-border" })
52
+ }
53
+ ));
54
+ j.displayName = h.ScrollAreaScrollbar.displayName;
55
+ const ue = ({ value: a, onChange: i, onBlur: u, disabled: l, className: m, format: D }) => {
56
+ const d = D || 24, T = k(null), y = k(null), [q, L] = I(!1), [w, x] = I(""), [v, P] = I(""), [N, C] = I("AM"), R = k(null), E = k(null), S = (t) => {
57
+ if (!t) return { hours: "", minutes: "" };
58
+ const e = t.split(":");
59
+ return {
60
+ hours: e[0] || "",
61
+ minutes: e[1] || ""
62
+ };
63
+ }, _ = () => {
64
+ if (!a) return { hours: "", minutes: "", amPm: "AM" };
65
+ const { hours: t, minutes: e } = S(a);
66
+ if (d === 12) {
67
+ const r = parseInt(t) || 0;
68
+ return r === 0 ? { hours: "12", minutes: e, amPm: "AM" } : r < 12 ? { hours: String(r), minutes: e, amPm: "AM" } : r === 12 ? { hours: "12", minutes: e, amPm: "PM" } : { hours: String(r - 12), minutes: e, amPm: "PM" };
69
+ }
70
+ return { hours: t, minutes: e, amPm: "AM" };
71
+ }, { hours: z, minutes: p, amPm: F } = _(), G = (t) => {
72
+ const e = parseInt(t) || 0;
73
+ return e === 0 ? { hour: "12", amPm: "AM" } : e < 12 ? { hour: String(e), amPm: "AM" } : e === 12 ? { hour: "12", amPm: "PM" } : { hour: String(e - 12), amPm: "PM" };
74
+ }, K = (t, e) => {
75
+ const r = parseInt(t) || 0;
76
+ return e === "AM" ? r === 12 ? "00" : String(r).padStart(2, "0") : r === 12 ? "12" : String(r + 12).padStart(2, "0");
77
+ }, J = (t) => {
78
+ if (L(t), t && a) {
79
+ const { hours: e, minutes: r } = S(a);
80
+ if (d === 12) {
81
+ const { hour: o, amPm: s } = G(e);
82
+ x(o), P(r), C(s), setTimeout(() => {
83
+ var A;
84
+ const c = parseInt(o) - 1, g = (A = R.current) == null ? void 0 : A.querySelector(`[data-hour-index="${c}"]`);
85
+ g && g.scrollIntoView({ block: "center", behavior: "smooth" });
86
+ }, 100);
87
+ } else
88
+ x(e), P(r), setTimeout(() => {
89
+ var c;
90
+ const o = parseInt(e) || 0, s = (c = R.current) == null ? void 0 : c.querySelector(`[data-hour-index="${o}"]`);
91
+ s && s.scrollIntoView({ block: "center", behavior: "smooth" });
92
+ }, 100);
93
+ setTimeout(() => {
94
+ var c;
95
+ const o = parseInt(r) || 0, s = (c = E.current) == null ? void 0 : c.querySelector(`[data-minute-index="${o}"]`);
96
+ s && s.scrollIntoView({ block: "center", behavior: "smooth" });
97
+ }, 150);
98
+ } else t && !a && (x(""), P(""), C("AM"));
99
+ }, M = (t, e, r) => {
100
+ let o = t;
101
+ d === 12 && r && (o = K(t, r));
102
+ const s = o ? String(parseInt(o) || 0).padStart(2, "0") : "00", c = e ? String(parseInt(e) || 0).padStart(2, "0") : "00", g = `${s}:${c}`;
103
+ i(g);
104
+ }, Q = m == null ? void 0 : m.includes("border-red-500"), U = (t) => {
105
+ var o;
106
+ let e = t.target.value.replace(/\D/g, "");
107
+ e.length > 2 && (e = e.slice(0, 2)), e.length === 2 && ((o = y.current) == null || o.focus());
108
+ const r = parseInt(e);
109
+ if (d === 12) {
110
+ if (e && (isNaN(r) || r < 1 || r > 12))
111
+ return;
112
+ const s = S(a).hours, c = parseInt(s) >= 12 ? "PM" : "AM", g = K(e || "12", c), A = e ? `${g}:${p.padStart(2, "0") || "00"}` : p ? `00:${p.padStart(2, "0")}` : "";
113
+ i(A);
114
+ } else {
115
+ if (e && (isNaN(r) || r < 0 || r > 23))
116
+ return;
117
+ const s = e ? `${e.padStart(2, "0")}:${p.padStart(2, "0") || "00"}` : p ? `00:${p.padStart(2, "0")}` : "";
118
+ i(s);
119
+ }
120
+ }, W = (t) => {
121
+ let e = t.target.value.replace(/\D/g, "");
122
+ e.length > 2 && (e = e.slice(0, 2));
123
+ const r = parseInt(e);
124
+ if (e && (isNaN(r) || r < 0 || r > 59))
125
+ return;
126
+ const o = S(a).hours, s = o ? `${o.padStart(2, "0")}:${e.padStart(2, "0") || "00"}` : e ? `00:${e.padStart(2, "0")}` : "";
127
+ i(s);
128
+ }, X = (t) => {
129
+ var e, r;
130
+ (t.key === "ArrowRight" || t.key === ":") && (t.preventDefault(), (e = y.current) == null || e.focus()), t.key === "Enter" && z.length === 2 && (t.preventDefault(), (r = y.current) == null || r.focus());
131
+ }, Y = (t) => {
132
+ var e;
133
+ t.key === "ArrowLeft" && (t.preventDefault(), (e = T.current) == null || e.focus());
134
+ }, Z = () => {
135
+ const { hours: t, minutes: e } = S(a), r = t ? String(parseInt(t) || 0).padStart(2, "0") : "00", o = e ? String(parseInt(e) || 0).padStart(2, "0") : "00", s = `${r}:${o}`;
136
+ i(s), u == null || u();
137
+ }, B = (m == null ? void 0 : m.split(" ").filter(
138
+ (t) => !t.includes("border-red-500") && !t.includes("focus:border-red-500")
139
+ ).join(" ")) || "";
140
+ return /* @__PURE__ */ $(
141
+ "div",
142
+ {
143
+ className: f(
144
+ "flex h-10 w-full items-center gap-1 rounded-md border border-input bg-background px-3 py-2 text-base ring-offset-background transition-colors focus-within:outline-none focus-within:ring-2 focus-within:ring-ring focus-within:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 md:text-sm",
145
+ Q && "border-red-500 focus-within:ring-red-500",
146
+ l && "cursor-not-allowed opacity-50",
147
+ B
148
+ ),
149
+ onBlur: Z,
150
+ children: [
151
+ /* @__PURE__ */ n(
152
+ "input",
153
+ {
154
+ ref: T,
155
+ type: "text",
156
+ inputMode: "numeric",
157
+ value: z,
158
+ onChange: U,
159
+ onKeyDown: X,
160
+ disabled: l,
161
+ placeholder: "HH",
162
+ maxLength: 2,
163
+ className: "w-12 text-center bg-transparent border-0 outline-none focus:outline-none p-0 placeholder:text-muted-foreground"
164
+ }
165
+ ),
166
+ /* @__PURE__ */ n("span", { className: "text-muted-foreground font-medium select-none", children: ":" }),
167
+ /* @__PURE__ */ n(
168
+ "input",
169
+ {
170
+ ref: y,
171
+ type: "text",
172
+ inputMode: "numeric",
173
+ value: p,
174
+ onChange: W,
175
+ onKeyDown: Y,
176
+ disabled: l,
177
+ placeholder: "mm",
178
+ maxLength: 2,
179
+ className: "w-12 text-center bg-transparent border-0 outline-none focus:outline-none p-0 placeholder:text-muted-foreground"
180
+ }
181
+ ),
182
+ d === 12 && /* @__PURE__ */ n("span", { className: "text-muted-foreground font-medium select-none ml-1", children: F }),
183
+ /* @__PURE__ */ $(ne, { open: q, onOpenChange: J, children: [
184
+ /* @__PURE__ */ n(oe, { asChild: !0, children: /* @__PURE__ */ n(
185
+ "button",
186
+ {
187
+ type: "button",
188
+ disabled: l,
189
+ className: "ml-auto flex-shrink-0 cursor-pointer hover:text-foreground transition-colors disabled:cursor-not-allowed disabled:opacity-50",
190
+ children: /* @__PURE__ */ n(re, { className: "h-4 w-4 text-muted-foreground" })
191
+ }
192
+ ) }),
193
+ /* @__PURE__ */ n(O, { className: "w-auto p-2", align: "end", children: /* @__PURE__ */ $("div", { className: "flex items-center gap-2", children: [
194
+ /* @__PURE__ */ n("div", { className: "flex flex-col", children: /* @__PURE__ */ n(V, { className: "h-48 w-16", children: /* @__PURE__ */ n("div", { className: "py-20", ref: R, children: d === 12 ? Array.from({ length: 12 }, (t, e) => e + 1).map((t) => {
195
+ const e = String(t).padStart(2, "0"), r = w === String(t);
196
+ return /* @__PURE__ */ n(
197
+ "button",
198
+ {
199
+ type: "button",
200
+ "data-hour-index": t - 1,
201
+ onClick: () => {
202
+ x(String(t)), M(String(t), v, d === 12 ? N : void 0);
203
+ },
204
+ className: f(
205
+ "w-full py-2 text-center text-sm hover:bg-accent hover:text-accent-foreground transition-colors",
206
+ r && "bg-primary text-primary-foreground font-medium"
207
+ ),
208
+ children: e
209
+ },
210
+ t
211
+ );
212
+ }) : Array.from({ length: 24 }, (t, e) => e).map((t) => {
213
+ const e = String(t).padStart(2, "0");
214
+ return /* @__PURE__ */ n(
215
+ "button",
216
+ {
217
+ type: "button",
218
+ "data-hour-index": t,
219
+ onClick: () => {
220
+ x(e), M(e, v, D === 12 ? N : void 0);
221
+ },
222
+ className: f(
223
+ "w-full py-2 text-center text-sm hover:bg-accent hover:text-accent-foreground transition-colors",
224
+ w === e && "bg-primary text-primary-foreground font-medium"
225
+ ),
226
+ children: e
227
+ },
228
+ t
229
+ );
230
+ }) }) }) }),
231
+ /* @__PURE__ */ n("span", { className: "text-lg font-medium", children: ":" }),
232
+ /* @__PURE__ */ n("div", { className: "flex flex-col", children: /* @__PURE__ */ n(V, { className: "h-48 w-16", children: /* @__PURE__ */ n("div", { className: "py-20", ref: E, children: Array.from({ length: 60 }, (t, e) => e).map((t) => {
233
+ const e = String(t).padStart(2, "0");
234
+ return /* @__PURE__ */ n(
235
+ "button",
236
+ {
237
+ type: "button",
238
+ "data-minute-index": t,
239
+ onClick: () => {
240
+ P(e), M(w, e, d === 12 ? N : void 0);
241
+ },
242
+ className: f(
243
+ "w-full py-2 text-center text-sm hover:bg-accent hover:text-accent-foreground transition-colors",
244
+ v === e && "bg-primary text-primary-foreground font-medium"
245
+ ),
246
+ children: e
247
+ },
248
+ t
249
+ );
250
+ }) }) }) }),
251
+ d === 12 && /* @__PURE__ */ n("div", { className: "flex flex-col", children: /* @__PURE__ */ n(V, { className: "h-48 w-16", children: /* @__PURE__ */ n("div", { className: "py-20", children: ["AM", "PM"].map((t) => /* @__PURE__ */ n(
252
+ "button",
253
+ {
254
+ type: "button",
255
+ onClick: () => {
256
+ C(t), M(w, v, t);
257
+ },
258
+ className: f(
259
+ "w-full py-2 text-center text-sm hover:bg-accent hover:text-accent-foreground transition-colors",
260
+ N === t && "bg-primary text-primary-foreground font-medium"
261
+ ),
262
+ children: t
263
+ },
264
+ t
265
+ )) }) }) })
266
+ ] }) })
267
+ ] })
268
+ ]
269
+ }
270
+ );
271
+ };
272
+ export {
273
+ ue as TimeInput
274
+ };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("react/jsx-runtime"),d=require("react"),Z=require("clsx"),B=require("tailwind-merge"),ee=require("lucide-react"),te=require("@radix-ui/react-popover"),re=require("@radix-ui/react-scroll-area");function H(o){const a=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(o){for(const c in o)if(c!=="default"){const u=Object.getOwnPropertyDescriptor(o,c);Object.defineProperty(a,c,u.get?u:{enumerable:!0,get:()=>o[c]})}}return a.default=o,Object.freeze(a)}const y=H(te),x=H(re);function p(...o){return B.twMerge(Z.clsx(o))}const ne=y.Root,oe=y.Trigger,q=d.forwardRef(({className:o,align:a="center",sideOffset:c=4,...u},f)=>n.jsx(y.Portal,{children:n.jsx(y.Content,{ref:f,align:a,sideOffset:c,className:p("z-50 w-72 rounded-md border bg-popover p-4 text-popover-foreground shadow-md outline-none data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",o),...u})}));q.displayName=y.Content.displayName;const k=d.forwardRef(({className:o,children:a,...c},u)=>n.jsxs(x.Root,{ref:u,className:p("relative overflow-hidden",o),...c,children:[n.jsx(x.Viewport,{className:"h-full w-full rounded-[inherit]",children:a}),n.jsx(O,{}),n.jsx(x.Corner,{})]}));k.displayName=x.Root.displayName;const O=d.forwardRef(({className:o,orientation:a="vertical",...c},u)=>n.jsx(x.ScrollAreaScrollbar,{ref:u,orientation:a,className:p("flex touch-none select-none transition-colors",a==="vertical"&&"h-full w-2.5 border-l border-l-transparent p-[1px]",a==="horizontal"&&"h-2.5 flex-col border-t border-t-transparent p-[1px]",o),...c,children:n.jsx(x.ScrollAreaThumb,{className:"relative flex-1 rounded-full bg-border"})}));O.displayName=x.ScrollAreaScrollbar.displayName;const se=({value:o,onChange:a,onBlur:c,disabled:u,className:f,format:$})=>{const m=$||24,V=d.useRef(null),v=d.useRef(null),[_,z]=d.useState(!1),[w,b]=d.useState(""),[P,j]=d.useState(""),[N,I]=d.useState("AM"),R=d.useRef(null),C=d.useRef(null),S=t=>{if(!t)return{hours:"",minutes:""};const e=t.split(":");return{hours:e[0]||"",minutes:e[1]||""}},E=()=>{if(!o)return{hours:"",minutes:"",amPm:"AM"};const{hours:t,minutes:e}=S(o);if(m===12){const r=parseInt(t)||0;return r===0?{hours:"12",minutes:e,amPm:"AM"}:r<12?{hours:String(r),minutes:e,amPm:"AM"}:r===12?{hours:"12",minutes:e,amPm:"PM"}:{hours:String(r-12),minutes:e,amPm:"PM"}}return{hours:t,minutes:e,amPm:"AM"}},{hours:T,minutes:h,amPm:K}=E(),L=t=>{const e=parseInt(t)||0;return e===0?{hour:"12",amPm:"AM"}:e<12?{hour:String(e),amPm:"AM"}:e===12?{hour:"12",amPm:"PM"}:{hour:String(e-12),amPm:"PM"}},D=(t,e)=>{const r=parseInt(t)||0;return e==="AM"?r===12?"00":String(r).padStart(2,"0"):r===12?"12":String(r+12).padStart(2,"0")},F=t=>{if(z(t),t&&o){const{hours:e,minutes:r}=S(o);if(m===12){const{hour:s,amPm:i}=L(e);b(s),j(r),I(i),setTimeout(()=>{var A;const l=parseInt(s)-1,g=(A=R.current)==null?void 0:A.querySelector(`[data-hour-index="${l}"]`);g&&g.scrollIntoView({block:"center",behavior:"smooth"})},100)}else b(e),j(r),setTimeout(()=>{var l;const s=parseInt(e)||0,i=(l=R.current)==null?void 0:l.querySelector(`[data-hour-index="${s}"]`);i&&i.scrollIntoView({block:"center",behavior:"smooth"})},100);setTimeout(()=>{var l;const s=parseInt(r)||0,i=(l=C.current)==null?void 0:l.querySelector(`[data-minute-index="${s}"]`);i&&i.scrollIntoView({block:"center",behavior:"smooth"})},150)}else t&&!o&&(b(""),j(""),I("AM"))},M=(t,e,r)=>{let s=t;m===12&&r&&(s=D(t,r));const i=s?String(parseInt(s)||0).padStart(2,"0"):"00",l=e?String(parseInt(e)||0).padStart(2,"0"):"00",g=`${i}:${l}`;a(g)},G=f==null?void 0:f.includes("border-red-500"),J=t=>{var s;let e=t.target.value.replace(/\D/g,"");e.length>2&&(e=e.slice(0,2)),e.length===2&&((s=v.current)==null||s.focus());const r=parseInt(e);if(m===12){if(e&&(isNaN(r)||r<1||r>12))return;const i=S(o).hours,l=parseInt(i)>=12?"PM":"AM",g=D(e||"12",l),A=e?`${g}:${h.padStart(2,"0")||"00"}`:h?`00:${h.padStart(2,"0")}`:"";a(A)}else{if(e&&(isNaN(r)||r<0||r>23))return;const i=e?`${e.padStart(2,"0")}:${h.padStart(2,"0")||"00"}`:h?`00:${h.padStart(2,"0")}`:"";a(i)}},Q=t=>{let e=t.target.value.replace(/\D/g,"");e.length>2&&(e=e.slice(0,2));const r=parseInt(e);if(e&&(isNaN(r)||r<0||r>59))return;const s=S(o).hours,i=s?`${s.padStart(2,"0")}:${e.padStart(2,"0")||"00"}`:e?`00:${e.padStart(2,"0")}`:"";a(i)},U=t=>{var e,r;(t.key==="ArrowRight"||t.key===":")&&(t.preventDefault(),(e=v.current)==null||e.focus()),t.key==="Enter"&&T.length===2&&(t.preventDefault(),(r=v.current)==null||r.focus())},W=t=>{var e;t.key==="ArrowLeft"&&(t.preventDefault(),(e=V.current)==null||e.focus())},X=()=>{const{hours:t,minutes:e}=S(o),r=t?String(parseInt(t)||0).padStart(2,"0"):"00",s=e?String(parseInt(e)||0).padStart(2,"0"):"00",i=`${r}:${s}`;a(i),c==null||c()},Y=(f==null?void 0:f.split(" ").filter(t=>!t.includes("border-red-500")&&!t.includes("focus:border-red-500")).join(" "))||"";return n.jsxs("div",{className:p("flex h-10 w-full items-center gap-1 rounded-md border border-input bg-background px-3 py-2 text-base ring-offset-background transition-colors focus-within:outline-none focus-within:ring-2 focus-within:ring-ring focus-within:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 md:text-sm",G&&"border-red-500 focus-within:ring-red-500",u&&"cursor-not-allowed opacity-50",Y),onBlur:X,children:[n.jsx("input",{ref:V,type:"text",inputMode:"numeric",value:T,onChange:J,onKeyDown:U,disabled:u,placeholder:"HH",maxLength:2,className:"w-12 text-center bg-transparent border-0 outline-none focus:outline-none p-0 placeholder:text-muted-foreground"}),n.jsx("span",{className:"text-muted-foreground font-medium select-none",children:":"}),n.jsx("input",{ref:v,type:"text",inputMode:"numeric",value:h,onChange:Q,onKeyDown:W,disabled:u,placeholder:"mm",maxLength:2,className:"w-12 text-center bg-transparent border-0 outline-none focus:outline-none p-0 placeholder:text-muted-foreground"}),m===12&&n.jsx("span",{className:"text-muted-foreground font-medium select-none ml-1",children:K}),n.jsxs(ne,{open:_,onOpenChange:F,children:[n.jsx(oe,{asChild:!0,children:n.jsx("button",{type:"button",disabled:u,className:"ml-auto flex-shrink-0 cursor-pointer hover:text-foreground transition-colors disabled:cursor-not-allowed disabled:opacity-50",children:n.jsx(ee.Clock,{className:"h-4 w-4 text-muted-foreground"})})}),n.jsx(q,{className:"w-auto p-2",align:"end",children:n.jsxs("div",{className:"flex items-center gap-2",children:[n.jsx("div",{className:"flex flex-col",children:n.jsx(k,{className:"h-48 w-16",children:n.jsx("div",{className:"py-20",ref:R,children:m===12?Array.from({length:12},(t,e)=>e+1).map(t=>{const e=String(t).padStart(2,"0"),r=w===String(t);return n.jsx("button",{type:"button","data-hour-index":t-1,onClick:()=>{b(String(t)),M(String(t),P,m===12?N:void 0)},className:p("w-full py-2 text-center text-sm hover:bg-accent hover:text-accent-foreground transition-colors",r&&"bg-primary text-primary-foreground font-medium"),children:e},t)}):Array.from({length:24},(t,e)=>e).map(t=>{const e=String(t).padStart(2,"0"),r=w===e;return n.jsx("button",{type:"button","data-hour-index":t,onClick:()=>{b(e),M(e,P,$===12?N:void 0)},className:p("w-full py-2 text-center text-sm hover:bg-accent hover:text-accent-foreground transition-colors",r&&"bg-primary text-primary-foreground font-medium"),children:e},t)})})})}),n.jsx("span",{className:"text-lg font-medium",children:":"}),n.jsx("div",{className:"flex flex-col",children:n.jsx(k,{className:"h-48 w-16",children:n.jsx("div",{className:"py-20",ref:C,children:Array.from({length:60},(t,e)=>e).map(t=>{const e=String(t).padStart(2,"0"),r=P===e;return n.jsx("button",{type:"button","data-minute-index":t,onClick:()=>{j(e),M(w,e,m===12?N:void 0)},className:p("w-full py-2 text-center text-sm hover:bg-accent hover:text-accent-foreground transition-colors",r&&"bg-primary text-primary-foreground font-medium"),children:e},t)})})})}),m===12&&n.jsx("div",{className:"flex flex-col",children:n.jsx(k,{className:"h-48 w-16",children:n.jsx("div",{className:"py-20",children:["AM","PM"].map(t=>{const e=N===t;return n.jsx("button",{type:"button",onClick:()=>{I(t),M(w,P,t)},className:p("w-full py-2 text-center text-sm hover:bg-accent hover:text-accent-foreground transition-colors",e&&"bg-primary text-primary-foreground font-medium"),children:t},t)})})})})]})})]})]})};exports.TimeInput=se;
@@ -0,0 +1,2 @@
1
+ import { ClassValue } from 'clsx';
2
+ export declare function cn(...inputs: ClassValue[]): string;
@@ -0,0 +1,9 @@
1
+ export interface TimeInputProps {
2
+ value: string;
3
+ onChange: (value: string) => void;
4
+ onBlur?: () => void;
5
+ disabled?: boolean;
6
+ className?: string;
7
+ format?: 12 | 24;
8
+ }
9
+ export declare const TimeInput: ({ value, onChange, onBlur, disabled, className, format }: TimeInputProps) => import("react/jsx-runtime").JSX.Element;
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "time-picker-input",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "description": "A customizable React time input component that enforces 12-hour or 24-hour format, bypassing OS and browser settings for consistent time input across all platforms",
6
+ "main": "dist/index.js",
7
+ "module": "dist/index.esm.js",
8
+ "types": "dist/index.d.ts",
9
+ "files": [
10
+ "dist",
11
+ "README.md",
12
+ "LICENSE"
13
+ ],
14
+ "scripts": {
15
+ "build": "vite build",
16
+ "dev": "vite",
17
+ "lint": "eslint .",
18
+ "prepublishOnly": "npm run build"
19
+ },
20
+ "keywords": [
21
+ "react",
22
+ "time-picker",
23
+ "time-input",
24
+ "time-selector",
25
+ "12-hour",
26
+ "24-hour",
27
+ "tailwindcss",
28
+ "shadcn-ui",
29
+ "radix-ui"
30
+ ],
31
+ "author": "",
32
+ "license": "MIT",
33
+ "peerDependencies": {
34
+ "react": "^18.0.0",
35
+ "react-dom": "^18.0.0",
36
+ "tailwindcss": "^3.0.0"
37
+ },
38
+ "dependencies": {
39
+ "@radix-ui/react-popover": "^1.1.14",
40
+ "@radix-ui/react-scroll-area": "^1.2.9",
41
+ "clsx": "^2.1.1",
42
+ "lucide-react": "^0.462.0",
43
+ "tailwind-merge": "^2.6.0"
44
+ },
45
+ "devDependencies": {
46
+ "@eslint/js": "^9.32.0",
47
+ "@types/node": "^22.16.5",
48
+ "@types/react": "^18.3.23",
49
+ "@types/react-dom": "^18.3.7",
50
+ "@vitejs/plugin-react-swc": "^3.11.0",
51
+ "autoprefixer": "^10.4.21",
52
+ "eslint": "^9.32.0",
53
+ "eslint-plugin-react-hooks": "^5.2.0",
54
+ "eslint-plugin-react-refresh": "^0.4.20",
55
+ "globals": "^15.15.0",
56
+ "postcss": "^8.5.6",
57
+ "tailwindcss": "^3.4.17",
58
+ "typescript": "^5.8.3",
59
+ "typescript-eslint": "^8.38.0",
60
+ "vite": "^5.4.19",
61
+ "vite-plugin-dts": "^4.3.0"
62
+ }
63
+ }