@bookinglab/booking-ui-react 1.11.2 → 1.12.1
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.d.cts +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +223 -97
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +142 -20
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,10 +1,123 @@
|
|
|
1
|
-
import { forwardRef, useId, useState, useCallback, useImperativeHandle, useEffect, useRef } from 'react';
|
|
1
|
+
import React, { forwardRef, useId, useState, useCallback, useImperativeHandle, useEffect, useRef } from 'react';
|
|
2
2
|
import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
|
|
3
3
|
|
|
4
4
|
var __defProp = Object.defineProperty;
|
|
5
5
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
6
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
7
7
|
var cx = (...classes) => classes.filter(Boolean).join(" ");
|
|
8
|
+
function DateInputGroup({
|
|
9
|
+
question,
|
|
10
|
+
value,
|
|
11
|
+
onChange,
|
|
12
|
+
inputId,
|
|
13
|
+
hasError,
|
|
14
|
+
errorId,
|
|
15
|
+
fieldsetCls,
|
|
16
|
+
legendCls,
|
|
17
|
+
groupCls,
|
|
18
|
+
itemCls,
|
|
19
|
+
sublabelCls,
|
|
20
|
+
dateInputCls,
|
|
21
|
+
renderHelpText,
|
|
22
|
+
renderError
|
|
23
|
+
}) {
|
|
24
|
+
const parseValue = (raw) => {
|
|
25
|
+
const m = raw.match(/^(\d{4})-(\d{2})-(\d{2})$/);
|
|
26
|
+
if (!m) return { day: "", month: "", year: "" };
|
|
27
|
+
return { year: m[1], month: String(Number(m[2])), day: String(Number(m[3])) };
|
|
28
|
+
};
|
|
29
|
+
const [parts, setParts] = useState(() => parseValue(value));
|
|
30
|
+
const lastEmittedRef = React.useRef(value);
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
if (value !== lastEmittedRef.current) {
|
|
33
|
+
setParts(parseValue(value));
|
|
34
|
+
lastEmittedRef.current = value;
|
|
35
|
+
}
|
|
36
|
+
}, [value]);
|
|
37
|
+
const buildValue = (d, m, y) => {
|
|
38
|
+
if (!/^\d{1,2}$/.test(d) || !/^\d{1,2}$/.test(m) || !/^\d{4}$/.test(y)) return "";
|
|
39
|
+
const dn = Number(d), mn = Number(m), yn = Number(y);
|
|
40
|
+
const date = new Date(yn, mn - 1, dn);
|
|
41
|
+
if (date.getFullYear() !== yn || date.getMonth() !== mn - 1 || date.getDate() !== dn) return "";
|
|
42
|
+
return `${String(yn).padStart(4, "0")}-${String(mn).padStart(2, "0")}-${String(dn).padStart(2, "0")}`;
|
|
43
|
+
};
|
|
44
|
+
const handleSubChange = (part, v) => {
|
|
45
|
+
const sanitized = v.replace(/[^\d]/g, "");
|
|
46
|
+
const max = part === "year" ? 4 : 2;
|
|
47
|
+
const next = { ...parts, [part]: sanitized.slice(0, max) };
|
|
48
|
+
setParts(next);
|
|
49
|
+
const combined = buildValue(next.day, next.month, next.year);
|
|
50
|
+
lastEmittedRef.current = combined;
|
|
51
|
+
onChange(combined);
|
|
52
|
+
};
|
|
53
|
+
const dayId = `${inputId}-day`;
|
|
54
|
+
const monthId = `${inputId}-month`;
|
|
55
|
+
const yearId = `${inputId}-year`;
|
|
56
|
+
return /* @__PURE__ */ jsxs("fieldset", { className: fieldsetCls, "aria-describedby": hasError ? errorId : void 0, children: [
|
|
57
|
+
/* @__PURE__ */ jsxs("legend", { className: legendCls, children: [
|
|
58
|
+
question.name,
|
|
59
|
+
question.required && /* @__PURE__ */ jsx("span", { className: "text-red-500 ml-1", "aria-hidden": "true", children: "*" })
|
|
60
|
+
] }),
|
|
61
|
+
/* @__PURE__ */ jsxs("div", { className: groupCls, role: "group", children: [
|
|
62
|
+
/* @__PURE__ */ jsxs("div", { className: itemCls, children: [
|
|
63
|
+
/* @__PURE__ */ jsx("label", { htmlFor: dayId, className: sublabelCls, children: "Day" }),
|
|
64
|
+
/* @__PURE__ */ jsx(
|
|
65
|
+
"input",
|
|
66
|
+
{
|
|
67
|
+
id: dayId,
|
|
68
|
+
type: "text",
|
|
69
|
+
inputMode: "numeric",
|
|
70
|
+
pattern: "[0-9]*",
|
|
71
|
+
maxLength: 2,
|
|
72
|
+
value: parts.day,
|
|
73
|
+
onChange: (e) => handleSubChange("day", e.target.value),
|
|
74
|
+
className: cx(dateInputCls, "w-16"),
|
|
75
|
+
"aria-invalid": hasError ? true : void 0,
|
|
76
|
+
"aria-required": question.required || void 0
|
|
77
|
+
}
|
|
78
|
+
)
|
|
79
|
+
] }),
|
|
80
|
+
/* @__PURE__ */ jsxs("div", { className: itemCls, children: [
|
|
81
|
+
/* @__PURE__ */ jsx("label", { htmlFor: monthId, className: sublabelCls, children: "Month" }),
|
|
82
|
+
/* @__PURE__ */ jsx(
|
|
83
|
+
"input",
|
|
84
|
+
{
|
|
85
|
+
id: monthId,
|
|
86
|
+
type: "text",
|
|
87
|
+
inputMode: "numeric",
|
|
88
|
+
pattern: "[0-9]*",
|
|
89
|
+
maxLength: 2,
|
|
90
|
+
value: parts.month,
|
|
91
|
+
onChange: (e) => handleSubChange("month", e.target.value),
|
|
92
|
+
className: cx(dateInputCls, "w-16"),
|
|
93
|
+
"aria-invalid": hasError ? true : void 0,
|
|
94
|
+
"aria-required": question.required || void 0
|
|
95
|
+
}
|
|
96
|
+
)
|
|
97
|
+
] }),
|
|
98
|
+
/* @__PURE__ */ jsxs("div", { className: itemCls, children: [
|
|
99
|
+
/* @__PURE__ */ jsx("label", { htmlFor: yearId, className: sublabelCls, children: "Year" }),
|
|
100
|
+
/* @__PURE__ */ jsx(
|
|
101
|
+
"input",
|
|
102
|
+
{
|
|
103
|
+
id: yearId,
|
|
104
|
+
type: "text",
|
|
105
|
+
inputMode: "numeric",
|
|
106
|
+
pattern: "[0-9]*",
|
|
107
|
+
maxLength: 4,
|
|
108
|
+
value: parts.year,
|
|
109
|
+
onChange: (e) => handleSubChange("year", e.target.value),
|
|
110
|
+
className: cx(dateInputCls, "w-24"),
|
|
111
|
+
"aria-invalid": hasError ? true : void 0,
|
|
112
|
+
"aria-required": question.required || void 0
|
|
113
|
+
}
|
|
114
|
+
)
|
|
115
|
+
] })
|
|
116
|
+
] }),
|
|
117
|
+
renderHelpText(),
|
|
118
|
+
renderError()
|
|
119
|
+
] });
|
|
120
|
+
}
|
|
8
121
|
function FormField({
|
|
9
122
|
question,
|
|
10
123
|
value,
|
|
@@ -147,25 +260,34 @@ function FormField({
|
|
|
147
260
|
renderHelpText(),
|
|
148
261
|
renderError()
|
|
149
262
|
] });
|
|
150
|
-
case "date":
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
263
|
+
case "date": {
|
|
264
|
+
const dateInputBase = classNames?.dateInput ?? classNames?.input ?? defaultInput;
|
|
265
|
+
const dateInputCls = hasError ? cx(dateInputBase, classNames?.inputError ?? defaultInputError) : dateInputBase;
|
|
266
|
+
const sublabelCls = classNames?.dateInputLabel ?? "block text-xs font-medium mb-1 text-gray-700";
|
|
267
|
+
const itemCls = classNames?.dateInputItem ?? "flex flex-col";
|
|
268
|
+
const groupCls = classNames?.dateInputGroup ?? "flex flex-row gap-3 items-end";
|
|
269
|
+
const legendCls = classNames?.dateLegend ?? labelClasses;
|
|
270
|
+
const fieldsetCls = classNames?.dateFieldset ?? (classNames?.fieldWrapper ?? defaultFieldWrapper);
|
|
271
|
+
return /* @__PURE__ */ jsx(
|
|
272
|
+
DateInputGroup,
|
|
273
|
+
{
|
|
274
|
+
question,
|
|
275
|
+
value: typeof value === "string" ? value : "",
|
|
276
|
+
onChange,
|
|
277
|
+
inputId,
|
|
278
|
+
hasError,
|
|
279
|
+
errorId,
|
|
280
|
+
fieldsetCls,
|
|
281
|
+
legendCls,
|
|
282
|
+
groupCls,
|
|
283
|
+
itemCls,
|
|
284
|
+
sublabelCls,
|
|
285
|
+
dateInputCls,
|
|
286
|
+
renderHelpText,
|
|
287
|
+
renderError
|
|
288
|
+
}
|
|
289
|
+
);
|
|
290
|
+
}
|
|
169
291
|
case "number":
|
|
170
292
|
return /* @__PURE__ */ jsxs("div", { className: classNames?.fieldWrapper ?? defaultFieldWrapper, children: [
|
|
171
293
|
renderLabel(),
|