@glueforms/react 0.0.0-stage → 0.2.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/CHANGELOG.md +11 -0
- package/LICENSE +21 -0
- package/README.md +110 -2
- package/THIRD-PARTY-NOTICES.txt +43 -0
- package/dist/form.css +495 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +650 -0
- package/dist/index.js.map +1 -0
- package/dist/styles.css +256 -0
- package/package.json +63 -4
- package/source/index.d.ts +17 -0
- package/source/index.jsx +557 -0
- package/source/shared/dates.js +225 -0
- package/source/shared/form.css +495 -0
- package/source/shared/native.js +17 -0
- package/source/shared/ownership.js +50 -0
- package/source/shared/styles.css +256 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,650 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
+
// shared/dates.js
|
|
4
|
+
var copy = {
|
|
5
|
+
en: [
|
|
6
|
+
"Previous month",
|
|
7
|
+
"Next month",
|
|
8
|
+
"Previous year",
|
|
9
|
+
"Next year",
|
|
10
|
+
"Clear",
|
|
11
|
+
"Any date",
|
|
12
|
+
"Today and future dates",
|
|
13
|
+
"From tomorrow",
|
|
14
|
+
"No date selected",
|
|
15
|
+
"Selected: ",
|
|
16
|
+
"The previous date is no longer available and has been cleared. Choose another date.",
|
|
17
|
+
"Choose a date"
|
|
18
|
+
],
|
|
19
|
+
es: [
|
|
20
|
+
"Mes anterior",
|
|
21
|
+
"Mes siguiente",
|
|
22
|
+
"Año anterior",
|
|
23
|
+
"Año siguiente",
|
|
24
|
+
"Borrar",
|
|
25
|
+
"Cualquier fecha",
|
|
26
|
+
"Hoy y fechas futuras",
|
|
27
|
+
"A partir de mañana",
|
|
28
|
+
"Aún no has elegido una fecha",
|
|
29
|
+
"Seleccionado: ",
|
|
30
|
+
"La fecha anterior ya no está permitida y se ha borrado. Elige otra fecha.",
|
|
31
|
+
"Elige una fecha"
|
|
32
|
+
],
|
|
33
|
+
de: [
|
|
34
|
+
"Vorheriger Monat",
|
|
35
|
+
"Nächster Monat",
|
|
36
|
+
"Vorheriges Jahr",
|
|
37
|
+
"Nächstes Jahr",
|
|
38
|
+
"Löschen",
|
|
39
|
+
"Beliebiges Datum",
|
|
40
|
+
"Heute und zukünftige Daten",
|
|
41
|
+
"Ab morgen",
|
|
42
|
+
"Kein Datum ausgewählt",
|
|
43
|
+
"Ausgewählt: ",
|
|
44
|
+
"Das bisherige Datum ist nicht mehr verfügbar und wurde gelöscht. Wähle ein anderes Datum.",
|
|
45
|
+
"Datum auswählen"
|
|
46
|
+
],
|
|
47
|
+
pt: [
|
|
48
|
+
"Mês anterior",
|
|
49
|
+
"Mês seguinte",
|
|
50
|
+
"Ano anterior",
|
|
51
|
+
"Ano seguinte",
|
|
52
|
+
"Limpar",
|
|
53
|
+
"Qualquer data",
|
|
54
|
+
"Hoje e datas futuras",
|
|
55
|
+
"A partir de amanhã",
|
|
56
|
+
"Nenhuma data selecionada",
|
|
57
|
+
"Selecionado: ",
|
|
58
|
+
"A data anterior já não está disponível e foi apagada. Escolha outra data.",
|
|
59
|
+
"Escolha uma data"
|
|
60
|
+
],
|
|
61
|
+
it: [
|
|
62
|
+
"Mese precedente",
|
|
63
|
+
"Mese successivo",
|
|
64
|
+
"Anno precedente",
|
|
65
|
+
"Anno successivo",
|
|
66
|
+
"Cancella",
|
|
67
|
+
"Qualsiasi data",
|
|
68
|
+
"Oggi e date future",
|
|
69
|
+
"Da domani",
|
|
70
|
+
"Nessuna data selezionata",
|
|
71
|
+
"Selezionato: ",
|
|
72
|
+
"La data precedente non è più disponibile ed è stata cancellata. Scegli un’altra data.",
|
|
73
|
+
"Scegli una data"
|
|
74
|
+
],
|
|
75
|
+
fr: [
|
|
76
|
+
"Mois précédent",
|
|
77
|
+
"Mois suivant",
|
|
78
|
+
"Année précédente",
|
|
79
|
+
"Année suivante",
|
|
80
|
+
"Effacer",
|
|
81
|
+
"Toutes les dates",
|
|
82
|
+
"Aujourd’hui et dates futures",
|
|
83
|
+
"À partir de demain",
|
|
84
|
+
"Aucune date sélectionnée",
|
|
85
|
+
"Sélection : ",
|
|
86
|
+
"La date précédente n’est plus disponible et a été effacée. Choisissez une autre date.",
|
|
87
|
+
"Choisir une date"
|
|
88
|
+
]
|
|
89
|
+
};
|
|
90
|
+
function civil(year, month, day) {
|
|
91
|
+
const date = new Date(2e3, 0, 1, 12);
|
|
92
|
+
date.setFullYear(year, month, day);
|
|
93
|
+
return date;
|
|
94
|
+
}
|
|
95
|
+
function iso(date) {
|
|
96
|
+
return String(date.getFullYear()).padStart(4, "0") + "-" + String(date.getMonth() + 1).padStart(2, "0") + "-" + String(date.getDate()).padStart(2, "0");
|
|
97
|
+
}
|
|
98
|
+
function parse(value) {
|
|
99
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(value || "")) return null;
|
|
100
|
+
const [year, month, day] = value.split("-").map(Number);
|
|
101
|
+
if (year < 1 || year > 9999) return null;
|
|
102
|
+
const date = civil(year, month - 1, day);
|
|
103
|
+
return iso(date) === value ? date : null;
|
|
104
|
+
}
|
|
105
|
+
function today() {
|
|
106
|
+
const n = /* @__PURE__ */ new Date();
|
|
107
|
+
return civil(n.getFullYear(), n.getMonth(), n.getDate());
|
|
108
|
+
}
|
|
109
|
+
function bounds(min, max, policy, day = today()) {
|
|
110
|
+
let lower = parse(min) ? min : "";
|
|
111
|
+
const upper = parse(max) ? max : "";
|
|
112
|
+
if (policy === "today" || policy === "tomorrow") {
|
|
113
|
+
const relative = iso(civil(day.getFullYear(), day.getMonth(), day.getDate() + (policy === "tomorrow" ? 1 : 0)));
|
|
114
|
+
if (relative > lower) lower = relative;
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
min: lower,
|
|
118
|
+
max: upper,
|
|
119
|
+
empty: lower.length > 10 || !!(lower && upper && lower > upper)
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
function inBounds(value, range) {
|
|
123
|
+
return !range.empty && !!parse(value) && (!range.min || value >= range.min) && (!range.max || value <= range.max);
|
|
124
|
+
}
|
|
125
|
+
function bounded(value, range) {
|
|
126
|
+
if (range.min && value < range.min) value = range.min;
|
|
127
|
+
if (range.max && value > range.max) value = range.max;
|
|
128
|
+
return inBounds(value, range) ? value : "";
|
|
129
|
+
}
|
|
130
|
+
function monthDate(date, offset) {
|
|
131
|
+
const first = civil(date.getFullYear(), date.getMonth() + offset, 1);
|
|
132
|
+
return civil(first.getFullYear(), first.getMonth(), Math.min(date.getDate(), civil(first.getFullYear(), first.getMonth() + 1, 0).getDate()));
|
|
133
|
+
}
|
|
134
|
+
function canNavigate(display, offset, range) {
|
|
135
|
+
const next = monthDate(display, offset);
|
|
136
|
+
return next.getFullYear() >= 1 && next.getFullYear() <= 9999 && (!range.min || iso(civil(next.getFullYear(), next.getMonth() + 1, 0)) >= range.min) && (!range.max || iso(civil(next.getFullYear(), next.getMonth(), 1)) <= range.max);
|
|
137
|
+
}
|
|
138
|
+
function monthValues(display) {
|
|
139
|
+
const first = civil(display.getFullYear(), display.getMonth(), 1);
|
|
140
|
+
const offset = (first.getDay() + 6) % 7;
|
|
141
|
+
const count = Math.ceil((offset + civil(first.getFullYear(), first.getMonth() + 1, 0).getDate()) / 7) * 7;
|
|
142
|
+
return Array.from({ length: count }, (_, i) => iso(civil(first.getFullYear(), first.getMonth(), 1 - offset + i)));
|
|
143
|
+
}
|
|
144
|
+
function keyDate(value, key, shift) {
|
|
145
|
+
const date = parse(value);
|
|
146
|
+
if (!date) return null;
|
|
147
|
+
if (key === "PageUp" || key === "PageDown") return monthDate(date, (key === "PageUp" ? -1 : 1) * (shift ? 12 : 1));
|
|
148
|
+
const day = (date.getDay() + 6) % 7;
|
|
149
|
+
const delta = {
|
|
150
|
+
ArrowLeft: -1,
|
|
151
|
+
ArrowRight: 1,
|
|
152
|
+
ArrowUp: -7,
|
|
153
|
+
ArrowDown: 7,
|
|
154
|
+
Home: -day,
|
|
155
|
+
End: 6 - day
|
|
156
|
+
}[key];
|
|
157
|
+
return delta === void 0 ? null : civil(date.getFullYear(), date.getMonth(), date.getDate() + delta);
|
|
158
|
+
}
|
|
159
|
+
function language(value) {
|
|
160
|
+
const lang = String(value || "en").toLowerCase().split("-")[0];
|
|
161
|
+
return copy[lang] ? lang : "en";
|
|
162
|
+
}
|
|
163
|
+
function formats(locale) {
|
|
164
|
+
return {
|
|
165
|
+
month: new Intl.DateTimeFormat(locale, {
|
|
166
|
+
month: "long",
|
|
167
|
+
year: "numeric"
|
|
168
|
+
}),
|
|
169
|
+
full: new Intl.DateTimeFormat(locale, {
|
|
170
|
+
weekday: "long",
|
|
171
|
+
day: "numeric",
|
|
172
|
+
month: "long",
|
|
173
|
+
year: "numeric"
|
|
174
|
+
}),
|
|
175
|
+
date: new Intl.DateTimeFormat(locale, {
|
|
176
|
+
day: "numeric",
|
|
177
|
+
month: "long",
|
|
178
|
+
year: "numeric"
|
|
179
|
+
}),
|
|
180
|
+
shortWeekday: new Intl.DateTimeFormat(locale, { weekday: "short" }),
|
|
181
|
+
fullWeekday: new Intl.DateTimeFormat(locale, { weekday: "long" })
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
var navigation = [
|
|
185
|
+
[
|
|
186
|
+
-12,
|
|
187
|
+
2,
|
|
188
|
+
"m11 18-6-6 6-6m8 12-6-6 6-6"
|
|
189
|
+
],
|
|
190
|
+
[
|
|
191
|
+
-1,
|
|
192
|
+
0,
|
|
193
|
+
"m15 18-6-6 6-6"
|
|
194
|
+
],
|
|
195
|
+
[
|
|
196
|
+
1,
|
|
197
|
+
1,
|
|
198
|
+
"m9 18 6-6-6-6"
|
|
199
|
+
],
|
|
200
|
+
[
|
|
201
|
+
12,
|
|
202
|
+
3,
|
|
203
|
+
"m5 18 6-6-6-6m8 12 6-6-6-6"
|
|
204
|
+
]
|
|
205
|
+
];
|
|
206
|
+
// shared/native.js
|
|
207
|
+
function nativeSelectable(probe, value, range, step, defaultValue) {
|
|
208
|
+
if (!inBounds(value, range)) return false;
|
|
209
|
+
probe.type = "date";
|
|
210
|
+
for (const [name, candidate] of Object.entries({
|
|
211
|
+
min: range.min || "",
|
|
212
|
+
step: step ?? "",
|
|
213
|
+
value: defaultValue ?? ""
|
|
214
|
+
})) {
|
|
215
|
+
const text = String(candidate);
|
|
216
|
+
if (probe.getAttribute(name) !== text) probe.setAttribute(name, text);
|
|
217
|
+
}
|
|
218
|
+
probe.value = value;
|
|
219
|
+
return !probe.validity.stepMismatch;
|
|
220
|
+
}
|
|
221
|
+
// shared/ownership.js
|
|
222
|
+
var VERSION = "0.2.0";
|
|
223
|
+
var registryKey = Symbol.for("glueforms.public.date-fields.ownership");
|
|
224
|
+
function acquire(root, kind, create) {
|
|
225
|
+
const document = root?.nodeType === 9 ? root : root?.ownerDocument;
|
|
226
|
+
const view = document?.defaultView;
|
|
227
|
+
if (!view || !(root instanceof view.Document || root instanceof view.Element)) throw new TypeError("GlueForms init requires a Document or Element root.");
|
|
228
|
+
let registry = document[registryKey];
|
|
229
|
+
if (registry && (registry.version !== "0.2.0" || !(registry.owners instanceof Map))) throw new Error("An incompatible GlueForms version already owns this document.");
|
|
230
|
+
if (registry) for (const [other, record] of registry.owners) {
|
|
231
|
+
if (root === other && record.kind === kind && kind === "core") return record.handle;
|
|
232
|
+
if (root === other || view.Node.prototype.contains.call(root, other) || view.Node.prototype.contains.call(other, root)) throw new Error("GlueForms roots overlap. Dispose the existing owner before initializing this root.");
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
registry = {
|
|
236
|
+
version: VERSION,
|
|
237
|
+
owners: /* @__PURE__ */ new Map()
|
|
238
|
+
};
|
|
239
|
+
Object.defineProperty(document, registryKey, {
|
|
240
|
+
value: registry,
|
|
241
|
+
configurable: true
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
const release = () => {
|
|
245
|
+
registry.owners.delete(root);
|
|
246
|
+
if (!registry.owners.size && document[registryKey] === registry) delete document[registryKey];
|
|
247
|
+
};
|
|
248
|
+
const record = {
|
|
249
|
+
kind,
|
|
250
|
+
handle: null
|
|
251
|
+
};
|
|
252
|
+
registry.owners.set(root, record);
|
|
253
|
+
try {
|
|
254
|
+
record.handle = create(document, view, release);
|
|
255
|
+
return record.handle;
|
|
256
|
+
} catch (error) {
|
|
257
|
+
release();
|
|
258
|
+
throw error;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
// react/src/index.jsx
|
|
262
|
+
var { useRef, useState, useEffect, useId, useImperativeHandle, forwardRef } = React;
|
|
263
|
+
function Icon({ path }) {
|
|
264
|
+
return /* @__PURE__ */ jsx("svg", {
|
|
265
|
+
viewBox: "0 0 24 24",
|
|
266
|
+
fill: "none",
|
|
267
|
+
stroke: "currentColor",
|
|
268
|
+
strokeWidth: "2",
|
|
269
|
+
strokeLinecap: "round",
|
|
270
|
+
strokeLinejoin: "round",
|
|
271
|
+
"aria-hidden": "true",
|
|
272
|
+
focusable: "false",
|
|
273
|
+
children: /* @__PURE__ */ jsx("path", { d: path })
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
var DateField = forwardRef(function DateField(props, forwardedRef) {
|
|
277
|
+
const { appearance = "native", datePolicy = "any", value, defaultValue, onChange, onValueChange, min, max, step, ...native } = props;
|
|
278
|
+
const controlled = value !== void 0;
|
|
279
|
+
const probe = useRef(null), inputRef = useRef(null), scope = useRef(null), latest = useRef(props), mounted = useRef(false), sync = useRef(() => {}), constraint = useRef(""), boundsKey = useRef(null), previous = useRef(controlled ? value : defaultValue || ""), focusTarget = useRef("");
|
|
280
|
+
latest.current = props;
|
|
281
|
+
const [internal, setInternal] = useState(defaultValue || ""), [facts, setFacts] = useState(null), [display, setDisplay] = useState(null), [cursor, setCursor] = useState(""), [announcement, setAnnouncement] = useState("");
|
|
282
|
+
const titleId = useId();
|
|
283
|
+
useImperativeHandle(forwardedRef, () => inputRef.current, []);
|
|
284
|
+
const selected = controlled ? value : internal;
|
|
285
|
+
const range = facts?.range || {
|
|
286
|
+
min: parse(min) ? min : "",
|
|
287
|
+
max: parse(max) ? max : ""
|
|
288
|
+
};
|
|
289
|
+
const locale = facts?.locale || "en", words = copy[locale], format = formats(locale);
|
|
290
|
+
function propose(next, reason) {
|
|
291
|
+
const p = latest.current, input = inputRef.current;
|
|
292
|
+
if (!input || input.readOnly || input.matches(":disabled")) return;
|
|
293
|
+
if (next === (p.value !== void 0 ? p.value : input.value)) return;
|
|
294
|
+
if (p.value === void 0) {
|
|
295
|
+
input.value = next;
|
|
296
|
+
previous.current = next;
|
|
297
|
+
setInternal(next);
|
|
298
|
+
}
|
|
299
|
+
p.onValueChange?.(next, reason);
|
|
300
|
+
setAnnouncement(next ? copy[language(input.closest("[lang]")?.getAttribute("lang") || input.ownerDocument.documentElement.lang)][9] + formats(locale).full.format(parse(next)) : words[8]);
|
|
301
|
+
}
|
|
302
|
+
sync.current = (options = {}) => {
|
|
303
|
+
const input = inputRef.current;
|
|
304
|
+
if (!mounted.current || !input) return;
|
|
305
|
+
const p = latest.current, doc = input.ownerDocument;
|
|
306
|
+
const day = today(), nextRange = bounds(p.min, p.max, p.datePolicy, day);
|
|
307
|
+
const lang = language(input.closest("[lang]")?.getAttribute("lang") || doc.documentElement.lang);
|
|
308
|
+
const locked = input.readOnly || input.matches(":disabled");
|
|
309
|
+
const nextFacts = {
|
|
310
|
+
range: nextRange,
|
|
311
|
+
day: iso(day),
|
|
312
|
+
locale: lang,
|
|
313
|
+
locked
|
|
314
|
+
};
|
|
315
|
+
const effective = nextRange.min || p.min;
|
|
316
|
+
if (effective === void 0) input.removeAttribute("min");
|
|
317
|
+
else if (input.getAttribute("min") !== effective) input.setAttribute("min", effective);
|
|
318
|
+
setFacts((old) => JSON.stringify(old) === JSON.stringify(nextFacts) ? old : nextFacts);
|
|
319
|
+
const current = p.value !== void 0 ? p.value : input.value;
|
|
320
|
+
if (p.value === void 0) {
|
|
321
|
+
previous.current = current;
|
|
322
|
+
setInternal((old) => old === current ? old : current);
|
|
323
|
+
}
|
|
324
|
+
const tuple = JSON.stringify([
|
|
325
|
+
current,
|
|
326
|
+
nextRange.min,
|
|
327
|
+
nextRange.max
|
|
328
|
+
]);
|
|
329
|
+
const signature = JSON.stringify([nextRange.min, nextRange.max]);
|
|
330
|
+
const constraintsChanged = boundsKey.current !== signature || options.reset;
|
|
331
|
+
if (!locked) boundsKey.current = signature;
|
|
332
|
+
if (current && !inBounds(current, nextRange) && !locked) {
|
|
333
|
+
if (p.value === void 0 ? constraintsChanged : constraint.current !== tuple) {
|
|
334
|
+
constraint.current = tuple;
|
|
335
|
+
propose("", "constraint");
|
|
336
|
+
setAnnouncement(copy[lang][10]);
|
|
337
|
+
}
|
|
338
|
+
} else constraint.current = "";
|
|
339
|
+
setDisplay((old) => {
|
|
340
|
+
const anchor = bounded(current || iso(day), nextRange);
|
|
341
|
+
if (!old) return parse(anchor) || day;
|
|
342
|
+
const start = iso(civil(old.getFullYear(), old.getMonth(), 1)), end = iso(civil(old.getFullYear(), old.getMonth() + 1, 0));
|
|
343
|
+
return anchor && (nextRange.min && end < nextRange.min || nextRange.max && start > nextRange.max) ? parse(anchor) : old;
|
|
344
|
+
});
|
|
345
|
+
};
|
|
346
|
+
useEffect(() => {
|
|
347
|
+
const input = inputRef.current, root = scope.current;
|
|
348
|
+
const owner = acquire(root, "react", (doc, win, release) => {
|
|
349
|
+
const controller = new win.AbortController(), pending = /* @__PURE__ */ new Set();
|
|
350
|
+
const listen = (target, type, handler, capture = false) => win.EventTarget.prototype.addEventListener.call(target, type, handler, {
|
|
351
|
+
signal: controller.signal,
|
|
352
|
+
capture
|
|
353
|
+
});
|
|
354
|
+
mounted.current = true;
|
|
355
|
+
probe.current = doc.createElement("input");
|
|
356
|
+
const refresh = () => sync.current();
|
|
357
|
+
listen(win, "focus", refresh);
|
|
358
|
+
listen(doc, "visibilitychange", () => {
|
|
359
|
+
if (!doc.hidden) refresh();
|
|
360
|
+
});
|
|
361
|
+
listen(input, "focus", refresh);
|
|
362
|
+
const observer = new win.MutationObserver((records) => {
|
|
363
|
+
if (records.some((r) => r.target === input || r.target.matches?.("fieldset") || r.attributeName === "lang")) refresh();
|
|
364
|
+
});
|
|
365
|
+
observer.observe(doc.documentElement, {
|
|
366
|
+
subtree: true,
|
|
367
|
+
attributes: true,
|
|
368
|
+
attributeFilter: [
|
|
369
|
+
"disabled",
|
|
370
|
+
"readonly",
|
|
371
|
+
"lang"
|
|
372
|
+
]
|
|
373
|
+
});
|
|
374
|
+
let day = iso(today());
|
|
375
|
+
const timer = win.setInterval(() => {
|
|
376
|
+
const next = iso(today());
|
|
377
|
+
if (next !== day) {
|
|
378
|
+
day = next;
|
|
379
|
+
refresh();
|
|
380
|
+
}
|
|
381
|
+
}, 3e4);
|
|
382
|
+
const form = input.form;
|
|
383
|
+
if (form) {
|
|
384
|
+
const validate = (event, submitter) => {
|
|
385
|
+
refresh();
|
|
386
|
+
if (!win.Element.prototype.hasAttribute.call(form, "novalidate") && !submitter?.formNoValidate && !win.HTMLFormElement.prototype.checkValidity.call(form)) {
|
|
387
|
+
event.preventDefault();
|
|
388
|
+
event.stopImmediatePropagation();
|
|
389
|
+
win.HTMLFormElement.prototype.reportValidity.call(form);
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
listen(form, "click", (event) => {
|
|
393
|
+
const target = event.target.closest?.("button,input");
|
|
394
|
+
if (target?.form === form && ["submit", "image"].includes(target.type)) validate(event, target);
|
|
395
|
+
}, true);
|
|
396
|
+
listen(form, "keydown", (event) => {
|
|
397
|
+
if (event.key === "Enter" && event.target.matches?.("input:not([type=button]):not([type=reset])")) validate(event, ["submit", "image"].includes(event.target.type) ? event.target : Array.from(Object.getOwnPropertyDescriptor(win.HTMLFormElement.prototype, "elements").get.call(form)).find((el) => ["submit", "image"].includes(el.type) && !el.disabled));
|
|
398
|
+
}, true);
|
|
399
|
+
listen(form, "submit", (event) => validate(event, event.submitter), true);
|
|
400
|
+
listen(form, "reset", (event) => {
|
|
401
|
+
const token = win.setTimeout(() => {
|
|
402
|
+
pending.delete(token);
|
|
403
|
+
if (!mounted.current || event.defaultPrevented) return;
|
|
404
|
+
const p = latest.current;
|
|
405
|
+
if (p.value !== void 0) input.value = p.value;
|
|
406
|
+
setAnnouncement("");
|
|
407
|
+
setCursor("");
|
|
408
|
+
sync.current({ reset: true });
|
|
409
|
+
}, 0);
|
|
410
|
+
pending.add(token);
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
refresh();
|
|
414
|
+
return { dispose() {
|
|
415
|
+
mounted.current = false;
|
|
416
|
+
controller.abort();
|
|
417
|
+
observer.disconnect();
|
|
418
|
+
win.clearInterval(timer);
|
|
419
|
+
for (const token of pending) win.clearTimeout(token);
|
|
420
|
+
pending.clear();
|
|
421
|
+
release();
|
|
422
|
+
} };
|
|
423
|
+
});
|
|
424
|
+
return () => owner.dispose();
|
|
425
|
+
}, [native.form]);
|
|
426
|
+
useEffect(() => {
|
|
427
|
+
sync.current();
|
|
428
|
+
}, [
|
|
429
|
+
min,
|
|
430
|
+
max,
|
|
431
|
+
step,
|
|
432
|
+
datePolicy,
|
|
433
|
+
value,
|
|
434
|
+
defaultValue,
|
|
435
|
+
native.disabled,
|
|
436
|
+
native.readOnly,
|
|
437
|
+
native.lang,
|
|
438
|
+
appearance
|
|
439
|
+
]);
|
|
440
|
+
useEffect(() => {
|
|
441
|
+
if (focusTarget.current) {
|
|
442
|
+
const target = scope.current?.querySelector("[data-gf-date=\"" + focusTarget.current + "\"]");
|
|
443
|
+
focusTarget.current = "";
|
|
444
|
+
if (target && !target.disabled) target.focus();
|
|
445
|
+
}
|
|
446
|
+
}, [
|
|
447
|
+
cursor,
|
|
448
|
+
display,
|
|
449
|
+
facts,
|
|
450
|
+
selected
|
|
451
|
+
]);
|
|
452
|
+
function changed(event) {
|
|
453
|
+
const old = previous.current;
|
|
454
|
+
sync.current();
|
|
455
|
+
const next = event.currentTarget.value;
|
|
456
|
+
previous.current = next;
|
|
457
|
+
if (!controlled) setInternal(next);
|
|
458
|
+
if (parse(next)) {
|
|
459
|
+
setCursor(next);
|
|
460
|
+
setDisplay(parse(next));
|
|
461
|
+
}
|
|
462
|
+
const currentRange = bounds(min, max, datePolicy);
|
|
463
|
+
if (JSON.stringify([currentRange.min, currentRange.max]) === boundsKey.current) constraint.current = JSON.stringify([
|
|
464
|
+
next,
|
|
465
|
+
currentRange.min,
|
|
466
|
+
currentRange.max
|
|
467
|
+
]);
|
|
468
|
+
onChange?.(event);
|
|
469
|
+
if (next !== old) onValueChange?.(next, "input");
|
|
470
|
+
}
|
|
471
|
+
useEffect(() => {
|
|
472
|
+
previous.current = selected;
|
|
473
|
+
}, [selected]);
|
|
474
|
+
const active = facts ? display || parse(selected) : null, values = active ? monthValues(active) : [];
|
|
475
|
+
const allowed = (next) => nativeSelectable(probe.current, next, range, step, defaultValue ?? (controlled ? value : ""));
|
|
476
|
+
const focusValue = values.includes(cursor) && allowed(cursor) ? cursor : values.find(allowed);
|
|
477
|
+
function choose(next) {
|
|
478
|
+
sync.current();
|
|
479
|
+
const p = latest.current;
|
|
480
|
+
const currentRange = bounds(p.min, p.max, p.datePolicy);
|
|
481
|
+
if (next && !nativeSelectable(probe.current, next, currentRange, p.step, p.defaultValue ?? (p.value !== void 0 ? p.value : ""))) return;
|
|
482
|
+
propose(next, next ? "selection" : "clear");
|
|
483
|
+
if (next) {
|
|
484
|
+
setCursor(next);
|
|
485
|
+
setDisplay(parse(next));
|
|
486
|
+
focusTarget.current = next;
|
|
487
|
+
} else inputRef.current.focus();
|
|
488
|
+
}
|
|
489
|
+
function key(event) {
|
|
490
|
+
sync.current();
|
|
491
|
+
const date = keyDate(event.currentTarget.getAttribute("data-gf-date"), event.key, event.shiftKey);
|
|
492
|
+
if (!date) return;
|
|
493
|
+
event.preventDefault();
|
|
494
|
+
let next = bounded(iso(date), range);
|
|
495
|
+
if (!next) return;
|
|
496
|
+
const direction = next < event.currentTarget.getAttribute("data-gf-date") ? -1 : 1;
|
|
497
|
+
for (let i = 0; i < 366 && !allowed(next) && inBounds(next, range); i++) {
|
|
498
|
+
const d = parse(next);
|
|
499
|
+
next = iso(civil(d.getFullYear(), d.getMonth(), d.getDate() + direction));
|
|
500
|
+
}
|
|
501
|
+
if (!allowed(next)) return;
|
|
502
|
+
setCursor(next);
|
|
503
|
+
setDisplay(parse(next));
|
|
504
|
+
focusTarget.current = next;
|
|
505
|
+
}
|
|
506
|
+
const label = facts && (native["aria-label"] || Array.from(inputRef.current?.labels || [], (label) => label.textContent.trim()).join(" ") || words[11]);
|
|
507
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
508
|
+
ref: scope,
|
|
509
|
+
"data-gf-react-date-field": "",
|
|
510
|
+
children: [
|
|
511
|
+
/* @__PURE__ */ jsx("input", {
|
|
512
|
+
...native,
|
|
513
|
+
type: "date",
|
|
514
|
+
ref: inputRef,
|
|
515
|
+
min: facts ? range.min || min : min,
|
|
516
|
+
max,
|
|
517
|
+
step,
|
|
518
|
+
value: controlled ? value : void 0,
|
|
519
|
+
defaultValue: controlled ? void 0 : defaultValue,
|
|
520
|
+
onChange: changed
|
|
521
|
+
}),
|
|
522
|
+
facts && appearance === "calendar" && active && /* @__PURE__ */ jsxs("div", {
|
|
523
|
+
className: "gf-calendar",
|
|
524
|
+
role: "group",
|
|
525
|
+
"aria-label": label,
|
|
526
|
+
children: [
|
|
527
|
+
/* @__PURE__ */ jsxs("div", {
|
|
528
|
+
className: "gf-calendar-header",
|
|
529
|
+
children: [
|
|
530
|
+
/* @__PURE__ */ jsx("div", {
|
|
531
|
+
className: "gf-calendar-nav",
|
|
532
|
+
children: navigation.slice(0, 2).map(([offset, index, path]) => /* @__PURE__ */ jsx("button", {
|
|
533
|
+
type: "button",
|
|
534
|
+
className: "gf-calendar-nav-button",
|
|
535
|
+
"aria-label": words[index],
|
|
536
|
+
title: words[index],
|
|
537
|
+
disabled: facts.locked || !canNavigate(active, offset, range),
|
|
538
|
+
onClick: () => {
|
|
539
|
+
sync.current();
|
|
540
|
+
const next = monthDate(active, offset);
|
|
541
|
+
setDisplay(next);
|
|
542
|
+
setCursor(bounded(iso(next), range));
|
|
543
|
+
},
|
|
544
|
+
children: /* @__PURE__ */ jsx(Icon, { path })
|
|
545
|
+
}, offset))
|
|
546
|
+
}),
|
|
547
|
+
/* @__PURE__ */ jsx("span", {
|
|
548
|
+
className: "gf-calendar-month",
|
|
549
|
+
id: titleId,
|
|
550
|
+
"aria-live": "polite",
|
|
551
|
+
"aria-atomic": "true",
|
|
552
|
+
children: format.month.format(active)
|
|
553
|
+
}),
|
|
554
|
+
/* @__PURE__ */ jsx("div", {
|
|
555
|
+
className: "gf-calendar-nav",
|
|
556
|
+
children: navigation.slice(2).map(([offset, index, path]) => /* @__PURE__ */ jsx("button", {
|
|
557
|
+
type: "button",
|
|
558
|
+
className: "gf-calendar-nav-button",
|
|
559
|
+
"aria-label": words[index],
|
|
560
|
+
title: words[index],
|
|
561
|
+
disabled: facts.locked || !canNavigate(active, offset, range),
|
|
562
|
+
onClick: () => {
|
|
563
|
+
sync.current();
|
|
564
|
+
const next = monthDate(active, offset);
|
|
565
|
+
setDisplay(next);
|
|
566
|
+
setCursor(bounded(iso(next), range));
|
|
567
|
+
},
|
|
568
|
+
children: /* @__PURE__ */ jsx(Icon, { path })
|
|
569
|
+
}, offset))
|
|
570
|
+
})
|
|
571
|
+
]
|
|
572
|
+
}),
|
|
573
|
+
/* @__PURE__ */ jsxs("div", {
|
|
574
|
+
className: "gf-calendar-grid",
|
|
575
|
+
role: "grid",
|
|
576
|
+
"aria-labelledby": titleId,
|
|
577
|
+
children: [/* @__PURE__ */ jsx("div", {
|
|
578
|
+
className: "gf-calendar-row",
|
|
579
|
+
role: "row",
|
|
580
|
+
children: Array.from({ length: 7 }, (_, i) => /* @__PURE__ */ jsx("div", {
|
|
581
|
+
className: "gf-calendar-weekday",
|
|
582
|
+
role: "columnheader",
|
|
583
|
+
"aria-label": format.fullWeekday.format(civil(2024, 0, i + 1)),
|
|
584
|
+
children: format.shortWeekday.format(civil(2024, 0, i + 1))
|
|
585
|
+
}, i))
|
|
586
|
+
}), /* @__PURE__ */ jsx("div", {
|
|
587
|
+
className: "gf-calendar-days",
|
|
588
|
+
role: "rowgroup",
|
|
589
|
+
children: Array.from({ length: values.length / 7 }, (_, week) => /* @__PURE__ */ jsx("div", {
|
|
590
|
+
className: "gf-calendar-row",
|
|
591
|
+
role: "row",
|
|
592
|
+
children: values.slice(week * 7, week * 7 + 7).map((day) => {
|
|
593
|
+
const date = parse(day), disabled = facts.locked || !allowed(day);
|
|
594
|
+
return /* @__PURE__ */ jsx("div", {
|
|
595
|
+
className: "gf-calendar-cell",
|
|
596
|
+
role: "gridcell",
|
|
597
|
+
"aria-selected": day === selected,
|
|
598
|
+
"aria-disabled": disabled,
|
|
599
|
+
children: date && /* @__PURE__ */ jsx("button", {
|
|
600
|
+
type: "button",
|
|
601
|
+
className: "gf-calendar-day",
|
|
602
|
+
"data-gf-date": day,
|
|
603
|
+
"data-outside": date.getMonth() !== active.getMonth(),
|
|
604
|
+
"aria-label": format.full.format(date),
|
|
605
|
+
"aria-pressed": day === selected,
|
|
606
|
+
"aria-current": day === facts.day ? "date" : void 0,
|
|
607
|
+
tabIndex: day === focusValue && !disabled ? 0 : -1,
|
|
608
|
+
disabled,
|
|
609
|
+
onClick: () => choose(day),
|
|
610
|
+
onKeyDown: key,
|
|
611
|
+
children: date.getDate()
|
|
612
|
+
})
|
|
613
|
+
}, day);
|
|
614
|
+
})
|
|
615
|
+
}, week))
|
|
616
|
+
})]
|
|
617
|
+
}),
|
|
618
|
+
/* @__PURE__ */ jsxs("div", {
|
|
619
|
+
className: "gf-calendar-footer",
|
|
620
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
621
|
+
className: "gf-calendar-policy",
|
|
622
|
+
children: datePolicy === "today" ? words[6] : datePolicy === "tomorrow" ? words[7] : words[5]
|
|
623
|
+
}), /* @__PURE__ */ jsx("button", {
|
|
624
|
+
type: "button",
|
|
625
|
+
className: "gf-calendar-clear",
|
|
626
|
+
disabled: facts.locked || !selected,
|
|
627
|
+
onClick: () => choose(""),
|
|
628
|
+
children: words[4]
|
|
629
|
+
})]
|
|
630
|
+
}),
|
|
631
|
+
/* @__PURE__ */ jsx("div", {
|
|
632
|
+
className: "gf-calendar-selection",
|
|
633
|
+
"data-selected": !!parse(selected),
|
|
634
|
+
children: parse(selected) ? format.date.format(parse(selected)) : words[8]
|
|
635
|
+
})
|
|
636
|
+
]
|
|
637
|
+
}),
|
|
638
|
+
facts && /* @__PURE__ */ jsx("span", {
|
|
639
|
+
className: "gf-calendar-sr-only",
|
|
640
|
+
role: "status",
|
|
641
|
+
"aria-live": "polite",
|
|
642
|
+
"aria-atomic": "true",
|
|
643
|
+
children: announcement
|
|
644
|
+
})
|
|
645
|
+
]
|
|
646
|
+
});
|
|
647
|
+
});
|
|
648
|
+
export { DateField };
|
|
649
|
+
|
|
650
|
+
//# sourceMappingURL=index.js.map
|