performa 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/dist/index.js ADDED
@@ -0,0 +1,1591 @@
1
+ import { createContext, memo, useState, useRef, useEffect, useContext, useMemo } from 'react';
2
+ import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
3
+ import { Calendar, ChevronLeft, ChevronRight, Clock, Eye, EyeOff } from 'lucide-react';
4
+
5
+ // src/core/types.ts
6
+ var mimeTypes = {
7
+ JPG: "image/jpeg",
8
+ JPEG: "image/jpeg",
9
+ PNG: "image/png",
10
+ GIF: "image/gif",
11
+ WEBP: "image/webp",
12
+ SVG: "image/svg+xml",
13
+ BMP: "image/bmp",
14
+ TIFF: "image/tiff",
15
+ ICO: "image/x-icon",
16
+ PDF: "application/pdf",
17
+ DOC: "application/msword",
18
+ DOCX: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
19
+ XLS: "application/vnd.ms-excel",
20
+ XLSX: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
21
+ PPT: "application/vnd.ms-powerpoint",
22
+ PPTX: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
23
+ ZIP: "application/zip",
24
+ RAR: "application/x-rar-compressed",
25
+ "7Z": "application/x-7z-compressed",
26
+ TAR: "application/x-tar",
27
+ GZ: "application/x-gzip",
28
+ BZ2: "application/x-bzip2",
29
+ ISO: "application/x-iso9660-image",
30
+ SWF: "application/x-shockwave-flash",
31
+ TTF: "application/x-font-ttf",
32
+ MP4: "video/mp4",
33
+ MP3: "audio/mpeg",
34
+ MOV: "video/quicktime",
35
+ AVI: "video/x-msvideo",
36
+ WMV: "video/x-ms-wmv",
37
+ MPEG: "video/mpeg",
38
+ TXT: "text/plain",
39
+ CSV: "text/csv"
40
+ };
41
+ var FormContext = createContext(null);
42
+ function useFormContext() {
43
+ const context = useContext(FormContext);
44
+ if (!context) {
45
+ throw new Error("useFormContext must be used within a Form component");
46
+ }
47
+ return context;
48
+ }
49
+ function useFormState() {
50
+ const { state } = useFormContext();
51
+ return state;
52
+ }
53
+ function useFormConfig() {
54
+ const { config } = useFormContext();
55
+ return config;
56
+ }
57
+ function useFieldError(fieldName) {
58
+ const { state } = useFormContext();
59
+ return state.errors?.[fieldName];
60
+ }
61
+
62
+ // src/core/utils.ts
63
+ function bytesToHumanReadable(bytes) {
64
+ const sizes = ["Bytes", "KB", "MB", "GB", "TB"];
65
+ if (bytes === 0) return "0 Bytes";
66
+ const i = Math.floor(Math.log(bytes) / Math.log(1024));
67
+ return Math.round(bytes / Math.pow(1024, i) * 100) / 100 + " " + sizes[i];
68
+ }
69
+ function isImage(filename) {
70
+ const imageExtensions = [
71
+ ".jpg",
72
+ ".jpeg",
73
+ ".png",
74
+ ".gif",
75
+ ".webp",
76
+ ".svg",
77
+ ".bmp",
78
+ ".tiff",
79
+ ".ico"
80
+ ];
81
+ const ext = filename.toLowerCase().slice(filename.lastIndexOf("."));
82
+ return imageExtensions.includes(ext);
83
+ }
84
+ var widthClasses = {
85
+ 1: "col-span-12 @lg:col-span-1",
86
+ 2: "col-span-4 @lg:col-span-2",
87
+ 3: "col-span-6 @lg:col-span-3",
88
+ 4: "col-span-8 @lg:col-span-4",
89
+ 5: "col-span-12 @lg:col-span-5",
90
+ 6: "col-span-12 @lg:col-span-6",
91
+ 7: "col-span-12 @lg:col-span-7",
92
+ 8: "col-span-12 @lg:col-span-8",
93
+ 9: "col-span-12 @lg:col-span-9",
94
+ 10: "col-span-12 @lg:col-span-10",
95
+ 11: "col-span-12 @lg:col-span-11",
96
+ 12: "col-span-12"
97
+ };
98
+ var defaultTheme = {
99
+ darkMode: true,
100
+ formGroup: "form-group",
101
+ form: "w-full",
102
+ fieldset: "@container grid grid-cols-12 gap-4",
103
+ label: {
104
+ base: "block text-sm font-medium text-gray-700 dark:text-gray-300",
105
+ required: "text-red-500 ml-1"
106
+ },
107
+ input: {
108
+ base: "block w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 placeholder-gray-400 dark:placeholder-gray-500 focus:outline-none focus:ring focus:ring-primary focus:border-primary",
109
+ error: "border-red-500 dark:border-red-400",
110
+ disabled: "disabled:bg-gray-100 dark:disabled:bg-gray-700 disabled:cursor-not-allowed",
111
+ focus: "focus:outline-none focus:ring focus:ring-primary focus:border-primary"
112
+ },
113
+ select: {
114
+ base: "mt-1 block w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 focus:outline-none focus:ring focus:ring-primary focus:border-primary disabled:bg-gray-100 dark:disabled:bg-gray-700 disabled:cursor-not-allowed",
115
+ error: "border-red-500 dark:border-red-400",
116
+ disabled: "disabled:bg-gray-100 dark:disabled:bg-gray-700 disabled:cursor-not-allowed",
117
+ option: "bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100"
118
+ },
119
+ textarea: {
120
+ base: "mt-1 block w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 placeholder-gray-400 dark:placeholder-gray-500 focus:outline-none focus:ring focus:ring-primary focus:border-primary disabled:bg-gray-100 dark:disabled:bg-gray-700 disabled:cursor-not-allowed",
121
+ error: "border-red-500 dark:border-red-400",
122
+ disabled: "disabled:bg-gray-100 dark:disabled:bg-gray-700 disabled:cursor-not-allowed"
123
+ },
124
+ checkbox: {
125
+ base: "h-4 w-4 text-primary focus:ring-primary border-gray-300 dark:border-gray-600 dark:bg-gray-800 rounded disabled:cursor-not-allowed",
126
+ error: "border-red-500 dark:border-red-400",
127
+ label: "ml-2 block text-sm font-medium text-gray-700 dark:text-gray-300"
128
+ },
129
+ toggle: {
130
+ track: "absolute inset-0 rounded-full transition-colors duration-200 bg-gray-300 dark:bg-gray-600 peer-focus:ring-2 peer-focus:ring-primary peer-focus:ring-offset-2 dark:peer-focus:ring-offset-gray-800",
131
+ trackActive: "peer-checked:bg-primary peer-checked:dark:bg-primary-light",
132
+ thumb: "absolute left-1 top-1 w-4 h-4 bg-white dark:bg-gray-200 rounded-full transition-transform duration-200 peer-checked:translate-x-5",
133
+ label: "block text-sm font-medium text-gray-700 dark:text-gray-300"
134
+ },
135
+ radio: {
136
+ base: "h-4 w-4 text-primary focus:ring-primary border-gray-300 dark:border-gray-600 dark:bg-gray-800 disabled:cursor-not-allowed",
137
+ label: "ml-2 text-sm text-gray-700 dark:text-gray-300",
138
+ group: "mt-1"
139
+ },
140
+ file: {
141
+ dropzone: "flex flex-col items-center justify-center w-full p-6 border-2 border-dashed rounded-md cursor-pointer border-gray-300 dark:border-gray-600 hover:border-primary dark:hover:border-primary transition-colors duration-200",
142
+ dropzoneActive: "border-primary bg-primary-50 dark:bg-primary-900/20",
143
+ dropzoneError: "border-red-500 dark:border-red-400 hover:border-red-500 dark:hover:border-red-400",
144
+ icon: "w-8 h-8 text-gray-400 dark:text-gray-500",
145
+ text: "mt-2 text-sm text-gray-500 dark:text-gray-400",
146
+ hint: "mt-1 text-xs text-gray-500 dark:text-gray-400"
147
+ },
148
+ submit: {
149
+ base: "mt-1 flex items-center justify-center w-full px-4 py-2 border border-transparent rounded-md shadow-sm text-white bg-primary hover:bg-primary-dark dark:bg-primary-light dark:hover:bg-primary focus:outline-none focus:ring-2 focus:ring-offset-2 dark:focus:ring-offset-gray-800 focus:ring-primary transition-colors cursor-pointer disabled:opacity-50 disabled:cursor-not-allowed",
150
+ loading: "opacity-75",
151
+ spinner: "mr-3 -ml-1 size-5 animate-spin text-white"
152
+ },
153
+ error: "mt-1 text-sm text-red-600 dark:text-red-400",
154
+ alert: {
155
+ base: "p-4 mb-4 border rounded-md",
156
+ error: "bg-red-50 dark:bg-red-900/20 text-red-800 dark:text-red-200 border-red-200 dark:border-red-800",
157
+ success: "bg-green-50 dark:bg-green-900/20 text-green-800 dark:text-green-200 border-green-200 dark:border-green-800",
158
+ warning: "bg-yellow-50 dark:bg-yellow-900/20 text-yellow-800 dark:text-yellow-200 border-yellow-200 dark:border-yellow-800",
159
+ info: "bg-blue-50 dark:bg-blue-900/20 text-blue-800 dark:text-blue-200 border-blue-200 dark:border-blue-800"
160
+ },
161
+ datetime: {
162
+ input: "block w-full px-3 py-2 pr-10 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 placeholder-gray-400 dark:placeholder-gray-500 focus:outline-none focus:ring focus:ring-primary focus:border-primary disabled:bg-gray-100 dark:disabled:bg-gray-700 disabled:cursor-not-allowed cursor-pointer",
163
+ dropdown: "absolute z-50 mt-2 p-4 bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded-lg shadow-lg w-80",
164
+ picker: "absolute z-50 mt-2 p-4 bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded-lg shadow-lg w-80",
165
+ calendar: "grid grid-cols-7 gap-1 mb-4",
166
+ day: "p-2 text-sm rounded hover:bg-primary hover:text-white transition-colors text-gray-700 dark:text-gray-300",
167
+ daySelected: "bg-primary text-white",
168
+ dayButton: "p-2 text-sm rounded hover:bg-primary hover:text-white transition-colors text-gray-700 dark:text-gray-300",
169
+ dayButtonSelected: "bg-primary text-white",
170
+ timeInput: "w-14 px-2 py-1 text-center border border-gray-300 dark:border-gray-600 rounded bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100",
171
+ timeLabel: "text-xs font-medium text-gray-700 dark:text-gray-300",
172
+ periodButton: "px-3 py-1 text-xs rounded bg-gray-100 dark:bg-gray-700 text-gray-700 dark:text-gray-300",
173
+ periodButtonActive: "bg-primary text-white",
174
+ iconButton: "absolute right-2 top-1/2 -translate-y-1/2 text-gray-500 dark:text-gray-400",
175
+ navButton: "p-1 hover:bg-gray-100 dark:hover:bg-gray-700 rounded text-gray-700 dark:text-white",
176
+ monthYear: "text-sm font-medium text-gray-900 dark:text-gray-100",
177
+ weekday: "text-center text-xs font-medium text-gray-500 dark:text-gray-400 p-2",
178
+ formatButton: "px-2 py-1 text-xs bg-gray-100 dark:bg-gray-700 text-gray-700 dark:text-gray-300 rounded hover:bg-gray-200 dark:hover:bg-gray-600"
179
+ },
180
+ button: {
181
+ primary: "flex-1 px-3 py-2 text-sm bg-primary text-white rounded hover:bg-primary-dark focus:outline-none focus:ring-2 focus:ring-primary focus:ring-offset-2",
182
+ secondary: "flex-1 px-3 py-2 text-sm border border-gray-300 dark:border-gray-600 rounded hover:bg-gray-50 dark:hover:bg-gray-700 text-gray-700 dark:text-gray-300"
183
+ },
184
+ readonly: "mt-1 block w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm bg-gray-100 dark:bg-gray-700 text-gray-900 dark:text-gray-100",
185
+ none: {
186
+ container: "form-group",
187
+ label: "block text-sm font-medium text-gray-700 dark:text-gray-300",
188
+ value: "mt-1 block w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm bg-gray-100 dark:bg-gray-700 text-gray-900 dark:text-gray-100"
189
+ }
190
+ };
191
+ var lightOnlyTheme = {
192
+ darkMode: false,
193
+ formGroup: "form-group",
194
+ form: "w-full",
195
+ fieldset: "@container grid grid-cols-12 gap-4",
196
+ label: {
197
+ base: "block text-sm font-medium text-gray-700",
198
+ required: "text-red-500 ml-1"
199
+ },
200
+ input: {
201
+ base: "block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm bg-white text-gray-900 placeholder-gray-400 focus:outline-none focus:ring focus:ring-primary focus:border-primary",
202
+ error: "border-red-500",
203
+ disabled: "disabled:bg-gray-100 disabled:cursor-not-allowed",
204
+ focus: "focus:outline-none focus:ring focus:ring-primary focus:border-primary"
205
+ },
206
+ select: {
207
+ base: "mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm bg-white text-gray-900 focus:outline-none focus:ring focus:ring-primary focus:border-primary disabled:bg-gray-100 disabled:cursor-not-allowed",
208
+ error: "border-red-500",
209
+ disabled: "disabled:bg-gray-100 disabled:cursor-not-allowed",
210
+ option: "bg-white text-gray-900"
211
+ },
212
+ textarea: {
213
+ base: "mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm bg-white text-gray-900 placeholder-gray-400 focus:outline-none focus:ring focus:ring-primary focus:border-primary disabled:bg-gray-100 disabled:cursor-not-allowed",
214
+ error: "border-red-500",
215
+ disabled: "disabled:bg-gray-100 disabled:cursor-not-allowed"
216
+ },
217
+ checkbox: {
218
+ base: "h-4 w-4 text-primary focus:ring-primary border-gray-300 rounded disabled:cursor-not-allowed",
219
+ error: "border-red-500",
220
+ label: "ml-2 block text-sm font-medium text-gray-700"
221
+ },
222
+ toggle: {
223
+ track: "absolute inset-0 rounded-full transition-colors duration-200 bg-gray-300 peer-focus:ring-2 peer-focus:ring-primary peer-focus:ring-offset-2",
224
+ trackActive: "peer-checked:bg-primary",
225
+ thumb: "absolute left-1 top-1 w-4 h-4 bg-white rounded-full transition-transform duration-200 peer-checked:translate-x-5",
226
+ label: "block text-sm font-medium text-gray-700"
227
+ },
228
+ radio: {
229
+ base: "h-4 w-4 text-primary focus:ring-primary border-gray-300 disabled:cursor-not-allowed",
230
+ label: "ml-2 text-sm text-gray-700",
231
+ group: "mt-1"
232
+ },
233
+ file: {
234
+ dropzone: "flex flex-col items-center justify-center w-full p-6 border-2 border-dashed rounded-md cursor-pointer border-gray-300 hover:border-primary transition-colors duration-200",
235
+ dropzoneActive: "border-primary bg-primary-50",
236
+ dropzoneError: "border-red-500 hover:border-red-500",
237
+ icon: "w-8 h-8 text-gray-400",
238
+ text: "mt-2 text-sm text-gray-500",
239
+ hint: "mt-1 text-xs text-gray-500"
240
+ },
241
+ submit: {
242
+ base: "mt-1 flex items-center justify-center w-full px-4 py-2 border border-transparent rounded-md shadow-sm text-white bg-primary hover:bg-primary-dark focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary transition-colors cursor-pointer disabled:opacity-50 disabled:cursor-not-allowed",
243
+ loading: "opacity-75",
244
+ spinner: "mr-3 -ml-1 size-5 animate-spin text-white"
245
+ },
246
+ error: "mt-1 text-sm text-red-600",
247
+ alert: {
248
+ base: "p-4 mb-4 border rounded-md",
249
+ error: "bg-red-50 text-red-800 border-red-200",
250
+ success: "bg-green-50 text-green-800 border-green-200",
251
+ warning: "bg-yellow-50 text-yellow-800 border-yellow-200",
252
+ info: "bg-blue-50 text-blue-800 border-blue-200"
253
+ },
254
+ datetime: {
255
+ input: "block w-full px-3 py-2 pr-10 border border-gray-300 rounded-md shadow-sm bg-white text-gray-900 placeholder-gray-400 focus:outline-none focus:ring focus:ring-primary focus:border-primary disabled:bg-gray-100 disabled:cursor-not-allowed cursor-pointer",
256
+ dropdown: "absolute z-50 mt-2 p-4 bg-white border border-gray-300 rounded-lg shadow-lg w-80",
257
+ picker: "absolute z-50 mt-2 p-4 bg-white border border-gray-300 rounded-lg shadow-lg w-80",
258
+ calendar: "grid grid-cols-7 gap-1 mb-4",
259
+ day: "p-2 text-sm rounded hover:bg-primary hover:text-white transition-colors text-gray-700",
260
+ daySelected: "bg-primary text-white",
261
+ dayButton: "p-2 text-sm rounded hover:bg-primary hover:text-white transition-colors text-gray-700",
262
+ dayButtonSelected: "bg-primary text-white",
263
+ timeInput: "w-14 px-2 py-1 text-center border border-gray-300 rounded bg-white text-gray-900",
264
+ timeLabel: "text-xs font-medium text-gray-700",
265
+ periodButton: "px-3 py-1 text-xs rounded bg-gray-100 text-gray-700",
266
+ periodButtonActive: "bg-primary text-white",
267
+ iconButton: "absolute right-2 top-1/2 -translate-y-1/2 text-gray-500",
268
+ navButton: "p-1 hover:bg-gray-100 rounded text-gray-700",
269
+ monthYear: "text-sm font-medium text-gray-900",
270
+ weekday: "text-center text-xs font-medium text-gray-500 p-2",
271
+ formatButton: "px-2 py-1 text-xs bg-gray-100 text-gray-700 rounded hover:bg-gray-200"
272
+ },
273
+ button: {
274
+ primary: "flex-1 px-3 py-2 text-sm bg-primary text-white rounded hover:bg-primary-dark focus:outline-none focus:ring-2 focus:ring-primary focus:ring-offset-2",
275
+ secondary: "flex-1 px-3 py-2 text-sm border border-gray-300 rounded hover:bg-gray-50 text-gray-700"
276
+ },
277
+ readonly: "mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm bg-gray-100 text-gray-900",
278
+ none: {
279
+ container: "form-group",
280
+ label: "block text-sm font-medium text-gray-700",
281
+ value: "mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm bg-gray-100 text-gray-900"
282
+ }
283
+ };
284
+ var defaultLabels = {
285
+ required: "*",
286
+ loading: "Loading...",
287
+ fileUpload: {
288
+ clickToUpload: "Click to upload",
289
+ dragAndDrop: "or drag and drop",
290
+ allowedTypes: "Allowed types:",
291
+ maxSize: "Max size:"
292
+ },
293
+ datetime: {
294
+ clear: "Clear",
295
+ done: "Done",
296
+ timeFormat: "Time Format"
297
+ },
298
+ select: {
299
+ loading: "Loading options...",
300
+ noOptions: "No options available"
301
+ }
302
+ };
303
+ var ThemeContext = createContext({
304
+ theme: defaultTheme,
305
+ labels: defaultLabels
306
+ });
307
+ function useFormTheme() {
308
+ const { theme } = useContext(ThemeContext);
309
+ return theme;
310
+ }
311
+ function useFormLabels() {
312
+ const { labels } = useContext(ThemeContext);
313
+ return labels;
314
+ }
315
+ function useFormThemeConfig() {
316
+ return useContext(ThemeContext);
317
+ }
318
+ function deepMerge(target, source) {
319
+ const result = { ...target };
320
+ for (const key in source) {
321
+ if (source[key] !== void 0) {
322
+ if (typeof source[key] === "object" && source[key] !== null && !Array.isArray(source[key]) && typeof target[key] === "object" && target[key] !== null) {
323
+ result[key] = deepMerge(
324
+ target[key],
325
+ source[key]
326
+ );
327
+ } else {
328
+ result[key] = source[key];
329
+ }
330
+ }
331
+ }
332
+ return result;
333
+ }
334
+ function FormThemeProvider({
335
+ children,
336
+ theme,
337
+ labels,
338
+ lightOnly = false
339
+ }) {
340
+ const baseTheme = lightOnly ? lightOnlyTheme : defaultTheme;
341
+ const mergedTheme = useMemo(
342
+ () => theme ? deepMerge(baseTheme, theme) : baseTheme,
343
+ [theme, baseTheme]
344
+ );
345
+ const mergedLabels = useMemo(
346
+ () => labels ? deepMerge(defaultLabels, labels) : defaultLabels,
347
+ [labels]
348
+ );
349
+ const contextValue = useMemo(
350
+ () => ({ theme: mergedTheme, labels: mergedLabels }),
351
+ [mergedTheme, mergedLabels]
352
+ );
353
+ return /* @__PURE__ */ jsx(ThemeContext.Provider, { value: contextValue, children });
354
+ }
355
+ function createTheme(customTheme, basedOn = "default") {
356
+ const baseTheme = basedOn === "lightOnly" ? lightOnlyTheme : defaultTheme;
357
+ return deepMerge(baseTheme, customTheme);
358
+ }
359
+ function cx(...classes) {
360
+ return classes.filter(Boolean).join(" ");
361
+ }
362
+ function Alert({ type, message, className = "" }) {
363
+ const theme = useFormTheme();
364
+ const alertClass = theme.alert?.[type] ?? "";
365
+ return /* @__PURE__ */ jsx(
366
+ "div",
367
+ {
368
+ className: cx(theme.alert?.base, alertClass, className),
369
+ role: "alert",
370
+ children: message
371
+ }
372
+ );
373
+ }
374
+ var Alert_default = memo(Alert);
375
+ function DateTimeInput({
376
+ label,
377
+ name,
378
+ placeholder,
379
+ defaultValue,
380
+ className,
381
+ disabled,
382
+ error,
383
+ rules,
384
+ before,
385
+ beforeClassName,
386
+ after,
387
+ afterClassName
388
+ }) {
389
+ const [selectedDate, setSelectedDate] = useState(null);
390
+ const [displayValue, setDisplayValue] = useState("");
391
+ const [showPicker, setShowPicker] = useState(false);
392
+ const [currentMonth, setCurrentMonth] = useState(/* @__PURE__ */ new Date());
393
+ const [hours, setHours] = useState("12");
394
+ const [minutes, setMinutes] = useState("00");
395
+ const [period, setPeriod] = useState("PM");
396
+ const [use24Hour, setUse24Hour] = useState(true);
397
+ const pickerRef = useRef(null);
398
+ const theme = useFormTheme();
399
+ const labels = useFormLabels();
400
+ useEffect(() => {
401
+ if (defaultValue) {
402
+ const date = new Date(defaultValue.toString());
403
+ if (!isNaN(date.getTime())) {
404
+ setSelectedDate(date);
405
+ setCurrentMonth(date);
406
+ updateDisplayValue(date);
407
+ const h24 = date.getHours();
408
+ const m = date.getMinutes();
409
+ const p = h24 >= 12 ? "PM" : "AM";
410
+ const h12 = h24 % 12 || 12;
411
+ if (use24Hour) {
412
+ setHours(String(h24).padStart(2, "0"));
413
+ } else {
414
+ setHours(String(h12).padStart(2, "0"));
415
+ }
416
+ setMinutes(String(m).padStart(2, "0"));
417
+ setPeriod(p);
418
+ }
419
+ }
420
+ }, [defaultValue, use24Hour]);
421
+ useEffect(() => {
422
+ const handleClickOutside = (event) => {
423
+ if (pickerRef.current && !pickerRef.current.contains(event.target)) {
424
+ setShowPicker(false);
425
+ }
426
+ };
427
+ if (showPicker) {
428
+ document.addEventListener("mousedown", handleClickOutside);
429
+ }
430
+ return () => {
431
+ document.removeEventListener("mousedown", handleClickOutside);
432
+ };
433
+ }, [showPicker]);
434
+ const updateDisplayValue = (date) => {
435
+ setDisplayValue(date.toLocaleString());
436
+ };
437
+ const getDaysInMonth = (date) => {
438
+ const year = date.getFullYear();
439
+ const month = date.getMonth();
440
+ const firstDay = new Date(year, month, 1);
441
+ const lastDay = new Date(year, month + 1, 0);
442
+ const daysInMonth = lastDay.getDate();
443
+ const startingDayOfWeek = firstDay.getDay();
444
+ return { daysInMonth, startingDayOfWeek };
445
+ };
446
+ const handleDateSelect = (day) => {
447
+ let hour24 = parseInt(hours, 10);
448
+ if (!use24Hour) {
449
+ if (period === "PM" && hour24 !== 12) hour24 += 12;
450
+ if (period === "AM" && hour24 === 12) hour24 = 0;
451
+ }
452
+ const newDate = new Date(
453
+ currentMonth.getFullYear(),
454
+ currentMonth.getMonth(),
455
+ day,
456
+ hour24,
457
+ parseInt(minutes, 10)
458
+ );
459
+ setSelectedDate(newDate);
460
+ updateDisplayValue(newDate);
461
+ };
462
+ const handleTimeChange = () => {
463
+ if (selectedDate) {
464
+ const newDate = new Date(selectedDate);
465
+ let h = parseInt(hours, 10);
466
+ if (!use24Hour) {
467
+ if (period === "PM" && h !== 12) h += 12;
468
+ if (period === "AM" && h === 12) h = 0;
469
+ }
470
+ newDate.setHours(h, parseInt(minutes, 10));
471
+ setSelectedDate(newDate);
472
+ updateDisplayValue(newDate);
473
+ }
474
+ };
475
+ const changeMonth = (offset) => {
476
+ setCurrentMonth(
477
+ (prev) => new Date(prev.getFullYear(), prev.getMonth() + offset, 1)
478
+ );
479
+ };
480
+ const renderCalendar = () => {
481
+ const { daysInMonth, startingDayOfWeek } = getDaysInMonth(currentMonth);
482
+ const days = [];
483
+ for (let i = 0; i < startingDayOfWeek; i++) {
484
+ days.push(/* @__PURE__ */ jsx("div", { className: "p-2" }, `empty-${i}`));
485
+ }
486
+ for (let day = 1; day <= daysInMonth; day++) {
487
+ const isSelected = selectedDate && selectedDate.getDate() === day && selectedDate.getMonth() === currentMonth.getMonth() && selectedDate.getFullYear() === currentMonth.getFullYear();
488
+ days.push(
489
+ /* @__PURE__ */ jsx(
490
+ "button",
491
+ {
492
+ type: "button",
493
+ onClick: () => handleDateSelect(day),
494
+ className: cx(
495
+ theme.datetime?.day,
496
+ isSelected && theme.datetime?.daySelected
497
+ ),
498
+ children: day
499
+ },
500
+ day
501
+ )
502
+ );
503
+ }
504
+ return days;
505
+ };
506
+ return /* @__PURE__ */ jsxs("div", { className: cx(theme.formGroup, className), ref: pickerRef, children: [
507
+ before && /* @__PURE__ */ jsx("p", { className: beforeClassName ?? "", children: before }),
508
+ /* @__PURE__ */ jsxs("label", { htmlFor: name, className: theme.label?.base, children: [
509
+ label,
510
+ " ",
511
+ rules?.required && /* @__PURE__ */ jsx("span", { className: theme.label?.required, children: "*" })
512
+ ] }),
513
+ /* @__PURE__ */ jsxs("div", { className: "relative mt-1", children: [
514
+ /* @__PURE__ */ jsx(
515
+ "input",
516
+ {
517
+ type: "text",
518
+ value: displayValue,
519
+ placeholder,
520
+ readOnly: true,
521
+ onFocus: () => !disabled && setShowPicker(true),
522
+ onClick: () => !disabled && setShowPicker(true),
523
+ className: cx(
524
+ theme.datetime?.input,
525
+ error && theme.input?.error
526
+ )
527
+ }
528
+ ),
529
+ /* @__PURE__ */ jsx(
530
+ "button",
531
+ {
532
+ type: "button",
533
+ onClick: () => !disabled && setShowPicker(true),
534
+ disabled,
535
+ className: theme.datetime?.iconButton,
536
+ children: /* @__PURE__ */ jsx(Calendar, { className: "h-5 w-5" })
537
+ }
538
+ ),
539
+ /* @__PURE__ */ jsx(
540
+ "input",
541
+ {
542
+ type: "hidden",
543
+ name,
544
+ id: name,
545
+ value: selectedDate ? selectedDate.toISOString() : "",
546
+ "aria-invalid": !!error,
547
+ "aria-describedby": error ? `${name}-error` : void 0
548
+ }
549
+ ),
550
+ showPicker && /* @__PURE__ */ jsxs("div", { className: theme.datetime?.dropdown, children: [
551
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between mb-4", children: [
552
+ /* @__PURE__ */ jsx(
553
+ "button",
554
+ {
555
+ type: "button",
556
+ onClick: () => changeMonth(-1),
557
+ className: theme.datetime?.navButton,
558
+ children: /* @__PURE__ */ jsx(ChevronLeft, { className: "w-5 h-5" })
559
+ }
560
+ ),
561
+ /* @__PURE__ */ jsx("span", { className: theme.datetime?.monthYear, children: currentMonth.toLocaleDateString("en-GB", {
562
+ month: "long",
563
+ year: "numeric"
564
+ }) }),
565
+ /* @__PURE__ */ jsx(
566
+ "button",
567
+ {
568
+ type: "button",
569
+ onClick: () => changeMonth(1),
570
+ className: theme.datetime?.navButton,
571
+ children: /* @__PURE__ */ jsx(ChevronRight, { className: "w-5 h-5" })
572
+ }
573
+ )
574
+ ] }),
575
+ /* @__PURE__ */ jsx("div", { className: "grid grid-cols-7 gap-1 mb-2", children: (labels.datetime?.weekdays ?? [
576
+ "Su",
577
+ "Mo",
578
+ "Tu",
579
+ "We",
580
+ "Th",
581
+ "Fr",
582
+ "Sa"
583
+ ]).map((day) => /* @__PURE__ */ jsx(
584
+ "div",
585
+ {
586
+ className: theme.datetime?.weekday,
587
+ children: day
588
+ },
589
+ day
590
+ )) }),
591
+ /* @__PURE__ */ jsx("div", { className: "grid grid-cols-7 gap-1 mb-4", children: renderCalendar() }),
592
+ /* @__PURE__ */ jsxs("div", { className: "border-t border-gray-200 dark:border-gray-700 pt-4", children: [
593
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between mb-3", children: [
594
+ /* @__PURE__ */ jsx("span", { className: theme.datetime?.timeLabel, children: labels.datetime?.timeFormat }),
595
+ /* @__PURE__ */ jsx(
596
+ "button",
597
+ {
598
+ type: "button",
599
+ onClick: () => {
600
+ const newFormat = !use24Hour;
601
+ setUse24Hour(newFormat);
602
+ if (selectedDate) {
603
+ const h24 = selectedDate.getHours();
604
+ if (newFormat) {
605
+ setHours(
606
+ String(h24).padStart(2, "0")
607
+ );
608
+ } else {
609
+ const h12 = h24 % 12 || 12;
610
+ setHours(
611
+ String(h12).padStart(2, "0")
612
+ );
613
+ setPeriod(
614
+ h24 >= 12 ? "PM" : "AM"
615
+ );
616
+ }
617
+ }
618
+ },
619
+ className: theme.datetime?.formatButton,
620
+ children: use24Hour ? "24hr" : "12hr"
621
+ }
622
+ )
623
+ ] }),
624
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-center gap-2", children: [
625
+ /* @__PURE__ */ jsx(Clock, { className: "h-4 w-4 text-gray-500 dark:text-gray-400" }),
626
+ /* @__PURE__ */ jsx(
627
+ "input",
628
+ {
629
+ type: "number",
630
+ min: use24Hour ? "0" : "1",
631
+ max: use24Hour ? "23" : "12",
632
+ value: hours,
633
+ onChange: (e) => {
634
+ const val = parseInt(e.target.value, 10);
635
+ const max = use24Hour ? 23 : 12;
636
+ const min = use24Hour ? 0 : 1;
637
+ if (val >= min && val <= max) {
638
+ setHours(
639
+ e.target.value.padStart(2, "0")
640
+ );
641
+ handleTimeChange();
642
+ }
643
+ },
644
+ className: theme.datetime?.timeInput
645
+ }
646
+ ),
647
+ /* @__PURE__ */ jsx("span", { className: "text-gray-900 dark:text-gray-100", children: ":" }),
648
+ /* @__PURE__ */ jsx(
649
+ "input",
650
+ {
651
+ type: "number",
652
+ min: "0",
653
+ max: "59",
654
+ value: minutes,
655
+ onChange: (e) => {
656
+ const val = parseInt(e.target.value, 10);
657
+ if (val >= 0 && val <= 59) {
658
+ setMinutes(
659
+ e.target.value.padStart(2, "0")
660
+ );
661
+ handleTimeChange();
662
+ }
663
+ },
664
+ className: theme.datetime?.timeInput
665
+ }
666
+ ),
667
+ !use24Hour && /* @__PURE__ */ jsxs("div", { className: "flex gap-1", children: [
668
+ /* @__PURE__ */ jsx(
669
+ "button",
670
+ {
671
+ type: "button",
672
+ onClick: () => {
673
+ setPeriod("AM");
674
+ handleTimeChange();
675
+ },
676
+ className: cx(
677
+ theme.datetime?.periodButton,
678
+ period === "AM" && theme.datetime?.periodButtonActive
679
+ ),
680
+ children: "AM"
681
+ }
682
+ ),
683
+ /* @__PURE__ */ jsx(
684
+ "button",
685
+ {
686
+ type: "button",
687
+ onClick: () => {
688
+ setPeriod("PM");
689
+ handleTimeChange();
690
+ },
691
+ className: cx(
692
+ theme.datetime?.periodButton,
693
+ period === "PM" && theme.datetime?.periodButtonActive
694
+ ),
695
+ children: "PM"
696
+ }
697
+ )
698
+ ] })
699
+ ] })
700
+ ] }),
701
+ /* @__PURE__ */ jsxs("div", { className: "flex gap-2 mt-4", children: [
702
+ /* @__PURE__ */ jsx(
703
+ "button",
704
+ {
705
+ type: "button",
706
+ onClick: () => {
707
+ setSelectedDate(null);
708
+ setDisplayValue("");
709
+ setShowPicker(false);
710
+ },
711
+ className: theme.button?.secondary,
712
+ children: labels.datetime?.clear
713
+ }
714
+ ),
715
+ /* @__PURE__ */ jsx(
716
+ "button",
717
+ {
718
+ type: "button",
719
+ onClick: () => setShowPicker(false),
720
+ className: theme.button?.primary,
721
+ children: labels.datetime?.done
722
+ }
723
+ )
724
+ ] })
725
+ ] })
726
+ ] }),
727
+ error && /* @__PURE__ */ jsx("p", { id: `${name}-error`, className: theme.error, children: error }),
728
+ after && /* @__PURE__ */ jsx("p", { className: afterClassName ?? "", children: after })
729
+ ] });
730
+ }
731
+ var DateTimeInput_default = memo(DateTimeInput);
732
+ function TextInput({
733
+ label,
734
+ placeholder,
735
+ name,
736
+ defaultValue,
737
+ className,
738
+ disabled,
739
+ error,
740
+ rules,
741
+ type = "text",
742
+ before,
743
+ beforeClassName,
744
+ after,
745
+ afterClassName
746
+ }) {
747
+ const [passwordVisible, setPasswordVisible] = useState(false);
748
+ const theme = useFormTheme();
749
+ const onTogglePassword = () => {
750
+ setPasswordVisible((prev) => !prev);
751
+ };
752
+ return /* @__PURE__ */ jsxs("div", { className: cx(theme.formGroup, className), children: [
753
+ before && /* @__PURE__ */ jsx("p", { className: beforeClassName ?? "", children: before }),
754
+ /* @__PURE__ */ jsxs("label", { htmlFor: name, className: theme.label?.base, children: [
755
+ label,
756
+ rules?.required && /* @__PURE__ */ jsx("span", { className: theme.label?.required, children: "*" })
757
+ ] }),
758
+ /* @__PURE__ */ jsxs("div", { className: "relative mt-1", children: [
759
+ /* @__PURE__ */ jsx(
760
+ "input",
761
+ {
762
+ type: type === "password" ? passwordVisible ? "text" : "password" : type,
763
+ name,
764
+ id: name,
765
+ placeholder,
766
+ defaultValue: defaultValue?.toString(),
767
+ disabled,
768
+ "aria-invalid": !!error,
769
+ "aria-describedby": error ? `${name}-error` : void 0,
770
+ className: cx(
771
+ theme.input?.base,
772
+ error && theme.input?.error,
773
+ theme.input?.disabled
774
+ )
775
+ }
776
+ ),
777
+ type === "password" && /* @__PURE__ */ jsxs(
778
+ "button",
779
+ {
780
+ type: "button",
781
+ onClick: onTogglePassword,
782
+ className: "absolute right-2 top-1/2 -translate-y-1/2 text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200 z-10",
783
+ "aria-label": passwordVisible ? "Hide password" : "Show password",
784
+ children: [
785
+ passwordVisible ? /* @__PURE__ */ jsx(Eye, { className: "h-5 w-5" }) : /* @__PURE__ */ jsx(EyeOff, { className: "h-5 w-5 opacity-50" }),
786
+ /* @__PURE__ */ jsx("span", { className: "sr-only", children: passwordVisible ? "Hide password" : "Show password" })
787
+ ]
788
+ }
789
+ )
790
+ ] }),
791
+ error && /* @__PURE__ */ jsx("p", { id: `${name}-error`, className: theme.error, children: error }),
792
+ after && /* @__PURE__ */ jsx("p", { className: afterClassName ?? "", children: after })
793
+ ] });
794
+ }
795
+ var TextInput_default = memo(TextInput);
796
+ function SelectInput({
797
+ label,
798
+ name,
799
+ options,
800
+ defaultValue,
801
+ className,
802
+ disabled,
803
+ error,
804
+ rules,
805
+ before,
806
+ beforeClassName,
807
+ after,
808
+ afterClassName
809
+ }) {
810
+ const theme = useFormTheme();
811
+ const labels = useFormLabels();
812
+ return /* @__PURE__ */ jsxs("div", { className: cx(theme.formGroup, className), children: [
813
+ before && /* @__PURE__ */ jsx("p", { className: beforeClassName ?? "", children: before }),
814
+ /* @__PURE__ */ jsxs("label", { htmlFor: name, className: theme.label?.base, children: [
815
+ label,
816
+ rules?.required && /* @__PURE__ */ jsx("span", { className: theme.label?.required, children: "*" })
817
+ ] }),
818
+ /* @__PURE__ */ jsx(
819
+ "select",
820
+ {
821
+ name,
822
+ id: name,
823
+ defaultValue: defaultValue?.toString(),
824
+ disabled,
825
+ "aria-invalid": !!error,
826
+ "aria-describedby": error ? `${name}-error` : void 0,
827
+ className: cx(
828
+ theme.select?.base,
829
+ error && theme.select?.error
830
+ ),
831
+ children: !options || options.length === 0 ? /* @__PURE__ */ jsx("option", { value: "", disabled: true, className: theme.select?.option, children: !options ? labels.select?.loading : labels.select?.noOptions }) : options.map((option) => /* @__PURE__ */ jsx(
832
+ "option",
833
+ {
834
+ value: option.value,
835
+ className: theme.select?.option,
836
+ children: option.label
837
+ },
838
+ option.value
839
+ ))
840
+ }
841
+ ),
842
+ error && /* @__PURE__ */ jsx("p", { id: `${name}-error`, className: theme.error, children: error }),
843
+ after && /* @__PURE__ */ jsx("p", { className: afterClassName ?? "", children: after })
844
+ ] });
845
+ }
846
+ var SelectInput_default = memo(SelectInput);
847
+ function TextAreaInput({
848
+ label,
849
+ name,
850
+ placeholder,
851
+ defaultValue,
852
+ className,
853
+ disabled,
854
+ error,
855
+ rules,
856
+ before,
857
+ beforeClassName,
858
+ after,
859
+ afterClassName
860
+ }) {
861
+ const theme = useFormTheme();
862
+ return /* @__PURE__ */ jsxs("div", { className: cx(theme.formGroup, className), children: [
863
+ before && /* @__PURE__ */ jsx("p", { className: beforeClassName ?? "", children: before }),
864
+ /* @__PURE__ */ jsxs("label", { htmlFor: name, className: theme.label?.base, children: [
865
+ label,
866
+ rules?.required && /* @__PURE__ */ jsx("span", { className: theme.label?.required, children: "*" })
867
+ ] }),
868
+ /* @__PURE__ */ jsx(
869
+ "textarea",
870
+ {
871
+ name,
872
+ id: name,
873
+ placeholder,
874
+ defaultValue: defaultValue?.toString(),
875
+ disabled,
876
+ "aria-invalid": !!error,
877
+ "aria-describedby": error ? `${name}-error` : void 0,
878
+ className: cx(
879
+ theme.textarea?.base,
880
+ error && theme.textarea?.error
881
+ ),
882
+ rows: 4
883
+ }
884
+ ),
885
+ error && /* @__PURE__ */ jsx("p", { id: `${name}-error`, className: theme.error, children: error }),
886
+ after && /* @__PURE__ */ jsx("p", { className: afterClassName ?? "", children: after })
887
+ ] });
888
+ }
889
+ var TextAreaInput_default = memo(TextAreaInput);
890
+ function CheckboxInput({
891
+ label,
892
+ name,
893
+ defaultChecked,
894
+ className,
895
+ disabled,
896
+ error,
897
+ rules,
898
+ before,
899
+ beforeClassName,
900
+ after,
901
+ afterClassName
902
+ }) {
903
+ const theme = useFormTheme();
904
+ return /* @__PURE__ */ jsxs("div", { className: cx(theme.formGroup, className), children: [
905
+ before && /* @__PURE__ */ jsx("p", { className: beforeClassName ?? "", children: before }),
906
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center", children: [
907
+ /* @__PURE__ */ jsx(
908
+ "input",
909
+ {
910
+ type: "checkbox",
911
+ name,
912
+ id: name,
913
+ defaultChecked,
914
+ disabled,
915
+ "aria-invalid": !!error,
916
+ "aria-describedby": error ? `${name}-error` : void 0,
917
+ className: cx(
918
+ theme.checkbox?.base,
919
+ error && theme.checkbox?.error
920
+ )
921
+ }
922
+ ),
923
+ /* @__PURE__ */ jsxs("label", { htmlFor: name, className: theme.checkbox?.label, children: [
924
+ label,
925
+ rules?.required && /* @__PURE__ */ jsx("span", { className: theme.label?.required, children: "*" })
926
+ ] })
927
+ ] }),
928
+ error && /* @__PURE__ */ jsx("p", { id: `${name}-error`, className: theme.error, children: error }),
929
+ after && /* @__PURE__ */ jsx("p", { className: afterClassName ?? "", children: after })
930
+ ] });
931
+ }
932
+ var CheckboxInput_default = memo(CheckboxInput);
933
+ function ToggleInput({
934
+ label,
935
+ name,
936
+ defaultChecked,
937
+ className,
938
+ disabled,
939
+ error,
940
+ rules,
941
+ before,
942
+ beforeClassName,
943
+ after,
944
+ afterClassName
945
+ }) {
946
+ const theme = useFormTheme();
947
+ return /* @__PURE__ */ jsxs("div", { className: cx(theme.formGroup, className), children: [
948
+ before && /* @__PURE__ */ jsx("p", { className: beforeClassName ?? "", children: before }),
949
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
950
+ /* @__PURE__ */ jsxs("label", { htmlFor: name, className: theme.toggle?.label, children: [
951
+ label,
952
+ rules?.required && /* @__PURE__ */ jsx("span", { className: theme.label?.required, children: "*" })
953
+ ] }),
954
+ /* @__PURE__ */ jsxs(
955
+ "label",
956
+ {
957
+ htmlFor: name,
958
+ className: "relative inline-block w-11 h-6 cursor-pointer",
959
+ children: [
960
+ /* @__PURE__ */ jsx(
961
+ "input",
962
+ {
963
+ type: "checkbox",
964
+ name,
965
+ id: name,
966
+ defaultChecked,
967
+ disabled,
968
+ "aria-invalid": !!error,
969
+ "aria-describedby": error ? `${name}-error` : void 0,
970
+ className: "sr-only peer"
971
+ }
972
+ ),
973
+ /* @__PURE__ */ jsx(
974
+ "span",
975
+ {
976
+ className: cx(
977
+ theme.toggle?.track,
978
+ !disabled && theme.toggle?.trackActive,
979
+ disabled && "cursor-not-allowed"
980
+ )
981
+ }
982
+ ),
983
+ /* @__PURE__ */ jsx("span", { className: theme.toggle?.thumb })
984
+ ]
985
+ }
986
+ )
987
+ ] }),
988
+ error && /* @__PURE__ */ jsx("p", { id: `${name}-error`, className: theme.error, children: error }),
989
+ after && /* @__PURE__ */ jsx("p", { className: afterClassName ?? "", children: after })
990
+ ] });
991
+ }
992
+ var ToggleInput_default = memo(ToggleInput);
993
+ function RadioInput({
994
+ label,
995
+ name,
996
+ options,
997
+ defaultValue,
998
+ className,
999
+ disabled,
1000
+ error,
1001
+ rules,
1002
+ before,
1003
+ beforeClassName,
1004
+ after,
1005
+ afterClassName
1006
+ }) {
1007
+ const theme = useFormTheme();
1008
+ return /* @__PURE__ */ jsxs("div", { className: cx(theme.formGroup, className), children: [
1009
+ before && /* @__PURE__ */ jsx("p", { className: beforeClassName ?? "", children: before }),
1010
+ /* @__PURE__ */ jsxs("div", { id: `${name}-group`, className: theme.label?.base, children: [
1011
+ label,
1012
+ rules?.required && /* @__PURE__ */ jsx("span", { className: theme.label?.required, children: "*" })
1013
+ ] }),
1014
+ /* @__PURE__ */ jsx(
1015
+ "div",
1016
+ {
1017
+ className: theme.radio?.group,
1018
+ role: "radiogroup",
1019
+ "aria-labelledby": `${name}-group`,
1020
+ children: options?.map((option) => /* @__PURE__ */ jsxs("div", { className: "flex items-center mb-2", children: [
1021
+ /* @__PURE__ */ jsx(
1022
+ "input",
1023
+ {
1024
+ type: "radio",
1025
+ name,
1026
+ id: `${name}-${option.value}`,
1027
+ value: option.value,
1028
+ defaultChecked: defaultValue === option.value,
1029
+ disabled,
1030
+ "aria-invalid": !!error,
1031
+ className: theme.radio?.base
1032
+ }
1033
+ ),
1034
+ /* @__PURE__ */ jsx(
1035
+ "label",
1036
+ {
1037
+ htmlFor: `${name}-${option.value}`,
1038
+ className: theme.radio?.label,
1039
+ children: option.label
1040
+ }
1041
+ )
1042
+ ] }, option.value))
1043
+ }
1044
+ ),
1045
+ error && /* @__PURE__ */ jsx("p", { id: `${name}-error`, className: theme.error, children: error }),
1046
+ after && /* @__PURE__ */ jsx("p", { className: afterClassName ?? "", children: after })
1047
+ ] });
1048
+ }
1049
+ var RadioInput_default = memo(RadioInput);
1050
+ function FileInput({
1051
+ label,
1052
+ name,
1053
+ className,
1054
+ disabled,
1055
+ error,
1056
+ rules,
1057
+ before,
1058
+ beforeClassName,
1059
+ after,
1060
+ afterClassName,
1061
+ defaultValue,
1062
+ metaData,
1063
+ uploadDir,
1064
+ baseUrl
1065
+ }) {
1066
+ const [fileName, setFileName] = useState(null);
1067
+ const [isDragging, setIsDragging] = useState(false);
1068
+ const inputRef = useRef(null);
1069
+ const theme = useFormTheme();
1070
+ const labels = useFormLabels();
1071
+ const handleChange = (e) => {
1072
+ if (e.target.files && e.target.files.length > 0) {
1073
+ setFileName(e.target.files[0].name);
1074
+ } else {
1075
+ setFileName(null);
1076
+ }
1077
+ };
1078
+ const handleDragEnter = (e) => {
1079
+ e.preventDefault();
1080
+ e.stopPropagation();
1081
+ if (!disabled) setIsDragging(true);
1082
+ };
1083
+ const handleDragLeave = (e) => {
1084
+ e.preventDefault();
1085
+ e.stopPropagation();
1086
+ setIsDragging(false);
1087
+ };
1088
+ const handleDragOver = (e) => {
1089
+ e.preventDefault();
1090
+ e.stopPropagation();
1091
+ if (!disabled) setIsDragging(true);
1092
+ };
1093
+ const handleDrop = (e) => {
1094
+ e.preventDefault();
1095
+ e.stopPropagation();
1096
+ setIsDragging(false);
1097
+ if (disabled) return;
1098
+ if (e.dataTransfer.files && e.dataTransfer.files.length > 0) {
1099
+ if (inputRef.current) {
1100
+ inputRef.current.files = e.dataTransfer.files;
1101
+ setFileName(e.dataTransfer.files[0].name);
1102
+ const event = new Event("change", { bubbles: true });
1103
+ inputRef.current.dispatchEvent(event);
1104
+ }
1105
+ }
1106
+ };
1107
+ const getFileUrl = () => {
1108
+ if (!defaultValue) return null;
1109
+ if (baseUrl && uploadDir) {
1110
+ return `${baseUrl}/${uploadDir}/${defaultValue}`;
1111
+ }
1112
+ if (baseUrl) {
1113
+ return `${baseUrl}/${defaultValue}`;
1114
+ }
1115
+ return null;
1116
+ };
1117
+ const fileUrl = getFileUrl();
1118
+ return /* @__PURE__ */ jsxs("div", { className: cx(theme.formGroup, className), children: [
1119
+ before && /* @__PURE__ */ jsx("p", { className: beforeClassName ?? "", children: before }),
1120
+ /* @__PURE__ */ jsxs("div", { className: cx(theme.label?.base, "mb-1"), children: [
1121
+ label,
1122
+ rules?.required && /* @__PURE__ */ jsx("span", { className: theme.label?.required, children: "*" })
1123
+ ] }),
1124
+ /* @__PURE__ */ jsxs("div", { className: "relative", children: [
1125
+ /* @__PURE__ */ jsx(
1126
+ "input",
1127
+ {
1128
+ ref: inputRef,
1129
+ type: "file",
1130
+ name,
1131
+ id: name,
1132
+ disabled,
1133
+ onChange: handleChange,
1134
+ "aria-invalid": !!error,
1135
+ "aria-describedby": error ? `${name}-error` : void 0,
1136
+ className: "sr-only",
1137
+ accept: rules?.mimeTypes ? rules.mimeTypes.map((type) => mimeTypes[type]).join(",") : "*"
1138
+ }
1139
+ ),
1140
+ /* @__PURE__ */ jsx(
1141
+ "label",
1142
+ {
1143
+ htmlFor: name,
1144
+ onDragEnter: handleDragEnter,
1145
+ onDragLeave: handleDragLeave,
1146
+ onDragOver: handleDragOver,
1147
+ onDrop: handleDrop,
1148
+ className: cx(
1149
+ theme.file?.dropzone,
1150
+ disabled && "cursor-not-allowed",
1151
+ isDragging && theme.file?.dropzoneActive,
1152
+ error && theme.file?.dropzoneError
1153
+ ),
1154
+ children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col items-center justify-center", children: [
1155
+ /* @__PURE__ */ jsx(
1156
+ "svg",
1157
+ {
1158
+ xmlns: "http://www.w3.org/2000/svg",
1159
+ className: theme.file?.icon,
1160
+ fill: "none",
1161
+ viewBox: "0 0 24 24",
1162
+ stroke: "currentColor",
1163
+ children: /* @__PURE__ */ jsx(
1164
+ "path",
1165
+ {
1166
+ strokeLinecap: "round",
1167
+ strokeLinejoin: "round",
1168
+ strokeWidth: 2,
1169
+ d: "M7 16a4 4 0 01-.88-7.903A5 5 0 1115.9 6L16 6a5 5 0 011 9.9M15 13l-3-3m0 0l-3 3m3-3v12"
1170
+ }
1171
+ )
1172
+ }
1173
+ ),
1174
+ /* @__PURE__ */ jsx("p", { className: theme.file?.text, children: fileName ? /* @__PURE__ */ jsx("span", { className: "font-medium text-primary", children: fileName }) : /* @__PURE__ */ jsxs(Fragment, { children: [
1175
+ /* @__PURE__ */ jsx("span", { className: "font-medium", children: labels.fileUpload?.clickToUpload }),
1176
+ " ",
1177
+ labels.fileUpload?.dragAndDrop
1178
+ ] }) }),
1179
+ rules?.mimeTypes && /* @__PURE__ */ jsxs("p", { className: theme.file?.hint, children: [
1180
+ labels.fileUpload?.allowedTypes,
1181
+ " ",
1182
+ Object.values(rules.mimeTypes).join(", ")
1183
+ ] }),
1184
+ rules?.maxFileSize && /* @__PURE__ */ jsxs("p", { className: theme.file?.hint, children: [
1185
+ labels.fileUpload?.maxSize,
1186
+ " ",
1187
+ Math.round(
1188
+ rules.maxFileSize / 1024 / 1024 * 10
1189
+ ) / 10,
1190
+ " ",
1191
+ "MB"
1192
+ ] })
1193
+ ] })
1194
+ }
1195
+ ),
1196
+ fileUrl && defaultValue && isImage(defaultValue.toString()) && /* @__PURE__ */ jsxs(Fragment, { children: [
1197
+ /* @__PURE__ */ jsx(
1198
+ "img",
1199
+ {
1200
+ src: fileUrl,
1201
+ alt: "",
1202
+ className: "mt-2 max-h-40 object-contain rounded-md border border-gray-300 dark:border-gray-600"
1203
+ }
1204
+ ),
1205
+ /* @__PURE__ */ jsxs(
1206
+ "a",
1207
+ {
1208
+ href: fileUrl,
1209
+ target: "_blank",
1210
+ rel: "noopener noreferrer",
1211
+ className: "mt-2 inline-flex items-center text-xs text-primary hover:underline dark:text-gray-50",
1212
+ children: [
1213
+ "View Current ",
1214
+ label,
1215
+ /* @__PURE__ */ jsx(
1216
+ "svg",
1217
+ {
1218
+ xmlns: "http://www.w3.org/2000/svg",
1219
+ className: "ml-1 h-4 w-4",
1220
+ fill: "none",
1221
+ viewBox: "0 0 24 24",
1222
+ stroke: "currentColor",
1223
+ children: /* @__PURE__ */ jsx(
1224
+ "path",
1225
+ {
1226
+ strokeLinecap: "round",
1227
+ strokeLinejoin: "round",
1228
+ strokeWidth: 2,
1229
+ d: "M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14"
1230
+ }
1231
+ )
1232
+ }
1233
+ )
1234
+ ]
1235
+ }
1236
+ )
1237
+ ] })
1238
+ ] }),
1239
+ error && /* @__PURE__ */ jsx("p", { id: `${name}-error`, className: theme.error, children: error }),
1240
+ after && /* @__PURE__ */ jsx("p", { className: afterClassName ?? "", children: after })
1241
+ ] });
1242
+ }
1243
+ var FileInput_default = memo(FileInput);
1244
+ function HiddenInput({
1245
+ name,
1246
+ defaultValue,
1247
+ className,
1248
+ disabled
1249
+ }) {
1250
+ return /* @__PURE__ */ jsx(
1251
+ "input",
1252
+ {
1253
+ type: "hidden",
1254
+ name,
1255
+ defaultValue: defaultValue?.toString(),
1256
+ disabled,
1257
+ className
1258
+ }
1259
+ );
1260
+ }
1261
+ function SubmitInput({
1262
+ label,
1263
+ name,
1264
+ className,
1265
+ disabled,
1266
+ loading,
1267
+ defaultValue,
1268
+ hideSpinner
1269
+ }) {
1270
+ const theme = useFormTheme();
1271
+ return /* @__PURE__ */ jsxs(
1272
+ "button",
1273
+ {
1274
+ type: "submit",
1275
+ name,
1276
+ disabled: disabled || loading,
1277
+ value: defaultValue?.toString() ?? "",
1278
+ className: cx(
1279
+ theme.submit?.base,
1280
+ loading && theme.submit?.loading,
1281
+ className
1282
+ ),
1283
+ children: [
1284
+ loading && hideSpinner !== true && /* @__PURE__ */ jsxs(
1285
+ "svg",
1286
+ {
1287
+ className: theme.submit?.spinner,
1288
+ xmlns: "http://www.w3.org/2000/svg",
1289
+ fill: "none",
1290
+ viewBox: "0 0 24 24",
1291
+ children: [
1292
+ /* @__PURE__ */ jsx(
1293
+ "circle",
1294
+ {
1295
+ className: "opacity-25",
1296
+ cx: "12",
1297
+ cy: "12",
1298
+ r: "10",
1299
+ stroke: "currentColor",
1300
+ strokeWidth: "4"
1301
+ }
1302
+ ),
1303
+ /* @__PURE__ */ jsx(
1304
+ "path",
1305
+ {
1306
+ className: "opacity-75",
1307
+ fill: "currentColor",
1308
+ d: "M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
1309
+ }
1310
+ )
1311
+ ]
1312
+ }
1313
+ ),
1314
+ label
1315
+ ]
1316
+ }
1317
+ );
1318
+ }
1319
+ function NoneInput({ label, value, className }) {
1320
+ const theme = useFormTheme();
1321
+ const isJsonString = (str) => {
1322
+ try {
1323
+ JSON.parse(str);
1324
+ return true;
1325
+ } catch {
1326
+ return false;
1327
+ }
1328
+ };
1329
+ return /* @__PURE__ */ jsxs("div", { className: cx(theme.formGroup, className), children: [
1330
+ /* @__PURE__ */ jsx("span", { className: theme.label?.base, children: label }),
1331
+ /* @__PURE__ */ jsx("div", { className: theme.readonly, children: isJsonString(value) ? /* @__PURE__ */ jsx("ul", { className: "list-disc list-inside space-y-1", children: Object.entries(JSON.parse(value)).map(([key, val]) => /* @__PURE__ */ jsxs("li", { className: "text-sm", children: [
1332
+ /* @__PURE__ */ jsxs("span", { className: "font-medium", children: [
1333
+ key,
1334
+ ":"
1335
+ ] }),
1336
+ " ",
1337
+ String(val)
1338
+ ] }, key)) }) : value })
1339
+ ] });
1340
+ }
1341
+ var NoneInput_default = memo(NoneInput);
1342
+ function Form({
1343
+ config,
1344
+ className,
1345
+ errors,
1346
+ isSubmitting = false,
1347
+ formElement: FormElement,
1348
+ formProps = {},
1349
+ fileBaseUrl
1350
+ }) {
1351
+ const theme = useFormTheme();
1352
+ const {
1353
+ method = "post",
1354
+ encType = config.encType || "application/x-www-form-urlencoded",
1355
+ action = "",
1356
+ fields
1357
+ } = config;
1358
+ const state = {
1359
+ errors,
1360
+ isSubmitting
1361
+ };
1362
+ const contextValue = {
1363
+ state,
1364
+ config
1365
+ };
1366
+ const FormTag = FormElement || "form";
1367
+ const formContent = /* @__PURE__ */ jsxs(Fragment, { children: [
1368
+ errors && "__server" in errors && errors.__server ? /* @__PURE__ */ jsx(Alert_default, { type: "error", message: errors.__server }) : null,
1369
+ /* @__PURE__ */ jsx(
1370
+ "fieldset",
1371
+ {
1372
+ className: theme.fieldset,
1373
+ disabled: isSubmitting,
1374
+ children: Object.entries(fields).map(([key, field]) => {
1375
+ const fieldError = errors?.[key];
1376
+ switch (field.type) {
1377
+ case "text":
1378
+ case "email":
1379
+ case "password":
1380
+ case "number":
1381
+ return /* @__PURE__ */ jsx(
1382
+ TextInput_default,
1383
+ {
1384
+ label: field.label,
1385
+ placeholder: field.placeholder,
1386
+ name: key,
1387
+ defaultValue: field.defaultValue,
1388
+ className: `${widthClasses[field.width || 12]} ${field.className || ""}`,
1389
+ disabled: field.disabled,
1390
+ error: fieldError,
1391
+ rules: field.rules,
1392
+ type: field.type,
1393
+ before: field.before,
1394
+ beforeClassName: field.beforeClassName,
1395
+ after: field.after,
1396
+ afterClassName: field.afterClassName
1397
+ },
1398
+ key
1399
+ );
1400
+ case "datetime":
1401
+ return /* @__PURE__ */ jsx(
1402
+ DateTimeInput_default,
1403
+ {
1404
+ label: field.label,
1405
+ placeholder: field.placeholder,
1406
+ name: key,
1407
+ defaultValue: field.defaultValue,
1408
+ className: `${widthClasses[field.width || 12]} ${field.className || ""}`,
1409
+ disabled: field.disabled,
1410
+ error: fieldError,
1411
+ rules: field.rules,
1412
+ before: field.before,
1413
+ beforeClassName: field.beforeClassName,
1414
+ after: field.after,
1415
+ afterClassName: field.afterClassName
1416
+ },
1417
+ key
1418
+ );
1419
+ case "select":
1420
+ return /* @__PURE__ */ jsx(
1421
+ SelectInput_default,
1422
+ {
1423
+ label: field.label,
1424
+ name: key,
1425
+ options: field.options,
1426
+ defaultValue: field.defaultValue,
1427
+ className: `${widthClasses[field.width || 12]} ${field.className || ""}`,
1428
+ disabled: field.disabled,
1429
+ error: fieldError,
1430
+ rules: field.rules,
1431
+ before: field.before,
1432
+ beforeClassName: field.beforeClassName,
1433
+ after: field.after,
1434
+ afterClassName: field.afterClassName
1435
+ },
1436
+ key
1437
+ );
1438
+ case "textarea":
1439
+ return /* @__PURE__ */ jsx(
1440
+ TextAreaInput_default,
1441
+ {
1442
+ label: field.label,
1443
+ placeholder: field.placeholder,
1444
+ name: key,
1445
+ defaultValue: field.defaultValue,
1446
+ className: `${widthClasses[field.width || 12]} ${field.className || ""}`,
1447
+ disabled: field.disabled,
1448
+ error: fieldError,
1449
+ rules: field.rules,
1450
+ before: field.before,
1451
+ beforeClassName: field.beforeClassName,
1452
+ after: field.after,
1453
+ afterClassName: field.afterClassName
1454
+ },
1455
+ key
1456
+ );
1457
+ case "checkbox":
1458
+ return /* @__PURE__ */ jsx(
1459
+ CheckboxInput_default,
1460
+ {
1461
+ label: field.label,
1462
+ name: key,
1463
+ defaultChecked: field.defaultChecked,
1464
+ className: `${widthClasses[field.width || 12]} ${field.className || ""}`,
1465
+ disabled: field.disabled,
1466
+ error: fieldError,
1467
+ rules: field.rules,
1468
+ before: field.before,
1469
+ beforeClassName: field.beforeClassName,
1470
+ after: field.after,
1471
+ afterClassName: field.afterClassName
1472
+ },
1473
+ key
1474
+ );
1475
+ case "toggle":
1476
+ return /* @__PURE__ */ jsx(
1477
+ ToggleInput_default,
1478
+ {
1479
+ label: field.label,
1480
+ name: key,
1481
+ defaultChecked: field.defaultChecked,
1482
+ className: `${widthClasses[field.width || 12]} ${field.className || ""}`,
1483
+ disabled: field.disabled,
1484
+ error: fieldError,
1485
+ rules: field.rules,
1486
+ before: field.before,
1487
+ beforeClassName: field.beforeClassName,
1488
+ after: field.after,
1489
+ afterClassName: field.afterClassName
1490
+ },
1491
+ key
1492
+ );
1493
+ case "radio":
1494
+ return /* @__PURE__ */ jsx(
1495
+ RadioInput_default,
1496
+ {
1497
+ label: field.label,
1498
+ name: key,
1499
+ options: field.options,
1500
+ defaultValue: field.defaultValue,
1501
+ className: `${widthClasses[field.width || 12]} ${field.className || ""}`,
1502
+ disabled: field.disabled,
1503
+ error: fieldError,
1504
+ rules: field.rules,
1505
+ before: field.before,
1506
+ beforeClassName: field.beforeClassName,
1507
+ after: field.after,
1508
+ afterClassName: field.afterClassName
1509
+ },
1510
+ key
1511
+ );
1512
+ case "file":
1513
+ return /* @__PURE__ */ jsx(
1514
+ FileInput_default,
1515
+ {
1516
+ label: field.label,
1517
+ name: key,
1518
+ className: `${widthClasses[field.width || 12]} ${field.className || ""}`,
1519
+ disabled: field.disabled,
1520
+ defaultValue: field.defaultValue,
1521
+ error: fieldError,
1522
+ rules: field.rules,
1523
+ before: field.before,
1524
+ beforeClassName: field.beforeClassName,
1525
+ after: field.after,
1526
+ afterClassName: field.afterClassName,
1527
+ metaData: field.metaData,
1528
+ uploadDir: field.uploadDir,
1529
+ baseUrl: fileBaseUrl
1530
+ },
1531
+ key
1532
+ );
1533
+ case "hidden":
1534
+ return /* @__PURE__ */ jsx(
1535
+ HiddenInput,
1536
+ {
1537
+ name: key,
1538
+ defaultValue: field.defaultValue,
1539
+ disabled: field.disabled
1540
+ },
1541
+ key
1542
+ );
1543
+ case "submit":
1544
+ return /* @__PURE__ */ jsx(
1545
+ SubmitInput,
1546
+ {
1547
+ label: field.label,
1548
+ name: key,
1549
+ className: `${widthClasses[field.width || 12]} ${field.className || ""}`,
1550
+ disabled: field.disabled,
1551
+ rules: field.rules,
1552
+ loading: isSubmitting,
1553
+ defaultValue: field.defaultValue,
1554
+ hideSpinner: field.hideSpinner
1555
+ },
1556
+ key
1557
+ );
1558
+ case "none":
1559
+ return /* @__PURE__ */ jsx(
1560
+ NoneInput_default,
1561
+ {
1562
+ className: `${widthClasses[field.width || 12]} ${field.className || ""}`,
1563
+ value: field.defaultValue?.toString() ?? "",
1564
+ label: field.label
1565
+ },
1566
+ key
1567
+ );
1568
+ default:
1569
+ return null;
1570
+ }
1571
+ })
1572
+ }
1573
+ )
1574
+ ] });
1575
+ return /* @__PURE__ */ jsx(FormContext.Provider, { value: contextValue, children: /* @__PURE__ */ jsx(
1576
+ FormTag,
1577
+ {
1578
+ method,
1579
+ encType,
1580
+ action,
1581
+ className: cx(theme.form, className),
1582
+ noValidate: true,
1583
+ ...formProps,
1584
+ children: formContent
1585
+ }
1586
+ ) });
1587
+ }
1588
+
1589
+ export { Alert_default as Alert, CheckboxInput_default as CheckboxInput, DateTimeInput_default as DateTimeInput, FileInput_default as FileInput, Form, FormContext, FormThemeProvider, HiddenInput, NoneInput_default as NoneInput, RadioInput_default as RadioInput, SelectInput_default as SelectInput, SubmitInput, TextAreaInput_default as TextAreaInput, TextInput_default as TextInput, ToggleInput_default as ToggleInput, bytesToHumanReadable, createTheme, cx, defaultLabels, defaultTheme, isImage, lightOnlyTheme, mimeTypes, useFieldError, useFormConfig, useFormContext, useFormLabels, useFormState, useFormTheme, useFormThemeConfig, widthClasses };
1590
+ //# sourceMappingURL=index.js.map
1591
+ //# sourceMappingURL=index.js.map