@bookinglab/booking-ui-react 1.12.0 → 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.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,
@@ -148,30 +261,6 @@ function FormField({
148
261
  renderError()
149
262
  ] });
150
263
  case "date": {
151
- const raw = typeof value === "string" ? value : "";
152
- const match2 = raw.match(/^(\d{0,4})-?(\d{0,2})-?(\d{0,2})$/);
153
- const yearPart = match2?.[1] ?? "";
154
- const monthPart = match2?.[2] ? String(Number(match2[2])) : "";
155
- const dayPart = match2?.[3] ? String(Number(match2[3])) : "";
156
- const dayId = `${inputId}-day`;
157
- const monthId = `${inputId}-month`;
158
- const yearId = `${inputId}-year`;
159
- const buildValue = (d, m, y) => {
160
- const dt = d.trim();
161
- const mt = m.trim();
162
- const yt = y.trim();
163
- if (!dt && !mt && !yt) return "";
164
- if (/^\d{1,2}$/.test(dt) && /^\d{1,2}$/.test(mt) && /^\d{4}$/.test(yt)) {
165
- const dn = Number(dt);
166
- const mn = Number(mt);
167
- const yn = Number(yt);
168
- const date = new Date(yn, mn - 1, dn);
169
- if (date.getFullYear() === yn && date.getMonth() === mn - 1 && date.getDate() === dn) {
170
- return `${String(yn).padStart(4, "0")}-${String(mn).padStart(2, "0")}-${String(dn).padStart(2, "0")}`;
171
- }
172
- }
173
- return "";
174
- };
175
264
  const dateInputBase = classNames?.dateInput ?? classNames?.input ?? defaultInput;
176
265
  const dateInputCls = hasError ? cx(dateInputBase, classNames?.inputError ?? defaultInputError) : dateInputBase;
177
266
  const sublabelCls = classNames?.dateInputLabel ?? "block text-xs font-medium mb-1 text-gray-700";
@@ -179,85 +268,23 @@ function FormField({
179
268
  const groupCls = classNames?.dateInputGroup ?? "flex flex-row gap-3 items-end";
180
269
  const legendCls = classNames?.dateLegend ?? labelClasses;
181
270
  const fieldsetCls = classNames?.dateFieldset ?? (classNames?.fieldWrapper ?? defaultFieldWrapper);
182
- const handleSubChange = (part, v) => {
183
- const sanitized = v.replace(/[^\d]/g, "");
184
- const next = {
185
- day: part === "day" ? sanitized : dayPart,
186
- month: part === "month" ? sanitized : monthPart,
187
- year: part === "year" ? sanitized : yearPart
188
- };
189
- const combined = buildValue(next.day, next.month, next.year);
190
- onChange(combined);
191
- };
192
- return /* @__PURE__ */ jsxs(
193
- "fieldset",
271
+ return /* @__PURE__ */ jsx(
272
+ DateInputGroup,
194
273
  {
195
- className: fieldsetCls,
196
- "aria-describedby": hasError ? errorId : void 0,
197
- children: [
198
- /* @__PURE__ */ jsxs("legend", { className: legendCls, children: [
199
- question.name,
200
- question.required && /* @__PURE__ */ jsx("span", { className: "text-red-500 ml-1", "aria-hidden": "true", children: "*" })
201
- ] }),
202
- /* @__PURE__ */ jsxs("div", { className: groupCls, role: "group", "aria-labelledby": void 0, children: [
203
- /* @__PURE__ */ jsxs("div", { className: itemCls, children: [
204
- /* @__PURE__ */ jsx("label", { htmlFor: dayId, className: sublabelCls, children: "Day" }),
205
- /* @__PURE__ */ jsx(
206
- "input",
207
- {
208
- id: dayId,
209
- type: "text",
210
- inputMode: "numeric",
211
- pattern: "[0-9]*",
212
- maxLength: 2,
213
- value: dayPart,
214
- onChange: (e) => handleSubChange("day", e.target.value),
215
- className: cx(dateInputCls, "w-16"),
216
- "aria-invalid": hasError ? true : void 0,
217
- "aria-required": question.required || void 0
218
- }
219
- )
220
- ] }),
221
- /* @__PURE__ */ jsxs("div", { className: itemCls, children: [
222
- /* @__PURE__ */ jsx("label", { htmlFor: monthId, className: sublabelCls, children: "Month" }),
223
- /* @__PURE__ */ jsx(
224
- "input",
225
- {
226
- id: monthId,
227
- type: "text",
228
- inputMode: "numeric",
229
- pattern: "[0-9]*",
230
- maxLength: 2,
231
- value: monthPart,
232
- onChange: (e) => handleSubChange("month", e.target.value),
233
- className: cx(dateInputCls, "w-16"),
234
- "aria-invalid": hasError ? true : void 0,
235
- "aria-required": question.required || void 0
236
- }
237
- )
238
- ] }),
239
- /* @__PURE__ */ jsxs("div", { className: itemCls, children: [
240
- /* @__PURE__ */ jsx("label", { htmlFor: yearId, className: sublabelCls, children: "Year" }),
241
- /* @__PURE__ */ jsx(
242
- "input",
243
- {
244
- id: yearId,
245
- type: "text",
246
- inputMode: "numeric",
247
- pattern: "[0-9]*",
248
- maxLength: 4,
249
- value: yearPart,
250
- onChange: (e) => handleSubChange("year", e.target.value),
251
- className: cx(dateInputCls, "w-24"),
252
- "aria-invalid": hasError ? true : void 0,
253
- "aria-required": question.required || void 0
254
- }
255
- )
256
- ] })
257
- ] }),
258
- renderHelpText(),
259
- renderError()
260
- ]
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
261
288
  }
262
289
  );
263
290
  }