@artemy-tech/datepicker 0.4.0 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import { DayPickerProps } from 'react-day-picker';
3
3
  export { DateRange } from 'react-day-picker';
4
- export { D as DatePicker, a as DatePickerProps, b as DatePickerShowTime, c as DateRangePicker, d as DateRangePickerProps } from './DateRangePicker-Bp95dxDX.cjs';
4
+ export { D as DatePicker, a as DatePickerInputProps, b as DatePickerProps, c as DatePickerShowTime, d as DateRangePicker, e as DateRangePickerProps } from './DateRangePicker-uJd_I9OI.cjs';
5
5
  import { ButtonHTMLAttributes } from 'react';
6
6
 
7
7
  type CalendarProps = DayPickerProps & {
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import { DayPickerProps } from 'react-day-picker';
3
3
  export { DateRange } from 'react-day-picker';
4
- export { D as DatePicker, a as DatePickerProps, b as DatePickerShowTime, c as DateRangePicker, d as DateRangePickerProps } from './DateRangePicker-Bp95dxDX.js';
4
+ export { D as DatePicker, a as DatePickerInputProps, b as DatePickerProps, c as DatePickerShowTime, d as DateRangePicker, e as DateRangePickerProps } from './DateRangePicker-uJd_I9OI.js';
5
5
  import { ButtonHTMLAttributes } from 'react';
6
6
 
7
7
  type CalendarProps = DayPickerProps & {
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ import {
3
3
  DatePicker,
4
4
  DateRangePicker,
5
5
  Spinner
6
- } from "./chunk-DQSX6QKR.js";
6
+ } from "./chunk-IIIOWD5F.js";
7
7
 
8
8
  // src/components/Button/Button.tsx
9
9
  import { jsx, jsxs } from "react/jsx-runtime";
package/dist/rhf.cjs CHANGED
@@ -157,10 +157,14 @@ function getCursorPos(masked, digitCount) {
157
157
  }
158
158
  return masked.length;
159
159
  }
160
+ function toDateOnly(date) {
161
+ return new Date(date.getFullYear(), date.getMonth(), date.getDate(), 12, 0, 0);
162
+ }
160
163
  function parseDateTime(masked, dateFormat, maxDigits) {
161
164
  if (masked.replace(/\D/g, "").length !== maxDigits) return void 0;
162
165
  const date = (0, import_date_fns.parse)(masked, dateFormat, /* @__PURE__ */ new Date());
163
- return (0, import_date_fns.isValid)(date) && (0, import_date_fns.format)(date, dateFormat) === masked ? date : void 0;
166
+ if (!(0, import_date_fns.isValid)(date) || (0, import_date_fns.format)(date, dateFormat) !== masked) return void 0;
167
+ return maxDigits === 8 ? toDateOnly(date) : date;
164
168
  }
165
169
  function DatePicker({
166
170
  value,
@@ -178,13 +182,21 @@ function DatePicker({
178
182
  showTime,
179
183
  icon,
180
184
  iconPosition = "end",
181
- className
185
+ className,
186
+ renderInput,
187
+ customInput
182
188
  }) {
183
189
  const timeFormat = resolveTimeFormat(showTime);
184
190
  const dateFormat = buildDateFormat(timeFormat);
185
191
  const maxDigits = buildMaxDigits(timeFormat);
186
192
  const defaultPlaceholder = placeholder != null ? placeholder : buildPlaceholder(timeFormat);
187
193
  const showSeconds = timeFormat === "HH:mm:ss";
194
+ const fromDay = fromDate ? (0, import_date_fns.startOfDay)(fromDate) : void 0;
195
+ const toDay = toDate ? (0, import_date_fns.startOfDay)(toDate) : void 0;
196
+ const disabledDays = [
197
+ ...fromDay ? [{ before: fromDay }] : [],
198
+ ...toDay ? [{ after: toDay }] : []
199
+ ];
188
200
  const resolvedIcon = loading ? /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(Spinner, {}) : icon === false ? null : icon != null ? icon : /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(CalendarIcon, {});
189
201
  const isControlled = value !== void 0;
190
202
  const [internalDate, setInternalDate] = (0, import_react3.useState)(defaultValue);
@@ -301,7 +313,7 @@ function DatePicker({
301
313
  if (!timeFormat) setOpen(false);
302
314
  return;
303
315
  }
304
- let dateToCommit = date;
316
+ let dateToCommit;
305
317
  if (timeFormat) {
306
318
  const base = selected && (0, import_date_fns.isValid)(selected) ? selected : /* @__PURE__ */ new Date(0);
307
319
  dateToCommit = new Date(
@@ -312,6 +324,8 @@ function DatePicker({
312
324
  base.getMinutes(),
313
325
  base.getSeconds()
314
326
  );
327
+ } else {
328
+ dateToCommit = toDateOnly(date);
315
329
  }
316
330
  applyValid((0, import_date_fns.format)(dateToCommit, dateFormat), dateToCommit);
317
331
  if (!timeFormat) setOpen(false);
@@ -332,10 +346,13 @@ function DatePicker({
332
346
  "data-failed": failed || inputInvalid || void 0,
333
347
  "data-disabled": !interactive || void 0,
334
348
  children: [
335
- /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(
349
+ customInput ? (0, import_react3.cloneElement)(customInput, {
350
+ value: inputValue,
351
+ onClick: () => interactive && setOpen((prev) => !prev)
352
+ }) : /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(
336
353
  "div",
337
354
  {
338
- className: "datepicker__field",
355
+ className: ["datepicker__field", renderInput ? "datepicker__field--custom" : ""].filter(Boolean).join(" "),
339
356
  "data-icon-start": resolvedIcon && iconPosition === "start" ? true : void 0,
340
357
  "data-icon-end": resolvedIcon && iconPosition === "end" ? true : void 0,
341
358
  onClick: () => {
@@ -345,9 +362,8 @@ function DatePicker({
345
362
  children: [
346
363
  resolvedIcon && iconPosition === "start" && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { className: "datepicker__icon datepicker__icon--start", children: resolvedIcon }),
347
364
  label && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { className: "datepicker__label", children: label }),
348
- /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
349
- "input",
350
- {
365
+ (() => {
366
+ const inputProps = {
351
367
  ref: inputRef,
352
368
  type: "text",
353
369
  inputMode: "numeric",
@@ -367,8 +383,11 @@ function DatePicker({
367
383
  "aria-expanded": !noCalendar ? open : void 0,
368
384
  "aria-haspopup": !noCalendar ? "dialog" : void 0,
369
385
  "aria-invalid": inputInvalid || void 0
370
- }
371
- ),
386
+ };
387
+ if (renderInput) return renderInput(inputProps);
388
+ const { ref, ...rest } = inputProps;
389
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("input", { ref, ...rest });
390
+ })(),
372
391
  resolvedIcon && iconPosition === "end" && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { className: "datepicker__icon datepicker__icon--end", children: resolvedIcon })
373
392
  ]
374
393
  }
@@ -391,8 +410,10 @@ function DatePicker({
391
410
  mode: "single",
392
411
  selected,
393
412
  onSelect: handleCalendarSelect,
394
- startMonth: fromDate,
395
- endMonth: toDate,
413
+ startMonth: fromDay,
414
+ endMonth: toDay,
415
+ disabled: disabledDays.length ? disabledDays : void 0,
416
+ navLayout: "around",
396
417
  locale: import_locale.ru
397
418
  }
398
419
  ) }),
@@ -421,8 +442,10 @@ function DatePicker({
421
442
  mode: "single",
422
443
  selected,
423
444
  onSelect: handleCalendarSelect,
424
- startMonth: fromDate,
425
- endMonth: toDate,
445
+ startMonth: fromDay,
446
+ endMonth: toDay,
447
+ disabled: disabledDays.length ? disabledDays : void 0,
448
+ navLayout: "around",
426
449
  locale: import_locale.ru
427
450
  }
428
451
  )
@@ -487,10 +510,14 @@ function getRangeCursorPos(masked, digitCount) {
487
510
  }
488
511
  return masked.length;
489
512
  }
513
+ function toDateOnly2(date) {
514
+ return new Date(date.getFullYear(), date.getMonth(), date.getDate(), 12, 0, 0);
515
+ }
490
516
  function parseDate(masked) {
491
517
  if (masked.replace(/\D/g, "").length !== 8) return void 0;
492
518
  const date = (0, import_date_fns2.parse)(masked, DATE_FORMAT2, /* @__PURE__ */ new Date());
493
- return (0, import_date_fns2.isValid)(date) && (0, import_date_fns2.format)(date, DATE_FORMAT2) === masked ? date : void 0;
519
+ if (!(0, import_date_fns2.isValid)(date) || (0, import_date_fns2.format)(date, DATE_FORMAT2) !== masked) return void 0;
520
+ return toDateOnly2(date);
494
521
  }
495
522
  function formatRange(from, to) {
496
523
  if (!from) return "";
@@ -514,7 +541,7 @@ function DateRangePicker({
514
541
  failed = false,
515
542
  loading = false,
516
543
  size = "m",
517
- calendarLayout = "vertical",
544
+ calendarLayout = "horizontal",
518
545
  showTime,
519
546
  icon,
520
547
  iconPosition = "end",
@@ -523,8 +550,18 @@ function DateRangePicker({
523
550
  const resolvedIcon = loading ? /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(Spinner, {}) : icon === false ? null : icon != null ? icon : /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(CalendarIcon, {});
524
551
  const isControlled = value !== void 0;
525
552
  const showSeconds = resolveShowSeconds(showTime);
526
- const [internalFrom, setInternalFrom] = (0, import_react4.useState)(defaultValue == null ? void 0 : defaultValue.from);
527
- const [internalTo, setInternalTo] = (0, import_react4.useState)(defaultValue == null ? void 0 : defaultValue.to);
553
+ const fromDay = fromConstraint ? (0, import_date_fns2.startOfDay)(fromConstraint) : void 0;
554
+ const toDay = toConstraint ? (0, import_date_fns2.startOfDay)(toConstraint) : void 0;
555
+ const disabledDays = [
556
+ ...fromDay ? [{ before: fromDay }] : [],
557
+ ...toDay ? [{ after: toDay }] : []
558
+ ];
559
+ const [internalFrom, setInternalFrom] = (0, import_react4.useState)(
560
+ defaultValue == null ? void 0 : defaultValue.from
561
+ );
562
+ const [internalTo, setInternalTo] = (0, import_react4.useState)(
563
+ defaultValue == null ? void 0 : defaultValue.to
564
+ );
528
565
  const [inputValue, setInputValue] = (0, import_react4.useState)(
529
566
  () => formatRange(defaultValue == null ? void 0 : defaultValue.from, defaultValue == null ? void 0 : defaultValue.to)
530
567
  );
@@ -535,8 +572,12 @@ function DateRangePicker({
535
572
  const [hoveredDate, setHoveredDate] = (0, import_react4.useState)(void 0);
536
573
  const inputRef = (0, import_react4.useRef)(null);
537
574
  const containerRef = (0, import_react4.useRef)(null);
538
- const lastEmittedFromRef = (0, import_react4.useRef)(value !== void 0 ? value == null ? void 0 : value.from : defaultValue == null ? void 0 : defaultValue.from);
539
- const lastEmittedToRef = (0, import_react4.useRef)(value !== void 0 ? value == null ? void 0 : value.to : defaultValue == null ? void 0 : defaultValue.to);
575
+ const lastEmittedFromRef = (0, import_react4.useRef)(
576
+ value !== void 0 ? value == null ? void 0 : value.from : defaultValue == null ? void 0 : defaultValue.from
577
+ );
578
+ const lastEmittedToRef = (0, import_react4.useRef)(
579
+ value !== void 0 ? value == null ? void 0 : value.to : defaultValue == null ? void 0 : defaultValue.to
580
+ );
540
581
  const wasControlledRef = (0, import_react4.useRef)(value !== void 0);
541
582
  const confirmedFrom = isControlled ? value == null ? void 0 : value.from : internalFrom;
542
583
  const confirmedTo = isControlled ? value == null ? void 0 : value.to : internalTo;
@@ -580,7 +621,7 @@ function DateRangePicker({
580
621
  (_a = confirmedFrom == null ? void 0 : confirmedFrom.getHours()) != null ? _a : 0,
581
622
  (_b = confirmedFrom == null ? void 0 : confirmedFrom.getMinutes()) != null ? _b : 0,
582
623
  (_c = confirmedFrom == null ? void 0 : confirmedFrom.getSeconds()) != null ? _c : 0
583
- ) : day;
624
+ ) : toDateOnly2(day);
584
625
  setAnchorDate(from);
585
626
  if (!isControlled) {
586
627
  setInternalFrom(from);
@@ -599,7 +640,7 @@ function DateRangePicker({
599
640
  (_d = confirmedTo == null ? void 0 : confirmedTo.getHours()) != null ? _d : 0,
600
641
  (_e = confirmedTo == null ? void 0 : confirmedTo.getMinutes()) != null ? _e : 0,
601
642
  (_f = confirmedTo == null ? void 0 : confirmedTo.getSeconds()) != null ? _f : 0
602
- ) : day;
643
+ ) : toDateOnly2(day);
603
644
  if (day < anchorDate) {
604
645
  const tmp = from;
605
646
  from = to;
@@ -623,14 +664,28 @@ function DateRangePicker({
623
664
  }
624
665
  function handleFromTimeChange(h, m, s) {
625
666
  const base = confirmedFrom != null ? confirmedFrom : /* @__PURE__ */ new Date();
626
- const newDate = new Date(base.getFullYear(), base.getMonth(), base.getDate(), h, m, s);
667
+ const newDate = new Date(
668
+ base.getFullYear(),
669
+ base.getMonth(),
670
+ base.getDate(),
671
+ h,
672
+ m,
673
+ s
674
+ );
627
675
  if (!isControlled) setInternalFrom(newDate);
628
676
  lastEmittedFromRef.current = newDate;
629
677
  onChange == null ? void 0 : onChange({ from: newDate, to: confirmedTo });
630
678
  }
631
679
  function handleToTimeChange(h, m, s) {
632
680
  const base = confirmedTo != null ? confirmedTo : /* @__PURE__ */ new Date();
633
- const newDate = new Date(base.getFullYear(), base.getMonth(), base.getDate(), h, m, s);
681
+ const newDate = new Date(
682
+ base.getFullYear(),
683
+ base.getMonth(),
684
+ base.getDate(),
685
+ h,
686
+ m,
687
+ s
688
+ );
634
689
  if (!isControlled) setInternalTo(newDate);
635
690
  lastEmittedToRef.current = newDate;
636
691
  onChange == null ? void 0 : onChange({ from: confirmedFrom, to: newDate });
@@ -659,7 +714,9 @@ function DateRangePicker({
659
714
  }
660
715
  lastEmittedFromRef.current = parsedFrom;
661
716
  lastEmittedToRef.current = parsedTo;
662
- onChange == null ? void 0 : onChange(parsedFrom || parsedTo ? { from: parsedFrom, to: parsedTo } : void 0);
717
+ onChange == null ? void 0 : onChange(
718
+ parsedFrom || parsedTo ? { from: parsedFrom, to: parsedTo } : void 0
719
+ );
663
720
  requestAnimationFrame(
664
721
  () => {
665
722
  var _a2;
@@ -683,9 +740,13 @@ function DateRangePicker({
683
740
  const val = input.value;
684
741
  const charsToSkip = (_c = (_b = val.slice(0, pos).match(/[\s—]+$/)) == null ? void 0 : _b[0].length) != null ? _c : 1;
685
742
  const newPos = pos - charsToSkip;
686
- const masked = applyRangeMask((val.slice(0, newPos - 1) + val.slice(newPos)).replace(/\D/g, ""));
743
+ const masked = applyRangeMask(
744
+ (val.slice(0, newPos - 1) + val.slice(newPos)).replace(/\D/g, "")
745
+ );
687
746
  setInputValue(masked);
688
- requestAnimationFrame(() => input.setSelectionRange(newPos - 1, newPos - 1));
747
+ requestAnimationFrame(
748
+ () => input.setSelectionRange(newPos - 1, newPos - 1)
749
+ );
689
750
  }
690
751
  }
691
752
  function handlePaste(e) {
@@ -698,18 +759,24 @@ function DateRangePicker({
698
759
  setHoveredDate(void 0);
699
760
  const parsedFrom = digits.length >= 8 ? parseDate(applyDateMask(digits.slice(0, 8))) : void 0;
700
761
  const parsedTo = digits.length >= 16 ? parseDate(applyDateMask(digits.slice(8, 16))) : void 0;
701
- setInputInvalid(digits.length >= 8 && !parsedFrom || digits.length >= 16 && !parsedTo);
762
+ setInputInvalid(
763
+ digits.length >= 8 && !parsedFrom || digits.length >= 16 && !parsedTo
764
+ );
702
765
  if (!isControlled) {
703
766
  setInternalFrom(parsedFrom);
704
767
  setInternalTo(parsedTo);
705
768
  }
706
769
  lastEmittedFromRef.current = parsedFrom;
707
770
  lastEmittedToRef.current = parsedTo;
708
- onChange == null ? void 0 : onChange(parsedFrom || parsedTo ? { from: parsedFrom, to: parsedTo } : void 0);
709
- requestAnimationFrame(() => {
710
- var _a;
711
- return (_a = inputRef.current) == null ? void 0 : _a.setSelectionRange(masked.length, masked.length);
712
- });
771
+ onChange == null ? void 0 : onChange(
772
+ parsedFrom || parsedTo ? { from: parsedFrom, to: parsedTo } : void 0
773
+ );
774
+ requestAnimationFrame(
775
+ () => {
776
+ var _a;
777
+ return (_a = inputRef.current) == null ? void 0 : _a.setSelectionRange(masked.length, masked.length);
778
+ }
779
+ );
713
780
  }
714
781
  const placeholder = label && !focused && !filled ? void 0 : "\u0434\u0434.\u043C\u043C.\u0433\u0433\u0433\u0433 \u2014 \u0434\u0434.\u043C\u043C.\u0433\u0433\u0433\u0433";
715
782
  const interactive = !disabled && !loading;
@@ -717,7 +784,12 @@ function DateRangePicker({
717
784
  "div",
718
785
  {
719
786
  ref: containerRef,
720
- className: ["datepicker", "daterangepicker", `datepicker--${size}`, className].filter(Boolean).join(" "),
787
+ className: [
788
+ "datepicker",
789
+ "daterangepicker",
790
+ `datepicker--${size}`,
791
+ className
792
+ ].filter(Boolean).join(" "),
721
793
  "data-focused": focused || open || void 0,
722
794
  "data-filled": filled || void 0,
723
795
  "data-failed": failed || inputInvalid || void 0,
@@ -786,8 +858,9 @@ function DateRangePicker({
786
858
  onDayClick: handleDayClick,
787
859
  onDayMouseEnter: handleDayMouseEnter,
788
860
  onDayMouseLeave: () => setHoveredDate(void 0),
789
- startMonth: fromConstraint,
790
- endMonth: toConstraint,
861
+ startMonth: fromDay,
862
+ endMonth: toDay,
863
+ disabled: disabledDays.length ? disabledDays : void 0,
791
864
  numberOfMonths: 2,
792
865
  locale: import_locale2.ru
793
866
  }
@@ -795,15 +868,37 @@ function DateRangePicker({
795
868
  /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { className: "datepicker__time-row", children: [
796
869
  /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { className: "datepicker__time-col", children: [
797
870
  /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("span", { className: "datepicker__time-label", children: "\u041D\u0430\u0447\u0430\u043B\u043E" }),
798
- /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(TimePanel, { value: confirmedFrom, showSeconds, onChange: handleFromTimeChange })
871
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
872
+ TimePanel,
873
+ {
874
+ value: confirmedFrom,
875
+ showSeconds,
876
+ onChange: handleFromTimeChange
877
+ }
878
+ )
799
879
  ] }),
800
880
  /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("div", { className: "datepicker__time-separator" }),
801
881
  /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { className: "datepicker__time-col", children: [
802
882
  /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("span", { className: "datepicker__time-label", children: "\u041A\u043E\u043D\u0435\u0446" }),
803
- /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(TimePanel, { value: confirmedTo, showSeconds, onChange: handleToTimeChange })
883
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
884
+ TimePanel,
885
+ {
886
+ value: confirmedTo,
887
+ showSeconds,
888
+ onChange: handleToTimeChange
889
+ }
890
+ )
804
891
  ] })
805
892
  ] }),
806
- /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("div", { className: "datepicker__popover-footer", children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("button", { className: "datepicker__ok-btn", type: "button", onClick: close, children: "OK" }) })
893
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("div", { className: "datepicker__popover-footer", children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
894
+ "button",
895
+ {
896
+ className: "datepicker__ok-btn",
897
+ type: "button",
898
+ onClick: close,
899
+ children: "OK"
900
+ }
901
+ ) })
807
902
  ] }) : /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
808
903
  Calendar,
809
904
  {