@artemy-tech/datepicker 0.2.0 → 0.4.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/DateRangePicker-Bp95dxDX.d.cts +50 -0
- package/dist/DateRangePicker-Bp95dxDX.d.ts +50 -0
- package/dist/chunk-DQSX6QKR.js +785 -0
- package/dist/chunk-DQSX6QKR.js.map +1 -0
- package/dist/index.cjs +136 -51
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -45
- package/dist/index.d.ts +9 -45
- package/dist/index.js +28 -714
- package/dist/index.js.map +1 -1
- package/dist/rhf.cjs +850 -0
- package/dist/rhf.cjs.map +1 -1
- package/dist/rhf.d.cts +20 -1
- package/dist/rhf.d.ts +20 -1
- package/dist/rhf.js +46 -0
- package/dist/rhf.js.map +1 -1
- package/package.json +4 -3
package/dist/index.js
CHANGED
|
@@ -1,723 +1,37 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
{
|
|
8
|
-
className: ["datepicker-calendar", className].filter(Boolean).join(" "),
|
|
9
|
-
...props
|
|
10
|
-
}
|
|
11
|
-
);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
// src/components/DatePicker/DatePicker.tsx
|
|
15
|
-
import { useCallback, useRef as useRef2, useState } from "react";
|
|
16
|
-
import { format, isValid, parse } from "date-fns";
|
|
17
|
-
import { ru } from "date-fns/locale";
|
|
18
|
-
|
|
19
|
-
// src/hooks/useClickOutside.ts
|
|
20
|
-
import { useEffect } from "react";
|
|
21
|
-
function useClickOutside(ref, handler) {
|
|
22
|
-
useEffect(() => {
|
|
23
|
-
function listener(e) {
|
|
24
|
-
if (!ref.current || ref.current.contains(e.target)) return;
|
|
25
|
-
handler();
|
|
26
|
-
}
|
|
27
|
-
document.addEventListener("mousedown", listener);
|
|
28
|
-
return () => document.removeEventListener("mousedown", listener);
|
|
29
|
-
}, [ref, handler]);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// src/components/TimePanel/TimePanel.tsx
|
|
33
|
-
import { useEffect as useEffect2, useRef } from "react";
|
|
34
|
-
import { jsx as jsx2, jsxs } from "react/jsx-runtime";
|
|
35
|
-
var HOURS = Array.from({ length: 24 }, (_, i) => i);
|
|
36
|
-
var MINUTES = Array.from({ length: 60 }, (_, i) => i);
|
|
37
|
-
var SECONDS = Array.from({ length: 60 }, (_, i) => i);
|
|
38
|
-
function pad2(n) {
|
|
39
|
-
return String(n).padStart(2, "0");
|
|
40
|
-
}
|
|
41
|
-
function Column({ values, selected, onSelect }) {
|
|
42
|
-
const selectedRef = useRef(null);
|
|
43
|
-
useEffect2(() => {
|
|
44
|
-
var _a;
|
|
45
|
-
(_a = selectedRef.current) == null ? void 0 : _a.scrollIntoView({ block: "center", behavior: "instant" });
|
|
46
|
-
}, [selected]);
|
|
47
|
-
return /* @__PURE__ */ jsx2("div", { className: "time-panel__column", children: values.map((v) => /* @__PURE__ */ jsx2(
|
|
48
|
-
"button",
|
|
49
|
-
{
|
|
50
|
-
ref: v === selected ? selectedRef : void 0,
|
|
51
|
-
className: "time-panel__item",
|
|
52
|
-
"data-selected": v === selected || void 0,
|
|
53
|
-
onClick: () => onSelect(v),
|
|
54
|
-
type: "button",
|
|
55
|
-
tabIndex: -1,
|
|
56
|
-
children: pad2(v)
|
|
57
|
-
},
|
|
58
|
-
v
|
|
59
|
-
)) });
|
|
60
|
-
}
|
|
61
|
-
function TimePanel({ value, showSeconds, onChange }) {
|
|
62
|
-
var _a, _b, _c;
|
|
63
|
-
const h = (_a = value == null ? void 0 : value.getHours()) != null ? _a : 0;
|
|
64
|
-
const m = (_b = value == null ? void 0 : value.getMinutes()) != null ? _b : 0;
|
|
65
|
-
const s = (_c = value == null ? void 0 : value.getSeconds()) != null ? _c : 0;
|
|
66
|
-
return /* @__PURE__ */ jsxs("div", { className: "time-panel", children: [
|
|
67
|
-
/* @__PURE__ */ jsx2(Column, { values: HOURS, selected: h, onSelect: (v) => onChange(v, m, s) }),
|
|
68
|
-
/* @__PURE__ */ jsx2(Column, { values: MINUTES, selected: m, onSelect: (v) => onChange(h, v, s) }),
|
|
69
|
-
showSeconds && /* @__PURE__ */ jsx2(Column, { values: SECONDS, selected: s, onSelect: (v) => onChange(h, m, v) })
|
|
70
|
-
] });
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// src/components/icons/CalendarIcon.tsx
|
|
74
|
-
import { jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
75
|
-
function CalendarIcon() {
|
|
76
|
-
return /* @__PURE__ */ jsxs2("svg", { width: "16", height: "16", viewBox: "0 0 16 16", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [
|
|
77
|
-
/* @__PURE__ */ jsx3("rect", { x: "1", y: "2.5", width: "14", height: "12", rx: "1.5", stroke: "currentColor", strokeWidth: "1.5" }),
|
|
78
|
-
/* @__PURE__ */ jsx3("path", { d: "M1 6.5H15", stroke: "currentColor", strokeWidth: "1.5" }),
|
|
79
|
-
/* @__PURE__ */ jsx3("path", { d: "M5 1V4M11 1V4", stroke: "currentColor", strokeWidth: "1.5", strokeLinecap: "round" })
|
|
80
|
-
] });
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// src/components/DatePicker/DatePicker.tsx
|
|
84
|
-
import { Fragment, jsx as jsx4, jsxs as jsxs3 } from "react/jsx-runtime";
|
|
85
|
-
var DATE_FORMAT = "dd.MM.yyyy";
|
|
86
|
-
function resolveTimeFormat(showTime) {
|
|
87
|
-
if (!showTime) return null;
|
|
88
|
-
if (showTime === true) return "HH:mm:ss";
|
|
89
|
-
return showTime.format;
|
|
90
|
-
}
|
|
91
|
-
function buildDateFormat(timeFormat) {
|
|
92
|
-
return timeFormat ? `${DATE_FORMAT} ${timeFormat}` : DATE_FORMAT;
|
|
93
|
-
}
|
|
94
|
-
function buildMaxDigits(timeFormat) {
|
|
95
|
-
if (!timeFormat) return 8;
|
|
96
|
-
return timeFormat === "HH:mm" ? 12 : 14;
|
|
97
|
-
}
|
|
98
|
-
function buildPlaceholder(timeFormat) {
|
|
99
|
-
if (!timeFormat) return "\u0434\u0434.\u043C\u043C.\u0433\u0433\u0433\u0433";
|
|
100
|
-
return timeFormat === "HH:mm" ? "\u0434\u0434.\u043C\u043C.\u0433\u0433\u0433\u0433 \u0447\u0447:\u043C\u043C" : "\u0434\u0434.\u043C\u043C.\u0433\u0433\u0433\u0433 \u0447\u0447:\u043C\u043C:\u0441\u0441";
|
|
101
|
-
}
|
|
102
|
-
function applyMask(digits, maxDigits) {
|
|
103
|
-
const d = digits.slice(0, maxDigits);
|
|
104
|
-
let result = "";
|
|
105
|
-
for (let i = 0; i < d.length; i++) {
|
|
106
|
-
if (i === 2 || i === 4) result += ".";
|
|
107
|
-
else if (i === 8) result += " ";
|
|
108
|
-
else if (i === 10 || i === 12) result += ":";
|
|
109
|
-
result += d[i];
|
|
110
|
-
}
|
|
111
|
-
return result;
|
|
112
|
-
}
|
|
113
|
-
function getCursorPos(masked, digitCount) {
|
|
114
|
-
if (digitCount === 0) return 0;
|
|
115
|
-
let count = 0;
|
|
116
|
-
for (let i = 0; i < masked.length; i++) {
|
|
117
|
-
if (/\d/.test(masked[i])) {
|
|
118
|
-
count++;
|
|
119
|
-
if (count === digitCount) return i + 1;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
return masked.length;
|
|
123
|
-
}
|
|
124
|
-
function parseDateTime(masked, dateFormat, maxDigits) {
|
|
125
|
-
if (masked.replace(/\D/g, "").length !== maxDigits) return void 0;
|
|
126
|
-
const date = parse(masked, dateFormat, /* @__PURE__ */ new Date());
|
|
127
|
-
return isValid(date) && format(date, dateFormat) === masked ? date : void 0;
|
|
128
|
-
}
|
|
129
|
-
function DatePicker({
|
|
130
|
-
value,
|
|
131
|
-
defaultValue,
|
|
132
|
-
onChange,
|
|
133
|
-
label,
|
|
134
|
-
placeholder,
|
|
135
|
-
fromDate,
|
|
136
|
-
toDate,
|
|
137
|
-
disabled = false,
|
|
138
|
-
failed = false,
|
|
139
|
-
size = "m",
|
|
140
|
-
noCalendar = false,
|
|
141
|
-
showTime,
|
|
142
|
-
icon,
|
|
143
|
-
iconPosition = "end",
|
|
144
|
-
className
|
|
145
|
-
}) {
|
|
146
|
-
const timeFormat = resolveTimeFormat(showTime);
|
|
147
|
-
const dateFormat = buildDateFormat(timeFormat);
|
|
148
|
-
const maxDigits = buildMaxDigits(timeFormat);
|
|
149
|
-
const defaultPlaceholder = placeholder != null ? placeholder : buildPlaceholder(timeFormat);
|
|
150
|
-
const showSeconds = timeFormat === "HH:mm:ss";
|
|
151
|
-
const resolvedIcon = icon === false ? null : icon != null ? icon : /* @__PURE__ */ jsx4(CalendarIcon, {});
|
|
152
|
-
const isControlled = value !== void 0;
|
|
153
|
-
const [internalDate, setInternalDate] = useState(defaultValue);
|
|
154
|
-
const [open, setOpen] = useState(false);
|
|
155
|
-
const [focused, setFocused] = useState(false);
|
|
156
|
-
const [inputValue, setInputValue] = useState(
|
|
157
|
-
() => defaultValue && isValid(defaultValue) ? format(defaultValue, dateFormat) : ""
|
|
158
|
-
);
|
|
159
|
-
const [inputInvalid, setInputInvalid] = useState(false);
|
|
160
|
-
const inputRef = useRef2(null);
|
|
161
|
-
const containerRef = useRef2(null);
|
|
162
|
-
const lastValidRef = useRef2(inputValue);
|
|
163
|
-
const selected = isControlled ? value : internalDate;
|
|
164
|
-
const filled = inputValue.length > 0;
|
|
165
|
-
const close = useCallback(() => setOpen(false), []);
|
|
166
|
-
useClickOutside(containerRef, close);
|
|
167
|
-
function applyValid(masked, date) {
|
|
168
|
-
lastValidRef.current = masked;
|
|
169
|
-
setInputValue(masked);
|
|
170
|
-
setInputInvalid(false);
|
|
171
|
-
if (!isControlled) setInternalDate(date);
|
|
172
|
-
onChange == null ? void 0 : onChange(date);
|
|
173
|
-
}
|
|
174
|
-
function commit(masked) {
|
|
175
|
-
const digits = masked.replace(/\D/g, "");
|
|
176
|
-
if (digits.length === 0) {
|
|
177
|
-
lastValidRef.current = "";
|
|
178
|
-
setInputInvalid(false);
|
|
179
|
-
if (!isControlled) setInternalDate(void 0);
|
|
180
|
-
onChange == null ? void 0 : onChange(void 0);
|
|
181
|
-
} else if (digits.length === maxDigits) {
|
|
182
|
-
const date = parseDateTime(masked, dateFormat, maxDigits);
|
|
183
|
-
if (date) lastValidRef.current = masked;
|
|
184
|
-
setInputInvalid(!date);
|
|
185
|
-
if (!isControlled) setInternalDate(date);
|
|
186
|
-
onChange == null ? void 0 : onChange(date);
|
|
187
|
-
} else {
|
|
188
|
-
setInputInvalid(false);
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
function handleBlur() {
|
|
192
|
-
setFocused(false);
|
|
193
|
-
const digits = inputValue.replace(/\D/g, "");
|
|
194
|
-
if (digits.length > 0 && digits.length < maxDigits || inputInvalid) {
|
|
195
|
-
setInputValue(lastValidRef.current);
|
|
196
|
-
setInputInvalid(false);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
function handleChange(e) {
|
|
200
|
-
var _a;
|
|
201
|
-
const input = e.target;
|
|
202
|
-
const cursorPos = (_a = input.selectionStart) != null ? _a : 0;
|
|
203
|
-
const raw = input.value;
|
|
204
|
-
const digits = raw.replace(/\D/g, "").slice(0, maxDigits);
|
|
205
|
-
const masked = applyMask(digits, maxDigits);
|
|
206
|
-
const digitsBeforeCursor = raw.slice(0, cursorPos).replace(/\D/g, "").length;
|
|
207
|
-
const newCursorPos = getCursorPos(masked, digitsBeforeCursor);
|
|
208
|
-
setInputValue(masked);
|
|
209
|
-
commit(masked);
|
|
210
|
-
requestAnimationFrame(() => {
|
|
211
|
-
var _a2;
|
|
212
|
-
return (_a2 = inputRef.current) == null ? void 0 : _a2.setSelectionRange(newCursorPos, newCursorPos);
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
function handleKeyDown(e) {
|
|
216
|
-
var _a;
|
|
217
|
-
const input = e.currentTarget;
|
|
218
|
-
const pos = (_a = input.selectionStart) != null ? _a : 0;
|
|
219
|
-
if (e.key.length === 1 && !/\d/.test(e.key) && !e.ctrlKey && !e.metaKey) {
|
|
220
|
-
e.preventDefault();
|
|
221
|
-
return;
|
|
222
|
-
}
|
|
223
|
-
if (e.key === "Backspace" && pos > 0 && /[.: ]/.test(input.value[pos - 1])) {
|
|
224
|
-
e.preventDefault();
|
|
225
|
-
const val = input.value;
|
|
226
|
-
const masked = applyMask((val.slice(0, pos - 2) + val.slice(pos)).replace(/\D/g, ""), maxDigits);
|
|
227
|
-
setInputValue(masked);
|
|
228
|
-
commit(masked);
|
|
229
|
-
requestAnimationFrame(() => input.setSelectionRange(pos - 2, pos - 2));
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
function handlePaste(e) {
|
|
233
|
-
e.preventDefault();
|
|
234
|
-
const masked = applyMask(e.clipboardData.getData("text").replace(/\D/g, ""), maxDigits);
|
|
235
|
-
setInputValue(masked);
|
|
236
|
-
commit(masked);
|
|
237
|
-
requestAnimationFrame(() => {
|
|
238
|
-
var _a;
|
|
239
|
-
return (_a = inputRef.current) == null ? void 0 : _a.setSelectionRange(masked.length, masked.length);
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
function handleCalendarSelect(date) {
|
|
243
|
-
if (!date || !isValid(date)) {
|
|
244
|
-
applyValid("", void 0);
|
|
245
|
-
if (!timeFormat) setOpen(false);
|
|
246
|
-
return;
|
|
247
|
-
}
|
|
248
|
-
let dateToCommit = date;
|
|
249
|
-
if (timeFormat) {
|
|
250
|
-
const base = selected && isValid(selected) ? selected : /* @__PURE__ */ new Date(0);
|
|
251
|
-
dateToCommit = new Date(
|
|
252
|
-
date.getFullYear(),
|
|
253
|
-
date.getMonth(),
|
|
254
|
-
date.getDate(),
|
|
255
|
-
base.getHours(),
|
|
256
|
-
base.getMinutes(),
|
|
257
|
-
base.getSeconds()
|
|
258
|
-
);
|
|
259
|
-
}
|
|
260
|
-
applyValid(format(dateToCommit, dateFormat), dateToCommit);
|
|
261
|
-
if (!timeFormat) setOpen(false);
|
|
262
|
-
}
|
|
263
|
-
function handleTimeChange(h, m, s) {
|
|
264
|
-
const base = selected && isValid(selected) ? selected : /* @__PURE__ */ new Date();
|
|
265
|
-
const newDate = new Date(base.getFullYear(), base.getMonth(), base.getDate(), h, m, s);
|
|
266
|
-
applyValid(format(newDate, dateFormat), newDate);
|
|
267
|
-
}
|
|
268
|
-
return /* @__PURE__ */ jsxs3(
|
|
269
|
-
"div",
|
|
270
|
-
{
|
|
271
|
-
ref: containerRef,
|
|
272
|
-
className: ["datepicker", `datepicker--${size}`, className].filter(Boolean).join(" "),
|
|
273
|
-
"data-focused": focused || open || void 0,
|
|
274
|
-
"data-filled": filled || void 0,
|
|
275
|
-
"data-failed": failed || inputInvalid || void 0,
|
|
276
|
-
"data-disabled": disabled || void 0,
|
|
277
|
-
children: [
|
|
278
|
-
/* @__PURE__ */ jsxs3(
|
|
279
|
-
"div",
|
|
280
|
-
{
|
|
281
|
-
className: "datepicker__field",
|
|
282
|
-
"data-icon-start": resolvedIcon && iconPosition === "start" ? true : void 0,
|
|
283
|
-
"data-icon-end": resolvedIcon && iconPosition === "end" ? true : void 0,
|
|
284
|
-
onClick: () => {
|
|
285
|
-
var _a;
|
|
286
|
-
return !disabled && ((_a = inputRef.current) == null ? void 0 : _a.focus());
|
|
287
|
-
},
|
|
288
|
-
children: [
|
|
289
|
-
resolvedIcon && iconPosition === "start" && /* @__PURE__ */ jsx4("span", { className: "datepicker__icon datepicker__icon--start", children: resolvedIcon }),
|
|
290
|
-
label && /* @__PURE__ */ jsx4("span", { className: "datepicker__label", children: label }),
|
|
291
|
-
/* @__PURE__ */ jsx4(
|
|
292
|
-
"input",
|
|
293
|
-
{
|
|
294
|
-
ref: inputRef,
|
|
295
|
-
type: "text",
|
|
296
|
-
inputMode: "numeric",
|
|
297
|
-
className: "datepicker__input",
|
|
298
|
-
value: inputValue,
|
|
299
|
-
placeholder: label && !focused ? void 0 : defaultPlaceholder,
|
|
300
|
-
disabled,
|
|
301
|
-
onChange: handleChange,
|
|
302
|
-
onKeyDown: handleKeyDown,
|
|
303
|
-
onPaste: handlePaste,
|
|
304
|
-
onFocus: () => {
|
|
305
|
-
setFocused(true);
|
|
306
|
-
if (!disabled && !noCalendar) setOpen(true);
|
|
307
|
-
},
|
|
308
|
-
onBlur: handleBlur,
|
|
309
|
-
"aria-label": label != null ? label : "\u0412\u044B\u0431\u0435\u0440\u0438\u0442\u0435 \u0434\u0430\u0442\u0443",
|
|
310
|
-
"aria-expanded": !noCalendar ? open : void 0,
|
|
311
|
-
"aria-haspopup": !noCalendar ? "dialog" : void 0,
|
|
312
|
-
"aria-invalid": inputInvalid || void 0
|
|
313
|
-
}
|
|
314
|
-
),
|
|
315
|
-
resolvedIcon && iconPosition === "end" && /* @__PURE__ */ jsx4("span", { className: "datepicker__icon datepicker__icon--end", children: resolvedIcon })
|
|
316
|
-
]
|
|
317
|
-
}
|
|
318
|
-
),
|
|
319
|
-
!noCalendar && open && /* @__PURE__ */ jsx4(
|
|
320
|
-
"div",
|
|
321
|
-
{
|
|
322
|
-
className: [
|
|
323
|
-
"datepicker__popover",
|
|
324
|
-
`datepicker__popover--${size}`,
|
|
325
|
-
timeFormat && "datepicker__popover--with-time"
|
|
326
|
-
].filter(Boolean).join(" "),
|
|
327
|
-
role: "dialog",
|
|
328
|
-
"aria-label": "\u041A\u0430\u043B\u0435\u043D\u0434\u0430\u0440\u044C",
|
|
329
|
-
children: timeFormat ? /* @__PURE__ */ jsxs3(Fragment, { children: [
|
|
330
|
-
/* @__PURE__ */ jsxs3("div", { className: "datepicker__popover-body", children: [
|
|
331
|
-
/* @__PURE__ */ jsx4("div", { className: "datepicker__popover-calendar", children: /* @__PURE__ */ jsx4(
|
|
332
|
-
Calendar,
|
|
333
|
-
{
|
|
334
|
-
mode: "single",
|
|
335
|
-
selected,
|
|
336
|
-
onSelect: handleCalendarSelect,
|
|
337
|
-
startMonth: fromDate,
|
|
338
|
-
endMonth: toDate,
|
|
339
|
-
locale: ru
|
|
340
|
-
}
|
|
341
|
-
) }),
|
|
342
|
-
/* @__PURE__ */ jsx4("div", { className: "datepicker__time-separator" }),
|
|
343
|
-
/* @__PURE__ */ jsx4("div", { className: "datepicker__popover-time", children: /* @__PURE__ */ jsx4(
|
|
344
|
-
TimePanel,
|
|
345
|
-
{
|
|
346
|
-
value: selected,
|
|
347
|
-
showSeconds,
|
|
348
|
-
onChange: handleTimeChange
|
|
349
|
-
}
|
|
350
|
-
) })
|
|
351
|
-
] }),
|
|
352
|
-
/* @__PURE__ */ jsx4("div", { className: "datepicker__popover-footer", children: /* @__PURE__ */ jsx4(
|
|
353
|
-
"button",
|
|
354
|
-
{
|
|
355
|
-
className: "datepicker__ok-btn",
|
|
356
|
-
type: "button",
|
|
357
|
-
onClick: () => setOpen(false),
|
|
358
|
-
children: "OK"
|
|
359
|
-
}
|
|
360
|
-
) })
|
|
361
|
-
] }) : /* @__PURE__ */ jsx4(
|
|
362
|
-
Calendar,
|
|
363
|
-
{
|
|
364
|
-
mode: "single",
|
|
365
|
-
selected,
|
|
366
|
-
onSelect: handleCalendarSelect,
|
|
367
|
-
startMonth: fromDate,
|
|
368
|
-
endMonth: toDate,
|
|
369
|
-
locale: ru
|
|
370
|
-
}
|
|
371
|
-
)
|
|
372
|
-
}
|
|
373
|
-
)
|
|
374
|
-
]
|
|
375
|
-
}
|
|
376
|
-
);
|
|
377
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
Calendar,
|
|
3
|
+
DatePicker,
|
|
4
|
+
DateRangePicker,
|
|
5
|
+
Spinner
|
|
6
|
+
} from "./chunk-DQSX6QKR.js";
|
|
378
7
|
|
|
379
|
-
// src/components/
|
|
380
|
-
import {
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
import { Fragment as Fragment2, jsx as jsx5, jsxs as jsxs4 } from "react/jsx-runtime";
|
|
384
|
-
var DATE_FORMAT2 = "dd.MM.yyyy";
|
|
385
|
-
function applyDateMask(digits) {
|
|
386
|
-
const d = digits.slice(0, 8);
|
|
387
|
-
let result = "";
|
|
388
|
-
for (let i = 0; i < d.length; i++) {
|
|
389
|
-
if (i === 2 || i === 4) result += ".";
|
|
390
|
-
result += d[i];
|
|
391
|
-
}
|
|
392
|
-
return result;
|
|
393
|
-
}
|
|
394
|
-
function applyRangeMask(digits) {
|
|
395
|
-
const all = digits.slice(0, 16);
|
|
396
|
-
const fromMasked = applyDateMask(all.slice(0, 8));
|
|
397
|
-
const toDigits = all.slice(8);
|
|
398
|
-
if (toDigits.length === 0) return fromMasked;
|
|
399
|
-
return `${fromMasked} \u2014 ${applyDateMask(toDigits)}`;
|
|
400
|
-
}
|
|
401
|
-
function getRangeCursorPos(masked, digitCount) {
|
|
402
|
-
if (digitCount === 0) return 0;
|
|
403
|
-
let count = 0;
|
|
404
|
-
for (let i = 0; i < masked.length; i++) {
|
|
405
|
-
if (/\d/.test(masked[i])) {
|
|
406
|
-
count++;
|
|
407
|
-
if (count === digitCount) return i + 1;
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
return masked.length;
|
|
411
|
-
}
|
|
412
|
-
function parseDate(masked) {
|
|
413
|
-
if (masked.replace(/\D/g, "").length !== 8) return void 0;
|
|
414
|
-
const date = parse2(masked, DATE_FORMAT2, /* @__PURE__ */ new Date());
|
|
415
|
-
return isValid2(date) && format2(date, DATE_FORMAT2) === masked ? date : void 0;
|
|
416
|
-
}
|
|
417
|
-
function formatRange(from, to) {
|
|
418
|
-
if (!from) return "";
|
|
419
|
-
const fromStr = format2(from, DATE_FORMAT2);
|
|
420
|
-
if (!to) return fromStr;
|
|
421
|
-
return `${fromStr} \u2014 ${format2(to, DATE_FORMAT2)}`;
|
|
422
|
-
}
|
|
423
|
-
function resolveShowSeconds(showTime) {
|
|
424
|
-
if (!showTime) return false;
|
|
425
|
-
if (showTime === true) return true;
|
|
426
|
-
return showTime.format === "HH:mm:ss";
|
|
427
|
-
}
|
|
428
|
-
function DateRangePicker({
|
|
429
|
-
value,
|
|
430
|
-
defaultValue,
|
|
431
|
-
onChange,
|
|
432
|
-
label,
|
|
433
|
-
fromDate: fromConstraint,
|
|
434
|
-
toDate: toConstraint,
|
|
435
|
-
disabled = false,
|
|
436
|
-
failed = false,
|
|
8
|
+
// src/components/Button/Button.tsx
|
|
9
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
10
|
+
function Button({
|
|
11
|
+
variant = "primary",
|
|
437
12
|
size = "m",
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
13
|
+
loading = false,
|
|
14
|
+
disabled,
|
|
15
|
+
className,
|
|
16
|
+
children,
|
|
17
|
+
...rest
|
|
443
18
|
}) {
|
|
444
|
-
const
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
const [anchorDate, setAnchorDate] = useState2(void 0);
|
|
456
|
-
const [hoveredDate, setHoveredDate] = useState2(void 0);
|
|
457
|
-
const inputRef = useRef3(null);
|
|
458
|
-
const containerRef = useRef3(null);
|
|
459
|
-
const confirmedFrom = isControlled ? value == null ? void 0 : value.from : internalFrom;
|
|
460
|
-
const confirmedTo = isControlled ? value == null ? void 0 : value.to : internalTo;
|
|
461
|
-
const filled = inputValue.length > 0;
|
|
462
|
-
const close = useCallback2(() => {
|
|
463
|
-
setOpen(false);
|
|
464
|
-
setAnchorDate(void 0);
|
|
465
|
-
setHoveredDate(void 0);
|
|
466
|
-
}, []);
|
|
467
|
-
useClickOutside(containerRef, close);
|
|
468
|
-
const calendarSelected = anchorDate ? hoveredDate ? anchorDate <= hoveredDate ? { from: anchorDate, to: hoveredDate } : { from: hoveredDate, to: anchorDate } : { from: anchorDate, to: void 0 } : { from: confirmedFrom, to: confirmedTo };
|
|
469
|
-
function handleDayClick(day) {
|
|
470
|
-
var _a, _b, _c, _d, _e, _f;
|
|
471
|
-
if (!anchorDate) {
|
|
472
|
-
const from = showTime ? new Date(
|
|
473
|
-
day.getFullYear(),
|
|
474
|
-
day.getMonth(),
|
|
475
|
-
day.getDate(),
|
|
476
|
-
(_a = confirmedFrom == null ? void 0 : confirmedFrom.getHours()) != null ? _a : 0,
|
|
477
|
-
(_b = confirmedFrom == null ? void 0 : confirmedFrom.getMinutes()) != null ? _b : 0,
|
|
478
|
-
(_c = confirmedFrom == null ? void 0 : confirmedFrom.getSeconds()) != null ? _c : 0
|
|
479
|
-
) : day;
|
|
480
|
-
setAnchorDate(from);
|
|
481
|
-
if (!isControlled) {
|
|
482
|
-
setInternalFrom(from);
|
|
483
|
-
setInternalTo(void 0);
|
|
484
|
-
}
|
|
485
|
-
setInputValue(format2(day, DATE_FORMAT2));
|
|
486
|
-
setInputInvalid(false);
|
|
487
|
-
onChange == null ? void 0 : onChange({ from, to: void 0 });
|
|
488
|
-
} else {
|
|
489
|
-
let from = anchorDate, to = showTime ? new Date(
|
|
490
|
-
day.getFullYear(),
|
|
491
|
-
day.getMonth(),
|
|
492
|
-
day.getDate(),
|
|
493
|
-
(_d = confirmedTo == null ? void 0 : confirmedTo.getHours()) != null ? _d : 0,
|
|
494
|
-
(_e = confirmedTo == null ? void 0 : confirmedTo.getMinutes()) != null ? _e : 0,
|
|
495
|
-
(_f = confirmedTo == null ? void 0 : confirmedTo.getSeconds()) != null ? _f : 0
|
|
496
|
-
) : day;
|
|
497
|
-
if (day < anchorDate) {
|
|
498
|
-
const tmp = from;
|
|
499
|
-
from = to;
|
|
500
|
-
to = tmp;
|
|
501
|
-
}
|
|
502
|
-
if (!isControlled) {
|
|
503
|
-
setInternalFrom(from);
|
|
504
|
-
setInternalTo(to);
|
|
505
|
-
}
|
|
506
|
-
setInputValue(formatRange(from, to));
|
|
507
|
-
setInputInvalid(false);
|
|
508
|
-
onChange == null ? void 0 : onChange({ from, to });
|
|
509
|
-
if (!showTime) close();
|
|
510
|
-
else setAnchorDate(void 0);
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
function handleDayMouseEnter(day) {
|
|
514
|
-
if (anchorDate) setHoveredDate(day);
|
|
515
|
-
}
|
|
516
|
-
function handleFromTimeChange(h, m, s) {
|
|
517
|
-
const base = confirmedFrom != null ? confirmedFrom : /* @__PURE__ */ new Date();
|
|
518
|
-
const newDate = new Date(base.getFullYear(), base.getMonth(), base.getDate(), h, m, s);
|
|
519
|
-
if (!isControlled) setInternalFrom(newDate);
|
|
520
|
-
onChange == null ? void 0 : onChange({ from: newDate, to: confirmedTo });
|
|
521
|
-
}
|
|
522
|
-
function handleToTimeChange(h, m, s) {
|
|
523
|
-
const base = confirmedTo != null ? confirmedTo : /* @__PURE__ */ new Date();
|
|
524
|
-
const newDate = new Date(base.getFullYear(), base.getMonth(), base.getDate(), h, m, s);
|
|
525
|
-
if (!isControlled) setInternalTo(newDate);
|
|
526
|
-
onChange == null ? void 0 : onChange({ from: confirmedFrom, to: newDate });
|
|
527
|
-
}
|
|
528
|
-
function handleChange(e) {
|
|
529
|
-
var _a;
|
|
530
|
-
const input = e.target;
|
|
531
|
-
const cursorPos = (_a = input.selectionStart) != null ? _a : 0;
|
|
532
|
-
const raw = input.value;
|
|
533
|
-
const digits = raw.replace(/\D/g, "").slice(0, 16);
|
|
534
|
-
const masked = applyRangeMask(digits);
|
|
535
|
-
const digitsBeforeCursor = raw.slice(0, cursorPos).replace(/\D/g, "").length;
|
|
536
|
-
setInputValue(masked);
|
|
537
|
-
setAnchorDate(void 0);
|
|
538
|
-
setHoveredDate(void 0);
|
|
539
|
-
const fromDigits = digits.slice(0, 8);
|
|
540
|
-
const toDigits = digits.slice(8);
|
|
541
|
-
const parsedFrom = fromDigits.length === 8 ? parseDate(applyDateMask(fromDigits)) : void 0;
|
|
542
|
-
const parsedTo = toDigits.length === 8 ? parseDate(applyDateMask(toDigits)) : void 0;
|
|
543
|
-
const fromComplete = fromDigits.length === 8;
|
|
544
|
-
const toComplete = toDigits.length === 8;
|
|
545
|
-
setInputInvalid(fromComplete && !parsedFrom || toComplete && !parsedTo);
|
|
546
|
-
if (!isControlled) {
|
|
547
|
-
setInternalFrom(parsedFrom);
|
|
548
|
-
setInternalTo(parsedTo);
|
|
549
|
-
}
|
|
550
|
-
onChange == null ? void 0 : onChange(parsedFrom || parsedTo ? { from: parsedFrom, to: parsedTo } : void 0);
|
|
551
|
-
requestAnimationFrame(
|
|
552
|
-
() => {
|
|
553
|
-
var _a2;
|
|
554
|
-
return (_a2 = inputRef.current) == null ? void 0 : _a2.setSelectionRange(
|
|
555
|
-
getRangeCursorPos(masked, digitsBeforeCursor),
|
|
556
|
-
getRangeCursorPos(masked, digitsBeforeCursor)
|
|
557
|
-
);
|
|
558
|
-
}
|
|
559
|
-
);
|
|
560
|
-
}
|
|
561
|
-
function handleKeyDown(e) {
|
|
562
|
-
var _a, _b, _c;
|
|
563
|
-
const input = e.currentTarget;
|
|
564
|
-
const pos = (_a = input.selectionStart) != null ? _a : 0;
|
|
565
|
-
if (e.key.length === 1 && !/\d/.test(e.key) && !e.ctrlKey && !e.metaKey) {
|
|
566
|
-
e.preventDefault();
|
|
567
|
-
return;
|
|
568
|
-
}
|
|
569
|
-
if (e.key === "Backspace" && pos > 0 && /[\s—]/.test(input.value[pos - 1])) {
|
|
570
|
-
e.preventDefault();
|
|
571
|
-
const val = input.value;
|
|
572
|
-
const charsToSkip = (_c = (_b = val.slice(0, pos).match(/[\s—]+$/)) == null ? void 0 : _b[0].length) != null ? _c : 1;
|
|
573
|
-
const newPos = pos - charsToSkip;
|
|
574
|
-
const masked = applyRangeMask((val.slice(0, newPos - 1) + val.slice(newPos)).replace(/\D/g, ""));
|
|
575
|
-
setInputValue(masked);
|
|
576
|
-
requestAnimationFrame(() => input.setSelectionRange(newPos - 1, newPos - 1));
|
|
577
|
-
}
|
|
578
|
-
}
|
|
579
|
-
function handlePaste(e) {
|
|
580
|
-
e.preventDefault();
|
|
581
|
-
const text = e.clipboardData.getData("text");
|
|
582
|
-
const digits = text.replace(/\D/g, "").slice(0, 16);
|
|
583
|
-
const masked = applyRangeMask(digits);
|
|
584
|
-
setInputValue(masked);
|
|
585
|
-
setAnchorDate(void 0);
|
|
586
|
-
setHoveredDate(void 0);
|
|
587
|
-
const parsedFrom = digits.length >= 8 ? parseDate(applyDateMask(digits.slice(0, 8))) : void 0;
|
|
588
|
-
const parsedTo = digits.length >= 16 ? parseDate(applyDateMask(digits.slice(8, 16))) : void 0;
|
|
589
|
-
setInputInvalid(digits.length >= 8 && !parsedFrom || digits.length >= 16 && !parsedTo);
|
|
590
|
-
if (!isControlled) {
|
|
591
|
-
setInternalFrom(parsedFrom);
|
|
592
|
-
setInternalTo(parsedTo);
|
|
593
|
-
}
|
|
594
|
-
onChange == null ? void 0 : onChange(parsedFrom || parsedTo ? { from: parsedFrom, to: parsedTo } : void 0);
|
|
595
|
-
requestAnimationFrame(() => {
|
|
596
|
-
var _a;
|
|
597
|
-
return (_a = inputRef.current) == null ? void 0 : _a.setSelectionRange(masked.length, masked.length);
|
|
598
|
-
});
|
|
599
|
-
}
|
|
600
|
-
const placeholder = label && !focused && !filled ? void 0 : "\u0434\u0434.\u043C\u043C.\u0433\u0433\u0433\u0433 \u2014 \u0434\u0434.\u043C\u043C.\u0433\u0433\u0433\u0433";
|
|
601
|
-
return /* @__PURE__ */ jsxs4(
|
|
602
|
-
"div",
|
|
603
|
-
{
|
|
604
|
-
ref: containerRef,
|
|
605
|
-
className: ["datepicker", "daterangepicker", `datepicker--${size}`, className].filter(Boolean).join(" "),
|
|
606
|
-
"data-focused": focused || open || void 0,
|
|
607
|
-
"data-filled": filled || void 0,
|
|
608
|
-
"data-failed": failed || inputInvalid || void 0,
|
|
609
|
-
"data-disabled": disabled || void 0,
|
|
610
|
-
children: [
|
|
611
|
-
/* @__PURE__ */ jsxs4(
|
|
612
|
-
"div",
|
|
613
|
-
{
|
|
614
|
-
className: "datepicker__field",
|
|
615
|
-
"data-icon-start": resolvedIcon && iconPosition === "start" ? true : void 0,
|
|
616
|
-
"data-icon-end": resolvedIcon && iconPosition === "end" ? true : void 0,
|
|
617
|
-
onClick: () => {
|
|
618
|
-
var _a;
|
|
619
|
-
return !disabled && ((_a = inputRef.current) == null ? void 0 : _a.focus());
|
|
620
|
-
},
|
|
621
|
-
children: [
|
|
622
|
-
resolvedIcon && iconPosition === "start" && /* @__PURE__ */ jsx5("span", { className: "datepicker__icon datepicker__icon--start", children: resolvedIcon }),
|
|
623
|
-
label && /* @__PURE__ */ jsx5("span", { className: "datepicker__label", children: label }),
|
|
624
|
-
/* @__PURE__ */ jsx5(
|
|
625
|
-
"input",
|
|
626
|
-
{
|
|
627
|
-
ref: inputRef,
|
|
628
|
-
type: "text",
|
|
629
|
-
inputMode: "numeric",
|
|
630
|
-
className: "datepicker__input",
|
|
631
|
-
value: inputValue,
|
|
632
|
-
placeholder,
|
|
633
|
-
disabled,
|
|
634
|
-
onChange: handleChange,
|
|
635
|
-
onKeyDown: handleKeyDown,
|
|
636
|
-
onPaste: handlePaste,
|
|
637
|
-
onFocus: () => {
|
|
638
|
-
setFocused(true);
|
|
639
|
-
if (!disabled) setOpen(true);
|
|
640
|
-
},
|
|
641
|
-
onBlur: () => setFocused(false),
|
|
642
|
-
"aria-label": label != null ? label : "\u0412\u044B\u0431\u0435\u0440\u0438\u0442\u0435 \u043F\u0435\u0440\u0438\u043E\u0434",
|
|
643
|
-
"aria-expanded": open,
|
|
644
|
-
"aria-haspopup": "dialog",
|
|
645
|
-
"aria-invalid": inputInvalid || void 0
|
|
646
|
-
}
|
|
647
|
-
),
|
|
648
|
-
resolvedIcon && iconPosition === "end" && /* @__PURE__ */ jsx5("span", { className: "datepicker__icon datepicker__icon--end", children: resolvedIcon })
|
|
649
|
-
]
|
|
650
|
-
}
|
|
651
|
-
),
|
|
652
|
-
open && /* @__PURE__ */ jsx5(
|
|
653
|
-
"div",
|
|
654
|
-
{
|
|
655
|
-
className: [
|
|
656
|
-
"datepicker__popover",
|
|
657
|
-
`datepicker__popover--${size}`,
|
|
658
|
-
calendarLayout === "horizontal" && "datepicker__popover--horizontal",
|
|
659
|
-
showTime && "datepicker__popover--with-time"
|
|
660
|
-
].filter(Boolean).join(" "),
|
|
661
|
-
role: "dialog",
|
|
662
|
-
"aria-label": "\u0412\u044B\u0431\u0435\u0440\u0438\u0442\u0435 \u043F\u0435\u0440\u0438\u043E\u0434",
|
|
663
|
-
children: showTime ? /* @__PURE__ */ jsxs4(Fragment2, { children: [
|
|
664
|
-
/* @__PURE__ */ jsx5("div", { className: "datepicker__popover-body", children: /* @__PURE__ */ jsx5("div", { className: "datepicker__popover-calendar", children: /* @__PURE__ */ jsx5(
|
|
665
|
-
Calendar,
|
|
666
|
-
{
|
|
667
|
-
mode: "range",
|
|
668
|
-
selected: calendarSelected,
|
|
669
|
-
onSelect: () => {
|
|
670
|
-
},
|
|
671
|
-
onDayClick: handleDayClick,
|
|
672
|
-
onDayMouseEnter: handleDayMouseEnter,
|
|
673
|
-
onDayMouseLeave: () => setHoveredDate(void 0),
|
|
674
|
-
startMonth: fromConstraint,
|
|
675
|
-
endMonth: toConstraint,
|
|
676
|
-
numberOfMonths: 2,
|
|
677
|
-
locale: ru2
|
|
678
|
-
}
|
|
679
|
-
) }) }),
|
|
680
|
-
/* @__PURE__ */ jsxs4("div", { className: "datepicker__time-row", children: [
|
|
681
|
-
/* @__PURE__ */ jsxs4("div", { className: "datepicker__time-col", children: [
|
|
682
|
-
/* @__PURE__ */ jsx5("span", { className: "datepicker__time-label", children: "\u041D\u0430\u0447\u0430\u043B\u043E" }),
|
|
683
|
-
/* @__PURE__ */ jsx5(TimePanel, { value: confirmedFrom, showSeconds, onChange: handleFromTimeChange })
|
|
684
|
-
] }),
|
|
685
|
-
/* @__PURE__ */ jsx5("div", { className: "datepicker__time-separator" }),
|
|
686
|
-
/* @__PURE__ */ jsxs4("div", { className: "datepicker__time-col", children: [
|
|
687
|
-
/* @__PURE__ */ jsx5("span", { className: "datepicker__time-label", children: "\u041A\u043E\u043D\u0435\u0446" }),
|
|
688
|
-
/* @__PURE__ */ jsx5(TimePanel, { value: confirmedTo, showSeconds, onChange: handleToTimeChange })
|
|
689
|
-
] })
|
|
690
|
-
] }),
|
|
691
|
-
/* @__PURE__ */ jsx5("div", { className: "datepicker__popover-footer", children: /* @__PURE__ */ jsx5("button", { className: "datepicker__ok-btn", type: "button", onClick: close, children: "OK" }) })
|
|
692
|
-
] }) : /* @__PURE__ */ jsx5(
|
|
693
|
-
Calendar,
|
|
694
|
-
{
|
|
695
|
-
mode: "range",
|
|
696
|
-
selected: calendarSelected,
|
|
697
|
-
onSelect: () => {
|
|
698
|
-
},
|
|
699
|
-
onDayClick: handleDayClick,
|
|
700
|
-
onDayMouseEnter: handleDayMouseEnter,
|
|
701
|
-
onDayMouseLeave: () => setHoveredDate(void 0),
|
|
702
|
-
startMonth: fromConstraint,
|
|
703
|
-
endMonth: toConstraint,
|
|
704
|
-
numberOfMonths: 2,
|
|
705
|
-
locale: ru2
|
|
706
|
-
}
|
|
707
|
-
)
|
|
708
|
-
}
|
|
709
|
-
)
|
|
710
|
-
]
|
|
711
|
-
}
|
|
712
|
-
);
|
|
19
|
+
const classes = [
|
|
20
|
+
"dp-btn",
|
|
21
|
+
`dp-btn--${variant}`,
|
|
22
|
+
`dp-btn--${size}`,
|
|
23
|
+
loading && "dp-btn--loading",
|
|
24
|
+
className
|
|
25
|
+
].filter(Boolean).join(" ");
|
|
26
|
+
return /* @__PURE__ */ jsxs("button", { ...rest, className: classes, disabled: disabled || loading, children: [
|
|
27
|
+
children,
|
|
28
|
+
loading && /* @__PURE__ */ jsx(Spinner, {})
|
|
29
|
+
] });
|
|
713
30
|
}
|
|
714
|
-
|
|
715
|
-
// src/index.ts
|
|
716
|
-
var VERSION = "0.0.1";
|
|
717
31
|
export {
|
|
32
|
+
Button,
|
|
718
33
|
Calendar,
|
|
719
34
|
DatePicker,
|
|
720
|
-
DateRangePicker
|
|
721
|
-
VERSION
|
|
35
|
+
DateRangePicker
|
|
722
36
|
};
|
|
723
37
|
//# sourceMappingURL=index.js.map
|