best-react-datepicker 0.2.1 → 0.2.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.cjs CHANGED
@@ -22,8 +22,48 @@ var DEFAULT_MONTH_NAMES = [
22
22
  "November",
23
23
  "December"
24
24
  ];
25
- function SelectChevron() {
26
- return /* @__PURE__ */ jsxRuntime.jsx("span", { className: "brdp-header__select-chevron", "aria-hidden": "true", children: /* @__PURE__ */ jsxRuntime.jsx("svg", { width: "10", height: "10", viewBox: "0 0 10 10", fill: "none", children: /* @__PURE__ */ jsxRuntime.jsx("path", { d: "M2.5 3.75L5 6.25L7.5 3.75", stroke: "currentColor", strokeWidth: "1.5", strokeLinecap: "round", strokeLinejoin: "round" }) }) });
25
+ var SHORT_MONTH_NAMES = [
26
+ "Jan",
27
+ "Feb",
28
+ "Mar",
29
+ "Apr",
30
+ "May",
31
+ "Jun",
32
+ "Jul",
33
+ "Aug",
34
+ "Sep",
35
+ "Oct",
36
+ "Nov",
37
+ "Dec"
38
+ ];
39
+ function ChevronDown() {
40
+ return /* @__PURE__ */ jsxRuntime.jsx("svg", { width: "12", height: "12", viewBox: "0 0 12 12", fill: "none", "aria-hidden": "true", style: { marginLeft: 2, opacity: 0.5 }, children: /* @__PURE__ */ jsxRuntime.jsx("path", { d: "M3 4.5L6 7.5L9 4.5", stroke: "currentColor", strokeWidth: "1.5", strokeLinecap: "round", strokeLinejoin: "round" }) });
41
+ }
42
+ function Dropdown({
43
+ isOpen,
44
+ onClose,
45
+ children
46
+ }) {
47
+ const ref = react.useRef(null);
48
+ react.useEffect(() => {
49
+ if (!isOpen) return;
50
+ const handleClick = (e) => {
51
+ if (ref.current && !ref.current.contains(e.target)) {
52
+ onClose();
53
+ }
54
+ };
55
+ const handleKey = (e) => {
56
+ if (e.key === "Escape") onClose();
57
+ };
58
+ document.addEventListener("mousedown", handleClick);
59
+ document.addEventListener("keydown", handleKey);
60
+ return () => {
61
+ document.removeEventListener("mousedown", handleClick);
62
+ document.removeEventListener("keydown", handleKey);
63
+ };
64
+ }, [isOpen, onClose]);
65
+ if (!isOpen) return null;
66
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { ref, className: "brdp-dropdown", children });
27
67
  }
28
68
  function CalendarHeader({
29
69
  label,
@@ -44,24 +84,25 @@ function CalendarHeader({
44
84
  monthNames
45
85
  }) {
46
86
  const hasDropdowns = viewingMonth != null && viewingYear != null && onMonthChange && onYearChange;
87
+ const [openDropdown, setOpenDropdown] = react.useState(null);
88
+ const yearGridRef = react.useRef(null);
47
89
  const currentYear = (/* @__PURE__ */ new Date()).getFullYear();
48
90
  const minYear = minDate ? minDate.getFullYear() : currentYear - 100;
49
91
  const maxYear = maxDate ? maxDate.getFullYear() : currentYear + 20;
50
92
  const names = monthNames ?? DEFAULT_MONTH_NAMES;
51
- const getMonthOptions = () => {
52
- const options = [];
53
- for (let m = 0; m < 12; m++) {
54
- let disabled = false;
55
- if (minDate && viewingYear != null && viewingYear === minDate.getFullYear() && m < minDate.getMonth()) {
56
- disabled = true;
57
- }
58
- if (maxDate && viewingYear != null && viewingYear === maxDate.getFullYear() && m > maxDate.getMonth()) {
59
- disabled = true;
93
+ const isMonthDisabled = (m) => {
94
+ if (minDate && viewingYear != null && viewingYear === minDate.getFullYear() && m < minDate.getMonth()) return true;
95
+ if (maxDate && viewingYear != null && viewingYear === maxDate.getFullYear() && m > maxDate.getMonth()) return true;
96
+ return false;
97
+ };
98
+ react.useEffect(() => {
99
+ if (openDropdown === "year" && yearGridRef.current) {
100
+ const active = yearGridRef.current.querySelector(".brdp-dropdown__item--active");
101
+ if (active) {
102
+ active.scrollIntoView({ block: "center", behavior: "instant" });
60
103
  }
61
- options.push({ value: m, label: names[m], disabled });
62
104
  }
63
- return options;
64
- };
105
+ }, [openDropdown]);
65
106
  return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: `brdp-header ${className ?? ""}`, children: [
66
107
  showPrev ? /* @__PURE__ */ jsxRuntime.jsx(
67
108
  "button",
@@ -74,31 +115,64 @@ function CalendarHeader({
74
115
  }
75
116
  ) : /* @__PURE__ */ jsxRuntime.jsx("span", { className: "brdp-header__nav-btn", style: { visibility: "hidden" } }),
76
117
  hasDropdowns ? /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "brdp-header__selects", children: [
77
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "brdp-header__select-wrapper", children: [
78
- /* @__PURE__ */ jsxRuntime.jsx(
79
- "select",
118
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "brdp-header__dropdown-anchor", children: [
119
+ /* @__PURE__ */ jsxRuntime.jsxs(
120
+ "button",
80
121
  {
81
- className: "brdp-header__select",
82
- value: viewingMonth,
83
- onChange: (e) => onMonthChange(Number(e.target.value)),
122
+ type: "button",
123
+ className: `brdp-header__trigger ${openDropdown === "month" ? "brdp-header__trigger--open" : ""}`,
124
+ onClick: () => setOpenDropdown(openDropdown === "month" ? null : "month"),
84
125
  "aria-label": "Select month",
85
- children: getMonthOptions().map((opt) => /* @__PURE__ */ jsxRuntime.jsx("option", { value: opt.value, disabled: opt.disabled, children: opt.label }, opt.value))
126
+ "aria-expanded": openDropdown === "month",
127
+ children: [
128
+ names[viewingMonth],
129
+ /* @__PURE__ */ jsxRuntime.jsx(ChevronDown, {})
130
+ ]
86
131
  }
87
132
  ),
88
- /* @__PURE__ */ jsxRuntime.jsx(SelectChevron, {})
133
+ /* @__PURE__ */ jsxRuntime.jsx(Dropdown, { isOpen: openDropdown === "month", onClose: () => setOpenDropdown(null), children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "brdp-dropdown__grid brdp-dropdown__grid--months", children: SHORT_MONTH_NAMES.map((short, m) => /* @__PURE__ */ jsxRuntime.jsx(
134
+ "button",
135
+ {
136
+ type: "button",
137
+ className: `brdp-dropdown__item ${viewingMonth === m ? "brdp-dropdown__item--active" : ""} ${isMonthDisabled(m) ? "brdp-dropdown__item--disabled" : ""}`,
138
+ disabled: isMonthDisabled(m),
139
+ onClick: () => {
140
+ onMonthChange(m);
141
+ setOpenDropdown(null);
142
+ },
143
+ children: short
144
+ },
145
+ m
146
+ )) }) })
89
147
  ] }),
90
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "brdp-header__select-wrapper", children: [
91
- /* @__PURE__ */ jsxRuntime.jsx(
92
- "select",
148
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "brdp-header__dropdown-anchor", children: [
149
+ /* @__PURE__ */ jsxRuntime.jsxs(
150
+ "button",
93
151
  {
94
- className: "brdp-header__select",
95
- value: viewingYear,
96
- onChange: (e) => onYearChange(Number(e.target.value)),
152
+ type: "button",
153
+ className: `brdp-header__trigger ${openDropdown === "year" ? "brdp-header__trigger--open" : ""}`,
154
+ onClick: () => setOpenDropdown(openDropdown === "year" ? null : "year"),
97
155
  "aria-label": "Select year",
98
- children: Array.from({ length: maxYear - minYear + 1 }, (_, i) => minYear + i).map((y) => /* @__PURE__ */ jsxRuntime.jsx("option", { value: y, children: y }, y))
156
+ "aria-expanded": openDropdown === "year",
157
+ children: [
158
+ viewingYear,
159
+ /* @__PURE__ */ jsxRuntime.jsx(ChevronDown, {})
160
+ ]
99
161
  }
100
162
  ),
101
- /* @__PURE__ */ jsxRuntime.jsx(SelectChevron, {})
163
+ /* @__PURE__ */ jsxRuntime.jsx(Dropdown, { isOpen: openDropdown === "year", onClose: () => setOpenDropdown(null), children: /* @__PURE__ */ jsxRuntime.jsx("div", { ref: yearGridRef, className: "brdp-dropdown__grid brdp-dropdown__grid--years", children: Array.from({ length: maxYear - minYear + 1 }, (_, i) => minYear + i).map((y) => /* @__PURE__ */ jsxRuntime.jsx(
164
+ "button",
165
+ {
166
+ type: "button",
167
+ className: `brdp-dropdown__item ${viewingYear === y ? "brdp-dropdown__item--active" : ""}`,
168
+ onClick: () => {
169
+ onYearChange(y);
170
+ setOpenDropdown(null);
171
+ },
172
+ children: y
173
+ },
174
+ y
175
+ )) }) })
102
176
  ] })
103
177
  ] }) : /* @__PURE__ */ jsxRuntime.jsx(
104
178
  "button",