@jirawatpyk/aura-react 4.4.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/LICENSE +30 -0
- package/README.md +68 -0
- package/dist/aura.bundle.js +4087 -0
- package/dist/cjs/index.cjs +4069 -0
- package/dist/esm/Button.js +29 -0
- package/dist/esm/Checkbox.js +49 -0
- package/dist/esm/Combobox.js +257 -0
- package/dist/esm/DataTable.js +826 -0
- package/dist/esm/DatePicker.js +575 -0
- package/dist/esm/Dialog.js +90 -0
- package/dist/esm/DropdownMenu.js +38 -0
- package/dist/esm/FileUpload.js +173 -0
- package/dist/esm/Icon.js +35 -0
- package/dist/esm/IconButton.js +18 -0
- package/dist/esm/Menu.js +98 -0
- package/dist/esm/Stat.js +45 -0
- package/dist/esm/StatusPill.js +32 -0
- package/dist/esm/Surface.js +22 -0
- package/dist/esm/ThemeStyle.js +13 -0
- package/dist/esm/TimePicker.js +302 -0
- package/dist/esm/extra.js +328 -0
- package/dist/esm/feedback.js +192 -0
- package/dist/esm/forms.js +181 -0
- package/dist/esm/index.js +89 -0
- package/dist/esm/internal.js +84 -0
- package/dist/esm/layout.js +198 -0
- package/dist/esm/layout2.js +114 -0
- package/dist/esm/locale.js +204 -0
- package/dist/esm/overlay.js +46 -0
- package/dist/esm/theme.js +186 -0
- package/dist/index.d.ts +825 -0
- package/dist/styles.css +967 -0
- package/package.json +78 -0
- package/scripts/aura-theme.mjs +24 -0
- package/src/theme.js +147 -0
|
@@ -0,0 +1,575 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import { useAuraLocale } from "./locale.js";
|
|
4
|
+
import { createPortal } from "react-dom";
|
|
5
|
+
import { cx, uid, useMaybeControlled, useMounted, useIsoLayoutEffect, trapTab, useMergedRef } from "./internal.js";
|
|
6
|
+
import { Icon } from "./Icon.js";
|
|
7
|
+
import { IconButton } from "./IconButton.js";
|
|
8
|
+
import { Field } from "./forms.js";
|
|
9
|
+
const h = React.createElement;
|
|
10
|
+
var ERA = /^พ\.ศ\.\s?|\s?(BE|พ\.ศ\.)$/g;
|
|
11
|
+
var pad = function(n) {
|
|
12
|
+
return (n < 10 ? "0" : "") + n;
|
|
13
|
+
};
|
|
14
|
+
function toISO(d) {
|
|
15
|
+
return d ? d.getFullYear() + "-" + pad(d.getMonth() + 1) + "-" + pad(d.getDate()) : null;
|
|
16
|
+
}
|
|
17
|
+
function fromISO(s) {
|
|
18
|
+
if (!s) return null;
|
|
19
|
+
var m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(s);
|
|
20
|
+
return m ? new Date(+m[1], +m[2] - 1, +m[3]) : null;
|
|
21
|
+
}
|
|
22
|
+
function addDays(d, n) {
|
|
23
|
+
return new Date(d.getFullYear(), d.getMonth(), d.getDate() + n);
|
|
24
|
+
}
|
|
25
|
+
function addMonths(d, n) {
|
|
26
|
+
var t = new Date(d.getFullYear(), d.getMonth() + n, 1);
|
|
27
|
+
var last = new Date(t.getFullYear(), t.getMonth() + 1, 0).getDate();
|
|
28
|
+
return new Date(t.getFullYear(), t.getMonth(), Math.min(d.getDate(), last));
|
|
29
|
+
}
|
|
30
|
+
function same(a, b) {
|
|
31
|
+
return !!a && !!b && a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate();
|
|
32
|
+
}
|
|
33
|
+
function localeTag(locale, calendar) {
|
|
34
|
+
return (locale === "en" ? "en-GB" : "th-TH") + "-u-ca-" + (calendar === "gregory" ? "gregory" : "buddhist");
|
|
35
|
+
}
|
|
36
|
+
var fmtCache = {};
|
|
37
|
+
var PRESETS = { short: { day: "numeric", month: "short", year: "numeric" }, long: { day: "numeric", month: "long", year: "numeric" }, numeric: { day: "2-digit", month: "2-digit", year: "numeric" } };
|
|
38
|
+
function fmt(tag, opts, d) {
|
|
39
|
+
var k = tag + JSON.stringify(opts);
|
|
40
|
+
if (!fmtCache[k]) fmtCache[k] = new Intl.DateTimeFormat(tag, opts);
|
|
41
|
+
return fmtCache[k].format(d);
|
|
42
|
+
}
|
|
43
|
+
function formatDate(iso, opts) {
|
|
44
|
+
var o = opts || {}, d = fromISO(iso);
|
|
45
|
+
if (!d) return "";
|
|
46
|
+
var f = typeof o.format === "object" ? o.format : PRESETS[o.format || "short"];
|
|
47
|
+
return fmt(localeTag(o.locale, o.calendar), f, d).replace(ERA, "");
|
|
48
|
+
}
|
|
49
|
+
var MONTHS = null;
|
|
50
|
+
function monthIndex(word) {
|
|
51
|
+
if (!MONTHS) {
|
|
52
|
+
MONTHS = {};
|
|
53
|
+
["th-TH", "en-GB"].forEach(function(tag) {
|
|
54
|
+
["short", "long"].forEach(function(w) {
|
|
55
|
+
for (var i = 0; i < 12; i++) {
|
|
56
|
+
var n = new Intl.DateTimeFormat(tag, { month: w }).format(new Date(2020, i, 1)).toLowerCase().replace(/\.$/, "");
|
|
57
|
+
MONTHS[n] = i;
|
|
58
|
+
if (/^[a-z]/.test(n)) MONTHS[n.slice(0, 3)] = i;
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
var k = word.toLowerCase().replace(/\.$/, "");
|
|
64
|
+
return MONTHS[k] != null ? MONTHS[k] : -1;
|
|
65
|
+
}
|
|
66
|
+
function parseDate(text) {
|
|
67
|
+
var s = String(text || "").trim();
|
|
68
|
+
var m = /^(\d{4})-(\d{1,2})-(\d{1,2})$/.exec(s), y, mo, d;
|
|
69
|
+
if (m) {
|
|
70
|
+
y = +m[1];
|
|
71
|
+
mo = +m[2];
|
|
72
|
+
d = +m[3];
|
|
73
|
+
} else if (m = /^(\d{1,2})[\/.\-](\d{1,2})[\/.\-](\d{4})$/.exec(s)) {
|
|
74
|
+
d = +m[1];
|
|
75
|
+
mo = +m[2];
|
|
76
|
+
y = +m[3];
|
|
77
|
+
} else if ((m = /^(\d{1,2})\s+(\S+)\s+(\d{4})$/.exec(s.replace(ERA, "").trim())) && monthIndex(m[2]) >= 0) {
|
|
78
|
+
d = +m[1];
|
|
79
|
+
mo = monthIndex(m[2]) + 1;
|
|
80
|
+
y = +m[3];
|
|
81
|
+
} else return null;
|
|
82
|
+
if (y >= 2400) y -= 543;
|
|
83
|
+
var dt = new Date(y, mo - 1, d);
|
|
84
|
+
return dt.getMonth() === mo - 1 && dt.getDate() === d ? toISO(dt) : null;
|
|
85
|
+
}
|
|
86
|
+
const Calendar = React.forwardRef(function Calendar2(props, ref) {
|
|
87
|
+
var ctx = useAuraLocale(), locale = props.locale || ctx.locale || "th", calendar = props.calendar || ctx.calendar || "buddhist", tag = localeTag(locale, calendar);
|
|
88
|
+
var weekStart = props.weekStartsOn == null ? 0 : props.weekStartsOn;
|
|
89
|
+
var today = /* @__PURE__ */ new Date();
|
|
90
|
+
today = new Date(today.getFullYear(), today.getMonth(), today.getDate());
|
|
91
|
+
var min = fromISO(props.min), max = fromISO(props.max);
|
|
92
|
+
var start = fromISO(props.start), end = fromISO(props.end);
|
|
93
|
+
var focusState = React.useState(fromISO(props.focus) || start || today);
|
|
94
|
+
var focusDate = focusState[0], setFocus = focusState[1];
|
|
95
|
+
var viewState = React.useState("days"), view = viewState[0], setView = viewState[1];
|
|
96
|
+
var hoverState = React.useState(null);
|
|
97
|
+
var gridRef = React.useRef(null), gridMerged = useMergedRef(ref, gridRef), moved = React.useRef(false);
|
|
98
|
+
var th = locale !== "en";
|
|
99
|
+
function disabled(d) {
|
|
100
|
+
return min && d < min || max && d > max || props.isDateDisabled && props.isDateDisabled(toISO(d));
|
|
101
|
+
}
|
|
102
|
+
React.useEffect(function() {
|
|
103
|
+
if (!moved.current) return;
|
|
104
|
+
moved.current = false;
|
|
105
|
+
var el = gridRef.current && gridRef.current.querySelector('[data-date="' + toISO(focusDate) + '"]');
|
|
106
|
+
if (el) el.focus();
|
|
107
|
+
});
|
|
108
|
+
React.useEffect(function() {
|
|
109
|
+
if (props.autoFocus === false) return;
|
|
110
|
+
var el = gridRef.current && gridRef.current.querySelector('[tabindex="0"]');
|
|
111
|
+
if (el) el.focus();
|
|
112
|
+
}, []);
|
|
113
|
+
function move(d) {
|
|
114
|
+
moved.current = true;
|
|
115
|
+
setFocus(d);
|
|
116
|
+
}
|
|
117
|
+
var first = new Date(focusDate.getFullYear(), focusDate.getMonth(), 1);
|
|
118
|
+
var lead = (first.getDay() - weekStart + 7) % 7;
|
|
119
|
+
var gridStart = addDays(first, -lead);
|
|
120
|
+
var days = [];
|
|
121
|
+
for (var i = 0; i < 42; i++) days.push(addDays(gridStart, i));
|
|
122
|
+
var weeks = [];
|
|
123
|
+
for (var w = 0; w < 6; w++) weeks.push(days.slice(w * 7, w * 7 + 7));
|
|
124
|
+
if (weeks[5][0].getMonth() !== focusDate.getMonth()) weeks.pop();
|
|
125
|
+
function onKey(e, d) {
|
|
126
|
+
var k = e.key, n = null;
|
|
127
|
+
if (k === "ArrowLeft") n = addDays(d, -1);
|
|
128
|
+
else if (k === "ArrowRight") n = addDays(d, 1);
|
|
129
|
+
else if (k === "ArrowUp") n = addDays(d, -7);
|
|
130
|
+
else if (k === "ArrowDown") n = addDays(d, 7);
|
|
131
|
+
else if (k === "Home") n = addDays(d, -((d.getDay() - weekStart + 7) % 7));
|
|
132
|
+
else if (k === "End") n = addDays(d, 6 - (d.getDay() - weekStart + 7) % 7);
|
|
133
|
+
else if (k === "PageUp") n = addMonths(d, e.shiftKey ? -12 : -1);
|
|
134
|
+
else if (k === "PageDown") n = addMonths(d, e.shiftKey ? 12 : 1);
|
|
135
|
+
else if (k === "Enter" || k === " ") {
|
|
136
|
+
e.preventDefault();
|
|
137
|
+
if (!disabled(d)) props.onSelect(toISO(d));
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
if (n) {
|
|
141
|
+
e.preventDefault();
|
|
142
|
+
move(n);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
var hover = hoverState[0];
|
|
146
|
+
var rangeEnd = end || (props.range && start && hover ? hover : null);
|
|
147
|
+
function inRange(d) {
|
|
148
|
+
if (!props.range || !start || !rangeEnd) return false;
|
|
149
|
+
var a = start < rangeEnd ? start : rangeEnd, b = start < rangeEnd ? rangeEnd : start;
|
|
150
|
+
return d > a && d < b;
|
|
151
|
+
}
|
|
152
|
+
var monthTitle = fmt(tag, { month: "long", year: "numeric" }, focusDate).replace(ERA, "");
|
|
153
|
+
var weekdayNames = weeks[0].map(function(d) {
|
|
154
|
+
return { short: fmt(tag, { weekday: th ? "narrow" : "short" }, d), long: fmt(tag, { weekday: "long" }, d) };
|
|
155
|
+
});
|
|
156
|
+
if (view === "years") {
|
|
157
|
+
var yr = focusDate.getFullYear(), base = yr - yr % 12;
|
|
158
|
+
var years = [];
|
|
159
|
+
for (var y = base; y < base + 12; y++) years.push(y);
|
|
160
|
+
return h(
|
|
161
|
+
"div",
|
|
162
|
+
{ className: "aura-cal", ref: gridMerged },
|
|
163
|
+
h(
|
|
164
|
+
"div",
|
|
165
|
+
{ className: "aura-cal__head" },
|
|
166
|
+
h(IconButton, { icon: "chevron-left", label: th ? "\u0E0A\u0E48\u0E27\u0E07\u0E1B\u0E35\u0E01\u0E48\u0E2D\u0E19\u0E2B\u0E19\u0E49\u0E32" : "Previous years", onClick: function() {
|
|
167
|
+
setFocus(new Date(yr - 12, focusDate.getMonth(), 1));
|
|
168
|
+
} }),
|
|
169
|
+
h(
|
|
170
|
+
"button",
|
|
171
|
+
{ type: "button", className: "aura-cal__title", onClick: function() {
|
|
172
|
+
setView("days");
|
|
173
|
+
} },
|
|
174
|
+
fmt(tag, { year: "numeric" }, new Date(base, 0, 1)).replace(ERA, "") + " \u2013 " + fmt(tag, { year: "numeric" }, new Date(base + 11, 0, 1)).replace(ERA, "")
|
|
175
|
+
),
|
|
176
|
+
h(IconButton, { icon: "chevron-right", label: th ? "\u0E0A\u0E48\u0E27\u0E07\u0E1B\u0E35\u0E16\u0E31\u0E14\u0E44\u0E1B" : "Next years", onClick: function() {
|
|
177
|
+
setFocus(new Date(yr + 12, focusDate.getMonth(), 1));
|
|
178
|
+
} })
|
|
179
|
+
),
|
|
180
|
+
h("div", { className: "aura-cal__years" }, years.map(function(y2) {
|
|
181
|
+
var d = new Date(y2, focusDate.getMonth(), 1);
|
|
182
|
+
return h(
|
|
183
|
+
"button",
|
|
184
|
+
{
|
|
185
|
+
key: y2,
|
|
186
|
+
type: "button",
|
|
187
|
+
tabIndex: y2 === yr ? 0 : -1,
|
|
188
|
+
className: cx("aura-cal__year", y2 === yr && "is-selected"),
|
|
189
|
+
onClick: function() {
|
|
190
|
+
setFocus(new Date(y2, focusDate.getMonth(), Math.min(focusDate.getDate(), 28)));
|
|
191
|
+
setView("days");
|
|
192
|
+
moved.current = true;
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
fmt(tag, { year: "numeric" }, d).replace(ERA, "")
|
|
196
|
+
);
|
|
197
|
+
}))
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
return h(
|
|
201
|
+
"div",
|
|
202
|
+
{ className: "aura-cal", ref: gridMerged },
|
|
203
|
+
h(
|
|
204
|
+
"div",
|
|
205
|
+
{ className: "aura-cal__head" },
|
|
206
|
+
h(IconButton, { icon: "chevron-left", label: th ? "\u0E40\u0E14\u0E37\u0E2D\u0E19\u0E01\u0E48\u0E2D\u0E19\u0E2B\u0E19\u0E49\u0E32" : "Previous month", onClick: function() {
|
|
207
|
+
setFocus(addMonths(focusDate, -1));
|
|
208
|
+
} }),
|
|
209
|
+
h(
|
|
210
|
+
"button",
|
|
211
|
+
{ type: "button", className: "aura-cal__title", "aria-live": "polite", onClick: function() {
|
|
212
|
+
setView("years");
|
|
213
|
+
} },
|
|
214
|
+
monthTitle,
|
|
215
|
+
h(Icon, { name: "chevron-down", size: 14 })
|
|
216
|
+
),
|
|
217
|
+
h(IconButton, { icon: "chevron-right", label: th ? "\u0E40\u0E14\u0E37\u0E2D\u0E19\u0E16\u0E31\u0E14\u0E44\u0E1B" : "Next month", onClick: function() {
|
|
218
|
+
setFocus(addMonths(focusDate, 1));
|
|
219
|
+
} })
|
|
220
|
+
),
|
|
221
|
+
h(
|
|
222
|
+
"table",
|
|
223
|
+
{ role: "grid", className: "aura-cal__grid", "aria-label": monthTitle },
|
|
224
|
+
h("thead", null, h("tr", null, weekdayNames.map(function(n, i2) {
|
|
225
|
+
return h("th", { key: i2, scope: "col", abbr: n.long }, h("span", { "aria-hidden": true }, n.short));
|
|
226
|
+
}))),
|
|
227
|
+
h("tbody", { onMouseLeave: function() {
|
|
228
|
+
hoverState[1](null);
|
|
229
|
+
} }, weeks.map(function(wk, wi) {
|
|
230
|
+
return h("tr", { key: wi }, wk.map(function(d) {
|
|
231
|
+
var iso = toISO(d), out = d.getMonth() !== focusDate.getMonth(), dis = disabled(d);
|
|
232
|
+
var isStart = same(d, start), isEnd = same(d, end) || !end && props.range && same(d, hover) && start;
|
|
233
|
+
var sel = isStart || same(d, end);
|
|
234
|
+
return h(
|
|
235
|
+
"td",
|
|
236
|
+
{
|
|
237
|
+
key: iso,
|
|
238
|
+
role: "gridcell",
|
|
239
|
+
"aria-selected": sel || void 0,
|
|
240
|
+
className: cx(inRange(d) && "is-in-range", props.range && isStart && rangeEnd && "is-range-start", props.range && isEnd && start && "is-range-end")
|
|
241
|
+
},
|
|
242
|
+
h("button", {
|
|
243
|
+
type: "button",
|
|
244
|
+
"data-date": iso,
|
|
245
|
+
tabIndex: same(d, focusDate) ? 0 : -1,
|
|
246
|
+
disabled: dis,
|
|
247
|
+
"aria-label": fmt(tag, { weekday: "long", day: "numeric", month: "long", year: "numeric" }, d),
|
|
248
|
+
"aria-current": same(d, today) ? "date" : void 0,
|
|
249
|
+
"aria-pressed": sel || void 0,
|
|
250
|
+
className: cx("aura-cal__day", out && "is-outside", sel && "is-selected", same(d, today) && "is-today"),
|
|
251
|
+
onClick: function() {
|
|
252
|
+
setFocus(d);
|
|
253
|
+
props.onSelect(iso);
|
|
254
|
+
},
|
|
255
|
+
onKeyDown: function(e) {
|
|
256
|
+
onKey(e, d);
|
|
257
|
+
},
|
|
258
|
+
onMouseEnter: function() {
|
|
259
|
+
if (props.range) hoverState[1](d);
|
|
260
|
+
}
|
|
261
|
+
}, d.getDate())
|
|
262
|
+
);
|
|
263
|
+
}));
|
|
264
|
+
}))
|
|
265
|
+
),
|
|
266
|
+
props.footer === false ? null : h(
|
|
267
|
+
"div",
|
|
268
|
+
{ className: "aura-cal__foot" },
|
|
269
|
+
h("button", { type: "button", className: "aura-cal__link", disabled: disabled(today), onClick: function() {
|
|
270
|
+
move(today);
|
|
271
|
+
props.onSelect(toISO(today));
|
|
272
|
+
} }, th ? "\u0E27\u0E31\u0E19\u0E19\u0E35\u0E49" : "Today"),
|
|
273
|
+
props.onClear ? h("button", { type: "button", className: "aura-cal__link", onClick: props.onClear }, th ? "\u0E25\u0E49\u0E32\u0E07" : "Clear") : null
|
|
274
|
+
)
|
|
275
|
+
);
|
|
276
|
+
});
|
|
277
|
+
function useCalendarPopover(boxRef) {
|
|
278
|
+
var openState = React.useState(false), open = openState[0], setOpen = openState[1];
|
|
279
|
+
var pos = React.useState(null), popRef = React.useRef(null), mounted = useMounted();
|
|
280
|
+
function place() {
|
|
281
|
+
if (!boxRef.current) return;
|
|
282
|
+
var r = boxRef.current.getBoundingClientRect(), H = 380;
|
|
283
|
+
var up = window.innerHeight - r.bottom < H && r.top > H;
|
|
284
|
+
var left = Math.max(8, Math.min(r.left, window.innerWidth - 320 - 8));
|
|
285
|
+
pos[1](up ? { left, bottom: window.innerHeight - r.top + 4 } : { left, top: r.bottom + 4 });
|
|
286
|
+
}
|
|
287
|
+
useIsoLayoutEffect(function() {
|
|
288
|
+
if (open) place();
|
|
289
|
+
}, [open]);
|
|
290
|
+
React.useEffect(function() {
|
|
291
|
+
if (!open) return;
|
|
292
|
+
function outside(e) {
|
|
293
|
+
if (boxRef.current && boxRef.current.contains(e.target)) return;
|
|
294
|
+
if (popRef.current && popRef.current.contains(e.target)) return;
|
|
295
|
+
setOpen(false);
|
|
296
|
+
}
|
|
297
|
+
document.addEventListener("pointerdown", outside, true);
|
|
298
|
+
window.addEventListener("resize", place);
|
|
299
|
+
window.addEventListener("scroll", place, true);
|
|
300
|
+
return function() {
|
|
301
|
+
document.removeEventListener("pointerdown", outside, true);
|
|
302
|
+
window.removeEventListener("resize", place);
|
|
303
|
+
window.removeEventListener("scroll", place, true);
|
|
304
|
+
};
|
|
305
|
+
}, [open]);
|
|
306
|
+
return { open, setOpen, pos: pos[0], popRef, mounted };
|
|
307
|
+
}
|
|
308
|
+
function DateField(props) {
|
|
309
|
+
var editState = React.useState(null), editing = editState[0], setEditing = editState[1];
|
|
310
|
+
return h(
|
|
311
|
+
Field,
|
|
312
|
+
{ id: props.id, label: props.label, hint: props.hint, error: props.error, required: props.required, optional: props.optional, disabled: props.disabled, className: props.className },
|
|
313
|
+
h(
|
|
314
|
+
"div",
|
|
315
|
+
{ ref: props.boxRef, className: cx("aura-input aura-date", props.open && "is-open") },
|
|
316
|
+
h("input", {
|
|
317
|
+
ref: props.inputRef,
|
|
318
|
+
id: props.id,
|
|
319
|
+
type: "text",
|
|
320
|
+
inputMode: "numeric",
|
|
321
|
+
autoComplete: "off",
|
|
322
|
+
className: "aura-input__control",
|
|
323
|
+
value: editing != null ? editing : props.display,
|
|
324
|
+
placeholder: props.placeholder,
|
|
325
|
+
disabled: props.disabled,
|
|
326
|
+
required: props.required,
|
|
327
|
+
name: props.name,
|
|
328
|
+
"aria-invalid": props.error ? true : void 0,
|
|
329
|
+
"aria-describedby": props.error ? props.id + "-error" : props.hint ? props.id + "-hint" : void 0,
|
|
330
|
+
onChange: function(e) {
|
|
331
|
+
setEditing(e.target.value);
|
|
332
|
+
},
|
|
333
|
+
onBlur: function() {
|
|
334
|
+
if (editing != null) {
|
|
335
|
+
props.onCommit(editing);
|
|
336
|
+
setEditing(null);
|
|
337
|
+
}
|
|
338
|
+
},
|
|
339
|
+
onKeyDown: function(e) {
|
|
340
|
+
if (e.key === "Enter" && editing != null) {
|
|
341
|
+
e.preventDefault();
|
|
342
|
+
props.onCommit(editing);
|
|
343
|
+
setEditing(null);
|
|
344
|
+
} else if (e.key === "ArrowDown" && (e.altKey || editing == null)) {
|
|
345
|
+
e.preventDefault();
|
|
346
|
+
props.onToggle(true);
|
|
347
|
+
} else if (e.key === "Escape" && props.open) {
|
|
348
|
+
e.preventDefault();
|
|
349
|
+
props.onToggle(false);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}),
|
|
353
|
+
props.clearable !== false && props.hasValue && !props.disabled ? h("button", { type: "button", tabIndex: -1, className: "aura-combo__clear", "aria-label": props.clearLabel, onClick: props.onClear }, h(Icon, { name: "x" })) : null,
|
|
354
|
+
h("button", {
|
|
355
|
+
type: "button",
|
|
356
|
+
className: "aura-date__toggle",
|
|
357
|
+
disabled: props.disabled,
|
|
358
|
+
"aria-label": props.toggleLabel,
|
|
359
|
+
"aria-haspopup": "dialog",
|
|
360
|
+
"aria-expanded": props.open,
|
|
361
|
+
"aria-controls": props.open ? props.dialogId : void 0,
|
|
362
|
+
onClick: function() {
|
|
363
|
+
props.onToggle(!props.open);
|
|
364
|
+
}
|
|
365
|
+
}, h(Icon, { name: "calendar" }))
|
|
366
|
+
)
|
|
367
|
+
);
|
|
368
|
+
}
|
|
369
|
+
const DatePicker = React.forwardRef(function DatePicker2(props, ref) {
|
|
370
|
+
var auto = uid(), id = props.id || auto, dialogId = id + "-cal";
|
|
371
|
+
var ctx = useAuraLocale(), locale = props.locale || ctx.locale || "th", calendar = props.calendar || ctx.calendar || "buddhist", th = locale !== "en";
|
|
372
|
+
var st = useMaybeControlled(props.value, props.defaultValue == null ? null : props.defaultValue, props.onChange);
|
|
373
|
+
var boxRef = React.useRef(null), inputRef = React.useRef(null), inputMerged = useMergedRef(ref, inputRef);
|
|
374
|
+
var pop = useCalendarPopover(boxRef);
|
|
375
|
+
function commit(text) {
|
|
376
|
+
if (!text.trim()) {
|
|
377
|
+
st[1](null);
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
var iso = parseDate(text);
|
|
381
|
+
if (iso) st[1](iso);
|
|
382
|
+
}
|
|
383
|
+
function close() {
|
|
384
|
+
pop.setOpen(false);
|
|
385
|
+
if (inputRef.current) inputRef.current.focus();
|
|
386
|
+
}
|
|
387
|
+
var cal = pop.open && pop.mounted && pop.pos ? createPortal(
|
|
388
|
+
h(
|
|
389
|
+
"div",
|
|
390
|
+
{
|
|
391
|
+
ref: pop.popRef,
|
|
392
|
+
id: dialogId,
|
|
393
|
+
role: "dialog",
|
|
394
|
+
"aria-modal": false,
|
|
395
|
+
"aria-label": props.label || (th ? "\u0E40\u0E25\u0E37\u0E2D\u0E01\u0E27\u0E31\u0E19\u0E17\u0E35\u0E48" : "Choose date"),
|
|
396
|
+
className: "aura-cal__popover",
|
|
397
|
+
style: pop.pos,
|
|
398
|
+
onKeyDown: function(e) {
|
|
399
|
+
if (e.key === "Escape") {
|
|
400
|
+
e.stopPropagation();
|
|
401
|
+
close();
|
|
402
|
+
} else trapTab(e, pop.popRef.current);
|
|
403
|
+
}
|
|
404
|
+
},
|
|
405
|
+
h(Calendar, {
|
|
406
|
+
locale,
|
|
407
|
+
calendar,
|
|
408
|
+
weekStartsOn: props.weekStartsOn,
|
|
409
|
+
min: props.min,
|
|
410
|
+
max: props.max,
|
|
411
|
+
isDateDisabled: props.isDateDisabled,
|
|
412
|
+
start: st[0],
|
|
413
|
+
focus: st[0],
|
|
414
|
+
onSelect: function(iso) {
|
|
415
|
+
st[1](iso);
|
|
416
|
+
close();
|
|
417
|
+
}
|
|
418
|
+
})
|
|
419
|
+
),
|
|
420
|
+
document.body
|
|
421
|
+
) : null;
|
|
422
|
+
return h(
|
|
423
|
+
React.Fragment,
|
|
424
|
+
null,
|
|
425
|
+
h(DateField, {
|
|
426
|
+
id,
|
|
427
|
+
dialogId,
|
|
428
|
+
label: props.label,
|
|
429
|
+
hint: props.hint,
|
|
430
|
+
error: props.error,
|
|
431
|
+
required: props.required,
|
|
432
|
+
optional: props.optional,
|
|
433
|
+
disabled: props.disabled,
|
|
434
|
+
className: props.className,
|
|
435
|
+
name: props.name,
|
|
436
|
+
boxRef,
|
|
437
|
+
inputRef: inputMerged,
|
|
438
|
+
open: pop.open,
|
|
439
|
+
display: formatDate(st[0], { locale, calendar }),
|
|
440
|
+
placeholder: props.placeholder || (th ? "\u0E27\u0E27/\u0E14\u0E14/\u0E1B\u0E1B\u0E1B\u0E1B" : "dd/mm/yyyy"),
|
|
441
|
+
hasValue: st[0] != null,
|
|
442
|
+
clearable: props.clearable,
|
|
443
|
+
onClear: function() {
|
|
444
|
+
st[1](null);
|
|
445
|
+
inputRef.current && inputRef.current.focus();
|
|
446
|
+
},
|
|
447
|
+
clearLabel: th ? "\u0E25\u0E49\u0E32\u0E07\u0E27\u0E31\u0E19\u0E17\u0E35\u0E48" : "Clear date",
|
|
448
|
+
toggleLabel: th ? "\u0E40\u0E1B\u0E34\u0E14\u0E1B\u0E0F\u0E34\u0E17\u0E34\u0E19" : "Open calendar",
|
|
449
|
+
onCommit: commit,
|
|
450
|
+
onToggle: function(o) {
|
|
451
|
+
pop.setOpen(o);
|
|
452
|
+
if (!o && inputRef.current) inputRef.current.focus();
|
|
453
|
+
}
|
|
454
|
+
}),
|
|
455
|
+
cal
|
|
456
|
+
);
|
|
457
|
+
});
|
|
458
|
+
const DateRangePicker = React.forwardRef(function DateRangePicker2(props, ref) {
|
|
459
|
+
var auto = uid(), id = props.id || auto, dialogId = id + "-cal";
|
|
460
|
+
var ctx = useAuraLocale(), locale = props.locale || ctx.locale || "th", calendar = props.calendar || ctx.calendar || "buddhist", th = locale !== "en";
|
|
461
|
+
var st = useMaybeControlled(props.value, props.defaultValue || { start: null, end: null }, props.onChange);
|
|
462
|
+
var v = st[0] || { start: null, end: null };
|
|
463
|
+
var draft = React.useState(null);
|
|
464
|
+
var boxRef = React.useRef(null), inputRef = React.useRef(null), inputMerged = useMergedRef(ref, inputRef);
|
|
465
|
+
var pop = useCalendarPopover(boxRef);
|
|
466
|
+
var o = { locale, calendar };
|
|
467
|
+
function show(r) {
|
|
468
|
+
if (!r.start) return "";
|
|
469
|
+
if (!r.end) return formatDate(r.start, o) + " \u2013";
|
|
470
|
+
return formatDate(r.start, o) + " \u2013 " + formatDate(r.end, o);
|
|
471
|
+
}
|
|
472
|
+
function commit(text) {
|
|
473
|
+
var parts = String(text).split(/\s[–-]\s|\s*–\s*/);
|
|
474
|
+
if (!text.trim()) {
|
|
475
|
+
st[1]({ start: null, end: null });
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
var a = parseDate(parts[0]), b = parseDate(parts[1] || "");
|
|
479
|
+
if (a && b) st[1](a <= b ? { start: a, end: b } : { start: b, end: a });
|
|
480
|
+
}
|
|
481
|
+
function close() {
|
|
482
|
+
pop.setOpen(false);
|
|
483
|
+
draft[1](null);
|
|
484
|
+
if (inputRef.current) inputRef.current.focus();
|
|
485
|
+
}
|
|
486
|
+
function pick(iso) {
|
|
487
|
+
if (!draft[0]) {
|
|
488
|
+
draft[1](iso);
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
var a = draft[0], b = iso;
|
|
492
|
+
st[1](a <= b ? { start: a, end: b } : { start: b, end: a });
|
|
493
|
+
close();
|
|
494
|
+
}
|
|
495
|
+
var cal = pop.open && pop.mounted && pop.pos ? createPortal(
|
|
496
|
+
h(
|
|
497
|
+
"div",
|
|
498
|
+
{
|
|
499
|
+
ref: pop.popRef,
|
|
500
|
+
id: dialogId,
|
|
501
|
+
role: "dialog",
|
|
502
|
+
"aria-modal": false,
|
|
503
|
+
"aria-label": props.label || (th ? "\u0E40\u0E25\u0E37\u0E2D\u0E01\u0E0A\u0E48\u0E27\u0E07\u0E27\u0E31\u0E19\u0E17\u0E35\u0E48" : "Choose dates"),
|
|
504
|
+
className: "aura-cal__popover",
|
|
505
|
+
style: pop.pos,
|
|
506
|
+
onKeyDown: function(e) {
|
|
507
|
+
if (e.key === "Escape") {
|
|
508
|
+
e.stopPropagation();
|
|
509
|
+
close();
|
|
510
|
+
} else trapTab(e, pop.popRef.current);
|
|
511
|
+
}
|
|
512
|
+
},
|
|
513
|
+
h("p", { className: "aura-cal__hint", "aria-live": "polite" }, draft[0] ? th ? "\u0E40\u0E25\u0E37\u0E2D\u0E01\u0E27\u0E31\u0E19\u0E2A\u0E34\u0E49\u0E19\u0E2A\u0E38\u0E14" : "Choose the end date" : th ? "\u0E40\u0E25\u0E37\u0E2D\u0E01\u0E27\u0E31\u0E19\u0E40\u0E23\u0E34\u0E48\u0E21\u0E15\u0E49\u0E19" : "Choose the start date"),
|
|
514
|
+
h(Calendar, {
|
|
515
|
+
range: true,
|
|
516
|
+
locale,
|
|
517
|
+
calendar,
|
|
518
|
+
weekStartsOn: props.weekStartsOn,
|
|
519
|
+
min: props.min,
|
|
520
|
+
max: props.max,
|
|
521
|
+
isDateDisabled: props.isDateDisabled,
|
|
522
|
+
start: draft[0] || v.start,
|
|
523
|
+
end: draft[0] ? null : v.end,
|
|
524
|
+
focus: draft[0] || v.start,
|
|
525
|
+
onSelect: pick
|
|
526
|
+
})
|
|
527
|
+
),
|
|
528
|
+
document.body
|
|
529
|
+
) : null;
|
|
530
|
+
return h(
|
|
531
|
+
React.Fragment,
|
|
532
|
+
null,
|
|
533
|
+
h(DateField, {
|
|
534
|
+
id,
|
|
535
|
+
dialogId,
|
|
536
|
+
label: props.label,
|
|
537
|
+
hint: props.hint,
|
|
538
|
+
error: props.error,
|
|
539
|
+
required: props.required,
|
|
540
|
+
optional: props.optional,
|
|
541
|
+
disabled: props.disabled,
|
|
542
|
+
className: props.className,
|
|
543
|
+
name: props.name,
|
|
544
|
+
boxRef,
|
|
545
|
+
inputRef: inputMerged,
|
|
546
|
+
open: pop.open,
|
|
547
|
+
display: show(v),
|
|
548
|
+
placeholder: props.placeholder || (th ? "\u0E27\u0E27/\u0E14\u0E14/\u0E1B\u0E1B\u0E1B\u0E1B \u2013 \u0E27\u0E27/\u0E14\u0E14/\u0E1B\u0E1B\u0E1B\u0E1B" : "dd/mm/yyyy \u2013 dd/mm/yyyy"),
|
|
549
|
+
hasValue: v.start != null,
|
|
550
|
+
clearable: props.clearable,
|
|
551
|
+
onClear: function() {
|
|
552
|
+
st[1]({ start: null, end: null });
|
|
553
|
+
inputRef.current && inputRef.current.focus();
|
|
554
|
+
},
|
|
555
|
+
clearLabel: th ? "\u0E25\u0E49\u0E32\u0E07\u0E0A\u0E48\u0E27\u0E07\u0E27\u0E31\u0E19\u0E17\u0E35\u0E48" : "Clear dates",
|
|
556
|
+
toggleLabel: th ? "\u0E40\u0E1B\u0E34\u0E14\u0E1B\u0E0F\u0E34\u0E17\u0E34\u0E19" : "Open calendar",
|
|
557
|
+
onCommit: commit,
|
|
558
|
+
onToggle: function(op) {
|
|
559
|
+
pop.setOpen(op);
|
|
560
|
+
draft[1](null);
|
|
561
|
+
if (!op && inputRef.current) inputRef.current.focus();
|
|
562
|
+
}
|
|
563
|
+
}),
|
|
564
|
+
cal
|
|
565
|
+
);
|
|
566
|
+
});
|
|
567
|
+
export {
|
|
568
|
+
Calendar,
|
|
569
|
+
DatePicker,
|
|
570
|
+
DateRangePicker,
|
|
571
|
+
formatDate,
|
|
572
|
+
fromISO,
|
|
573
|
+
parseDate,
|
|
574
|
+
toISO
|
|
575
|
+
};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import { useStrings, useAuraLocale } from "./locale.js";
|
|
4
|
+
import { createPortal } from "react-dom";
|
|
5
|
+
import { cx, uid, useMergedRef } from "./internal.js";
|
|
6
|
+
import { IconButton } from "./IconButton.js";
|
|
7
|
+
import { useModal } from "./overlay.js";
|
|
8
|
+
const h = React.createElement;
|
|
9
|
+
const Dialog = React.forwardRef(function Dialog2(props, ref) {
|
|
10
|
+
var t = useStrings();
|
|
11
|
+
var own = React.useRef(null), merged = useMergedRef(ref, own), titleId = uid(), descId = uid();
|
|
12
|
+
function close() {
|
|
13
|
+
if (props.dismissible !== false && props.onClose) props.onClose();
|
|
14
|
+
}
|
|
15
|
+
var modal = useModal(props.open, own, { autoFocus: props.autoFocus, onEscape: close, bodySelector: ".aura-dialog__body", footSelector: ".aura-dialog__foot" });
|
|
16
|
+
if (!modal.ready) return null;
|
|
17
|
+
return createPortal(
|
|
18
|
+
h(
|
|
19
|
+
"div",
|
|
20
|
+
{ className: "aura-dialog-layer", onKeyDown: modal.onKeyDown },
|
|
21
|
+
h("div", { className: "aura-scrim", onClick: close, "aria-hidden": true }),
|
|
22
|
+
h(
|
|
23
|
+
"div",
|
|
24
|
+
{
|
|
25
|
+
ref: merged,
|
|
26
|
+
role: props.role || "dialog",
|
|
27
|
+
"aria-modal": true,
|
|
28
|
+
"aria-labelledby": titleId,
|
|
29
|
+
"aria-describedby": props.description ? descId : void 0,
|
|
30
|
+
tabIndex: -1,
|
|
31
|
+
className: cx("aura-dialog", "aura-dialog--" + (props.size || "md"), props.className)
|
|
32
|
+
},
|
|
33
|
+
h(
|
|
34
|
+
"div",
|
|
35
|
+
{ className: "aura-dialog__head" },
|
|
36
|
+
h("h2", { className: "aura-dialog__title", id: titleId }, props.title),
|
|
37
|
+
props.dismissible !== false ? h(IconButton, { icon: "x", label: t.close, className: "aura-dialog__close", onClick: close }) : null
|
|
38
|
+
),
|
|
39
|
+
props.description ? h("p", { className: "aura-dialog__desc", id: descId }, props.description) : null,
|
|
40
|
+
props.children ? h("div", { className: "aura-dialog__body" }, props.children) : null,
|
|
41
|
+
props.footer ? h("div", { className: "aura-dialog__foot" }, props.footer) : null
|
|
42
|
+
)
|
|
43
|
+
),
|
|
44
|
+
document.body
|
|
45
|
+
);
|
|
46
|
+
});
|
|
47
|
+
const Drawer = React.forwardRef(function Drawer2(props, ref) {
|
|
48
|
+
var t = useStrings();
|
|
49
|
+
var own = React.useRef(null), merged = useMergedRef(ref, own), titleId = uid(), descId = uid();
|
|
50
|
+
function close() {
|
|
51
|
+
if (props.dismissible !== false && props.onClose) props.onClose();
|
|
52
|
+
}
|
|
53
|
+
var modal = useModal(props.open, own, { autoFocus: props.autoFocus, onEscape: close, bodySelector: ".aura-drawer__body", footSelector: ".aura-drawer__foot" });
|
|
54
|
+
if (!modal.ready) return null;
|
|
55
|
+
var side = props.side === "left" ? "left" : "right";
|
|
56
|
+
return createPortal(
|
|
57
|
+
h(
|
|
58
|
+
"div",
|
|
59
|
+
{ className: "aura-dialog-layer aura-drawer-layer", onKeyDown: modal.onKeyDown },
|
|
60
|
+
h("div", { className: "aura-scrim", onClick: close, "aria-hidden": true }),
|
|
61
|
+
h(
|
|
62
|
+
"div",
|
|
63
|
+
{
|
|
64
|
+
ref: merged,
|
|
65
|
+
role: "dialog",
|
|
66
|
+
"aria-modal": true,
|
|
67
|
+
"aria-labelledby": props.title ? titleId : void 0,
|
|
68
|
+
"aria-label": props.title ? void 0 : props["aria-label"],
|
|
69
|
+
"aria-describedby": props.description ? descId : void 0,
|
|
70
|
+
tabIndex: -1,
|
|
71
|
+
className: cx("aura-drawer", "aura-drawer--" + side, "aura-drawer--" + (props.size || "md"), props.className)
|
|
72
|
+
},
|
|
73
|
+
props.title || props.dismissible !== false ? h(
|
|
74
|
+
"div",
|
|
75
|
+
{ className: "aura-drawer__head" },
|
|
76
|
+
props.title ? h("h2", { className: "aura-drawer__title", id: titleId }, props.title) : h("span", { style: { flex: 1 } }),
|
|
77
|
+
props.dismissible !== false ? h(IconButton, { icon: "x", label: t.close, onClick: close }) : null
|
|
78
|
+
) : null,
|
|
79
|
+
props.description ? h("p", { className: "aura-drawer__desc", id: descId }, props.description) : null,
|
|
80
|
+
h("div", { className: "aura-drawer__body" }, props.children),
|
|
81
|
+
props.footer ? h("div", { className: "aura-drawer__foot" }, props.footer) : null
|
|
82
|
+
)
|
|
83
|
+
),
|
|
84
|
+
document.body
|
|
85
|
+
);
|
|
86
|
+
});
|
|
87
|
+
export {
|
|
88
|
+
Dialog,
|
|
89
|
+
Drawer
|
|
90
|
+
};
|