pixelize-design-library 3.3.1 → 3.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +39 -0
- package/dist/Components/DatePicker/CalendarPanel.js +12 -33
- package/dist/Components/DatePicker/RangeDatePicker.js +2 -2
- package/dist/Components/DatePicker/SingleDatePicker.js +102 -99
- package/dist/Components/DatePicker/TimePickerInput.js +29 -21
- package/dist/Components/Select/SelectPopup.js +24 -2
- package/dist/Hooks/exclusiveOverlay.d.ts +4 -1
- package/dist/Hooks/exclusiveOverlay.js +14 -9
- package/dist/Hooks/useExclusiveOverlay.d.ts +2 -1
- package/dist/Hooks/useExclusiveOverlay.js +7 -4
- package/dist/Hooks/useOutsideMouseDown.js +16 -1
- package/dist/esm/Components/DatePicker/CalendarPanel.js +12 -33
- package/dist/esm/Components/DatePicker/RangeDatePicker.js +2 -2
- package/dist/esm/Components/DatePicker/SingleDatePicker.js +102 -99
- package/dist/esm/Components/DatePicker/TimePickerInput.js +30 -22
- package/dist/esm/Components/Select/SelectPopup.js +24 -2
- package/dist/esm/Hooks/exclusiveOverlay.js +14 -9
- package/dist/esm/Hooks/useExclusiveOverlay.js +7 -4
- package/dist/esm/Hooks/useOutsideMouseDown.js +16 -1
- package/package.json +1 -1
|
@@ -1,22 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.resetExclusiveOverlay = exports.exclusiveClose = exports.exclusiveOpen = void 0;
|
|
4
|
-
let
|
|
5
|
-
const exclusiveOpen = (id, close) => {
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
let stack = [];
|
|
5
|
+
const exclusiveOpen = (id, close, options = {}) => {
|
|
6
|
+
var _a;
|
|
7
|
+
stack = stack.filter((entry) => entry.id !== id);
|
|
8
|
+
const { anchor } = options;
|
|
9
|
+
while (stack.length > 0) {
|
|
10
|
+
const top = stack[stack.length - 1];
|
|
11
|
+
if (anchor && ((_a = top.contains) === null || _a === void 0 ? void 0 : _a.call(top, anchor)))
|
|
12
|
+
break;
|
|
13
|
+
stack.pop();
|
|
14
|
+
top.close();
|
|
8
15
|
}
|
|
9
|
-
|
|
16
|
+
stack.push({ id, close, contains: options.contains });
|
|
10
17
|
};
|
|
11
18
|
exports.exclusiveOpen = exclusiveOpen;
|
|
12
19
|
const exclusiveClose = (id) => {
|
|
13
|
-
|
|
14
|
-
active = null;
|
|
15
|
-
}
|
|
20
|
+
stack = stack.filter((entry) => entry.id !== id);
|
|
16
21
|
};
|
|
17
22
|
exports.exclusiveClose = exclusiveClose;
|
|
18
23
|
/** Test-only reset — not exported from the package root. */
|
|
19
24
|
const resetExclusiveOverlay = () => {
|
|
20
|
-
|
|
25
|
+
stack = [];
|
|
21
26
|
};
|
|
22
27
|
exports.resetExclusiveOverlay = resetExclusiveOverlay;
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { type RefObject } from "react";
|
|
2
|
+
export declare const useExclusiveOverlay: (isOpen: boolean, onClose: () => void, contentRef?: RefObject<Element | null>) => (anchor?: Node | null) => void;
|
|
@@ -4,13 +4,16 @@ exports.useExclusiveOverlay = void 0;
|
|
|
4
4
|
const react_1 = require("react");
|
|
5
5
|
const exclusiveOverlay_1 = require("./exclusiveOverlay");
|
|
6
6
|
// NOTE: registers with the global dismiss coordinator — call requestOpen() before opening.
|
|
7
|
-
const useExclusiveOverlay = (isOpen, onClose) => {
|
|
7
|
+
const useExclusiveOverlay = (isOpen, onClose, contentRef) => {
|
|
8
8
|
const id = (0, react_1.useId)();
|
|
9
9
|
const onCloseRef = (0, react_1.useRef)(onClose);
|
|
10
10
|
onCloseRef.current = onClose;
|
|
11
|
-
const requestOpen = (0, react_1.useCallback)(() => {
|
|
12
|
-
(0, exclusiveOverlay_1.exclusiveOpen)(id, () => onCloseRef.current()
|
|
13
|
-
|
|
11
|
+
const requestOpen = (0, react_1.useCallback)((anchor) => {
|
|
12
|
+
(0, exclusiveOverlay_1.exclusiveOpen)(id, () => onCloseRef.current(), {
|
|
13
|
+
anchor,
|
|
14
|
+
contains: contentRef ? (node) => { var _a; return Boolean((_a = contentRef.current) === null || _a === void 0 ? void 0 : _a.contains(node)); } : undefined,
|
|
15
|
+
});
|
|
16
|
+
}, [id, contentRef]);
|
|
14
17
|
(0, react_1.useEffect)(() => {
|
|
15
18
|
if (!isOpen) {
|
|
16
19
|
(0, exclusiveOverlay_1.exclusiveClose)(id);
|
|
@@ -2,6 +2,19 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useOutsideMouseDown = void 0;
|
|
4
4
|
const react_1 = require("react");
|
|
5
|
+
const ownedIds = (root) => Array.from(root.querySelectorAll("[aria-controls], [aria-owns]")).flatMap((el) => { var _a, _b; return `${(_a = el.getAttribute("aria-controls")) !== null && _a !== void 0 ? _a : ""} ${(_b = el.getAttribute("aria-owns")) !== null && _b !== void 0 ? _b : ""}`.split(/\s+/).filter(Boolean); });
|
|
6
|
+
const ownsTarget = (root, target) => {
|
|
7
|
+
if (!root)
|
|
8
|
+
return false;
|
|
9
|
+
const ids = ownedIds(root);
|
|
10
|
+
if (ids.length === 0)
|
|
11
|
+
return false;
|
|
12
|
+
for (let node = target instanceof Element ? target : target.parentElement; node; node = node.parentElement) {
|
|
13
|
+
if (node.id && ids.includes(node.id))
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
return false;
|
|
17
|
+
};
|
|
5
18
|
/** Calls `onOutside` for a document `mousedown` outside every ref; a null ref contains nothing. Unlike Chakra's `useOutsideClick` it never waits for `mouseup`. */
|
|
6
19
|
const useOutsideMouseDown = (refs, onOutside, { enabled = true, capture = false, includeClick = false } = {}) => {
|
|
7
20
|
const latest = (0, react_1.useRef)({ refs, onOutside });
|
|
@@ -13,7 +26,9 @@ const useOutsideMouseDown = (refs, onOutside, { enabled = true, capture = false,
|
|
|
13
26
|
return;
|
|
14
27
|
const handle = (event) => {
|
|
15
28
|
const target = event.target;
|
|
16
|
-
if (
|
|
29
|
+
if (!target.isConnected)
|
|
30
|
+
return;
|
|
31
|
+
if (latest.current.refs.some((ref) => { var _a; return ((_a = ref.current) === null || _a === void 0 ? void 0 : _a.contains(target)) || ownsTarget(ref.current, target); }))
|
|
17
32
|
return;
|
|
18
33
|
latest.current.onOutside(event);
|
|
19
34
|
};
|
|
@@ -3,11 +3,13 @@ import { Box } from "@chakra-ui/react";
|
|
|
3
3
|
import { format, startOfMonth, endOfMonth, startOfWeek, endOfWeek, addDays } from "date-fns";
|
|
4
4
|
import { ChevronLeftIcon, ChevronRightIcon } from "lucide-react";
|
|
5
5
|
import Button from "../Button/Button.js";
|
|
6
|
+
import Select from "../Select/Select.js";
|
|
6
7
|
import { useCustomTheme } from "../../Theme/useCustomTheme.js";
|
|
7
8
|
import { resolveFilledSurface } from "../../Theme/tokens/builders/filledSurface.js";
|
|
8
|
-
import { fieldFocusVisible } from "../../Theme/chakra/focusRing.styles.js";
|
|
9
9
|
import { DAY_CLASS, dayAccessibleName, dayStateClassName, resolveDayStateVisual, } from "./dayStates.js";
|
|
10
10
|
import { transitionFor } from "../../Theme/tokens/primitives/motion.js";
|
|
11
|
+
const MONTH_OPTIONS = Array.from({ length: 12 }, (_, i) => ({ id: i, label: format(new Date(2000, i, 1), "MMMM") }));
|
|
12
|
+
const yearOptions = (thisYear) => Array.from({ length: 100 }, (_, i) => ({ id: thisYear - 50 + i, label: String(thisYear - 50 + i) }));
|
|
11
13
|
const BACKGROUND_COLOR_TRANSITION = transitionFor(["background", "color"]);
|
|
12
14
|
/** Internal month grid shared by Single/RangeDatePicker; not published from index.ts, so only reachable via a deep import. */
|
|
13
15
|
export const CalendarPanel = ({ currentMonth, setCurrentMonth, today, isRange, tempDate, tempRangeStart, tempRangeEnd, isSameDay, isBefore, isAfter, minDate, maxDate, disablePastDates, disableFutureDates, disableToday, onDaySelect, hoveredDate, onDayHover, renderWeekdays, dayStates, renderDayContent, }) => {
|
|
@@ -23,28 +25,6 @@ export const CalendarPanel = ({ currentMonth, setCurrentMonth, today, isRange, t
|
|
|
23
25
|
const rangeEnd = tempRangeEnd !== null && tempRangeEnd !== void 0 ? tempRangeEnd : tentativeEnd;
|
|
24
26
|
const isTentative = !tempRangeEnd && !!tentativeEnd;
|
|
25
27
|
const hasRange = !!(rangeStart && rangeEnd && !isSameDay(rangeStart, rangeEnd));
|
|
26
|
-
const selectStyles = {
|
|
27
|
-
border: "1px solid",
|
|
28
|
-
// boxborder[500] is the control-outline rung the sibling pickers already use; boxborder[200]
|
|
29
|
-
// is decorative and measured 1.85:1 on the dark canvas — the select read as borderless.
|
|
30
|
-
borderColor: theme.colors.boxborder[500],
|
|
31
|
-
borderRadius: "md",
|
|
32
|
-
px: 2,
|
|
33
|
-
py: 1,
|
|
34
|
-
fontSize: "xs",
|
|
35
|
-
fontWeight: 600,
|
|
36
|
-
color: theme.colors.text[700],
|
|
37
|
-
cursor: "pointer",
|
|
38
|
-
bg: theme.colors.background[50],
|
|
39
|
-
outline: "none",
|
|
40
|
-
_hover: { borderColor: theme.colors.primary[300] },
|
|
41
|
-
_focus: { borderColor: theme.colors.primary[500], ...fieldFocusVisible(theme.colors) },
|
|
42
|
-
style: {
|
|
43
|
-
WebkitAppearance: "menulist",
|
|
44
|
-
MozAppearance: "menulist",
|
|
45
|
-
appearance: "menulist",
|
|
46
|
-
},
|
|
47
|
-
};
|
|
48
28
|
const renderDays = () => {
|
|
49
29
|
var _a;
|
|
50
30
|
const monthStart = startOfMonth(currentMonth);
|
|
@@ -133,16 +113,15 @@ export const CalendarPanel = ({ currentMonth, setCurrentMonth, today, isRange, t
|
|
|
133
113
|
}
|
|
134
114
|
return rows;
|
|
135
115
|
};
|
|
136
|
-
return (_jsxs(Box, { children: [_jsxs(Box, { display: "flex", alignItems: "center", justifyContent: "space-between", mb: 2, gap: 1, children: [_jsx(Button, { variant: "ghost", size: "xs", sx: { flexShrink: 0 }, onClick: () => setCurrentMonth((prev) => new Date(prev.getFullYear(), prev.getMonth() - 1, 1)), "aria-label": "Previous month", children: _jsx(ChevronLeftIcon, { size: 16 }) }), _jsxs(Box, { display: "flex", alignItems: "center", gap: 1.5, minW: 0, children: [_jsx(
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
}) })] }), _jsxs(Box, { display: "flex", alignItems: "center", gap: 1, flexShrink: 0, children: [!isRange && !isViewingCurrentMonth && (_jsx(Button, { variant: "ghost", size: "xs", onClick: () => {
|
|
116
|
+
return (_jsxs(Box, { children: [_jsxs(Box, { display: "flex", alignItems: "center", justifyContent: "space-between", mb: 2, gap: 1, children: [_jsx(Button, { variant: "ghost", size: "xs", sx: { flexShrink: 0 }, onClick: () => setCurrentMonth((prev) => new Date(prev.getFullYear(), prev.getMonth() - 1, 1)), "aria-label": "Previous month", children: _jsx(ChevronLeftIcon, { size: 16 }) }), _jsxs(Box, { display: "flex", alignItems: "center", gap: 1.5, minW: 0, children: [_jsx(Select, { native: false, size: "xs", width: "7.5rem", "aria-label": "Month", options: MONTH_OPTIONS, value: currentMonth.getMonth(), onChange: (option) => {
|
|
117
|
+
if (option === undefined)
|
|
118
|
+
return;
|
|
119
|
+
setCurrentMonth((prev) => new Date(prev.getFullYear(), Number(option.id), 1));
|
|
120
|
+
} }), _jsx(Select, { native: false, size: "xs", width: "5.5rem", "aria-label": "Year", options: yearOptions(new Date().getFullYear()), value: currentMonth.getFullYear(), onChange: (option) => {
|
|
121
|
+
if (option === undefined)
|
|
122
|
+
return;
|
|
123
|
+
setCurrentMonth((prev) => new Date(Number(option.id), prev.getMonth(), 1));
|
|
124
|
+
} })] }), _jsxs(Box, { display: "flex", alignItems: "center", gap: 1, flexShrink: 0, children: [!isRange && !isViewingCurrentMonth && (_jsx(Button, { variant: "ghost", size: "xs", onClick: () => {
|
|
146
125
|
setCurrentMonth(() => new Date(today.getFullYear(), today.getMonth(), 1));
|
|
147
126
|
if (!disableToday) {
|
|
148
127
|
onDaySelect(new Date(today));
|
|
@@ -34,13 +34,13 @@ export const RangeDatePicker = forwardRef(function RangeDatePicker(props, ref) {
|
|
|
34
34
|
const calendarIconPx = getTextInputIconSizePx(sizeKey);
|
|
35
35
|
const calendarIconWrapperSx = getTextInputIconWrapperSx(sizeKey);
|
|
36
36
|
const { isOpen, onOpen, onClose } = useDisclosure();
|
|
37
|
-
const
|
|
37
|
+
const popoverRef = useRef(null);
|
|
38
|
+
const requestExclusiveOpen = useExclusiveOverlay(isOpen, onClose, popoverRef);
|
|
38
39
|
const pickerPortal = usePickerPortal(isOpen);
|
|
39
40
|
const calendarLabel = label !== null && label !== void 0 ? label : "Choose date range";
|
|
40
41
|
const inputRef = useRef(null);
|
|
41
42
|
const mergedInputRef = useMergeRefs(inputRef, ref);
|
|
42
43
|
const triggerRef = useRef(null);
|
|
43
|
-
const popoverRef = useRef(null);
|
|
44
44
|
const today = new Date();
|
|
45
45
|
const placeholder = useMemo(() => placeholderText || `${dateFormat} - ${dateFormat}`, [placeholderText, dateFormat]);
|
|
46
46
|
const selectedRange = (_b = (_a = props.value) !== null && _a !== void 0 ? _a : props.selectedDate) !== null && _b !== void 0 ? _b : null;
|
|
@@ -157,63 +157,30 @@ const getFormatForPickerType = (pickerType, dateFormat) => {
|
|
|
157
157
|
}
|
|
158
158
|
return dateFormat;
|
|
159
159
|
};
|
|
160
|
-
const
|
|
160
|
+
const normalizeTwoDigitSegment = (buf, firstMax, min, max) => {
|
|
161
|
+
if (buf.length === 1) {
|
|
162
|
+
const d = parseInt(buf, 10);
|
|
163
|
+
return d > firstMax ? { value: `0${d}`, completed: true } : { value: buf, completed: false };
|
|
164
|
+
}
|
|
165
|
+
const n = Math.min(max, Math.max(min, parseInt(buf, 10)));
|
|
166
|
+
return { value: n.toString().padStart(2, "0"), completed: true };
|
|
167
|
+
};
|
|
168
|
+
const normalizeSegmentBuffer = (tokenChar, rawBuffer, segmentLength, is12Hour) => {
|
|
161
169
|
if (!tokenChar)
|
|
162
170
|
return { value: rawBuffer, completed: rawBuffer.length >= segmentLength };
|
|
163
171
|
const t = tokenChar.toLowerCase();
|
|
164
172
|
const buf = rawBuffer.replace(/\D/g, "").slice(0, segmentLength);
|
|
165
|
-
if (segmentLength !== 2) {
|
|
173
|
+
if (segmentLength !== 2 || buf.length === 0) {
|
|
166
174
|
return { value: buf, completed: buf.length >= segmentLength };
|
|
167
175
|
}
|
|
168
|
-
if (
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
return { value: buf, completed: false };
|
|
177
|
-
}
|
|
178
|
-
if (buf.length === 2) {
|
|
179
|
-
let h = parseInt(buf, 10);
|
|
180
|
-
if (is12Hour) {
|
|
181
|
-
if (h < 1)
|
|
182
|
-
h = 1;
|
|
183
|
-
if (h > 12)
|
|
184
|
-
h = 12;
|
|
185
|
-
}
|
|
186
|
-
else {
|
|
187
|
-
if (h < 1)
|
|
188
|
-
h = 1;
|
|
189
|
-
if (h > 24)
|
|
190
|
-
h = 24;
|
|
191
|
-
}
|
|
192
|
-
const out = h.toString().padStart(2, "0");
|
|
193
|
-
return { value: out, completed: true };
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
if (t === "m") {
|
|
197
|
-
if (buf.length === 1) {
|
|
198
|
-
const d = parseInt(buf[0], 10);
|
|
199
|
-
if (d > 5) {
|
|
200
|
-
return { value: `0${d}`, completed: true };
|
|
201
|
-
}
|
|
202
|
-
if (d === 0) {
|
|
203
|
-
return { value: "01", completed: true };
|
|
204
|
-
}
|
|
205
|
-
return { value: buf, completed: false };
|
|
206
|
-
}
|
|
207
|
-
if (buf.length === 2) {
|
|
208
|
-
let m = parseInt(buf, 10);
|
|
209
|
-
if (m < 1)
|
|
210
|
-
m = 1;
|
|
211
|
-
if (m > 60)
|
|
212
|
-
m = 60;
|
|
213
|
-
const out = m.toString().padStart(2, "0");
|
|
214
|
-
return { value: out, completed: true };
|
|
215
|
-
}
|
|
216
|
-
}
|
|
176
|
+
if (tokenChar === "M")
|
|
177
|
+
return normalizeTwoDigitSegment(buf, 1, 1, 12);
|
|
178
|
+
if (t === "d")
|
|
179
|
+
return normalizeTwoDigitSegment(buf, 3, 1, 31);
|
|
180
|
+
if (t === "h")
|
|
181
|
+
return is12Hour ? normalizeTwoDigitSegment(buf, 1, 1, 12) : normalizeTwoDigitSegment(buf, 2, 0, 23);
|
|
182
|
+
if (tokenChar === "m")
|
|
183
|
+
return normalizeTwoDigitSegment(buf, 5, 0, 59);
|
|
217
184
|
return { value: buf, completed: buf.length >= segmentLength };
|
|
218
185
|
};
|
|
219
186
|
function SingleDatePickerInner(props, ref) {
|
|
@@ -226,12 +193,12 @@ function SingleDatePickerInner(props, ref) {
|
|
|
226
193
|
const calendarIconPx = getTextInputIconSizePx(sizeKey);
|
|
227
194
|
const calendarIconWrapperSx = getTextInputIconWrapperSx(sizeKey);
|
|
228
195
|
const { isOpen, onOpen, onClose } = useDisclosure();
|
|
229
|
-
const
|
|
196
|
+
const popoverRef = useRef(null);
|
|
197
|
+
const requestExclusiveOpen = useExclusiveOverlay(isOpen, onClose, popoverRef);
|
|
230
198
|
const pickerPortal = usePickerPortal(isOpen);
|
|
231
199
|
const calendarLabel = label !== null && label !== void 0 ? label : "Choose date";
|
|
232
200
|
const inputRef = useRef(null);
|
|
233
201
|
const mergedInputRef = useMergeRefs(inputRef, ref);
|
|
234
|
-
const popoverRef = useRef(null);
|
|
235
202
|
const triggerRef = useRef(null);
|
|
236
203
|
const resolvedPickerType = pickerType !== null && pickerType !== void 0 ? pickerType : inferPickerTypeFromFormat(dateFormat);
|
|
237
204
|
const resolvedDateFormat = getFormatForPickerType(pickerType, dateFormat);
|
|
@@ -258,6 +225,7 @@ function SingleDatePickerInner(props, ref) {
|
|
|
258
225
|
}
|
|
259
226
|
return buildFormatSegments(activeFormat);
|
|
260
227
|
}, [enableMask, activeFormat]);
|
|
228
|
+
const separatorKeys = useMemo(() => formatSegments.filter((segment) => segment.type === "literal").map((segment) => segment.placeholder), [formatSegments]);
|
|
261
229
|
const tokenSegments = useMemo(() => (enableMask ? formatSegments.filter((segment) => segment.type === "token") : []), [enableMask, formatSegments]);
|
|
262
230
|
const maskPlaceholder = useMemo(() => {
|
|
263
231
|
if (!enableMask) {
|
|
@@ -278,6 +246,7 @@ function SingleDatePickerInner(props, ref) {
|
|
|
278
246
|
const pendingSegmentNavigationRef = useRef(null);
|
|
279
247
|
const restoreSelectionRef = useRef(null);
|
|
280
248
|
const segmentInputBufferRef = useRef("");
|
|
249
|
+
const keystrokeSeqRef = useRef(0);
|
|
281
250
|
const maskInsertedRef = useRef(false);
|
|
282
251
|
const today = new Date();
|
|
283
252
|
const tomorrow = addDays(today, 1);
|
|
@@ -410,6 +379,8 @@ function SingleDatePickerInner(props, ref) {
|
|
|
410
379
|
};
|
|
411
380
|
const handleInputChange = (e) => {
|
|
412
381
|
if (enableMask && isMaskActive) {
|
|
382
|
+
if (e.target.value === "")
|
|
383
|
+
clearAllSegments();
|
|
413
384
|
return;
|
|
414
385
|
}
|
|
415
386
|
let value = e.target.value;
|
|
@@ -526,6 +497,7 @@ function SingleDatePickerInner(props, ref) {
|
|
|
526
497
|
setCurrentSegmentIndex(clamped);
|
|
527
498
|
lastSegmentIndexRef.current = clamped;
|
|
528
499
|
segmentInputBufferRef.current = "";
|
|
500
|
+
restoreSelectionRef.current = { start: segment.start, end: segment.end };
|
|
529
501
|
const inputEl = inputRef.current;
|
|
530
502
|
if (!inputEl)
|
|
531
503
|
return;
|
|
@@ -575,6 +547,29 @@ function SingleDatePickerInner(props, ref) {
|
|
|
575
547
|
const clamped = Math.max(0, Math.min(tokenSegments.length - 1, nextIndex));
|
|
576
548
|
selectSegment(clamped);
|
|
577
549
|
}, [enableMask, selectSegment, tokenSegments.length]);
|
|
550
|
+
const deferSelectSegment = (index) => {
|
|
551
|
+
const seq = keystrokeSeqRef.current;
|
|
552
|
+
requestAnimationFrame(() => {
|
|
553
|
+
if (keystrokeSeqRef.current === seq)
|
|
554
|
+
selectSegment(index);
|
|
555
|
+
});
|
|
556
|
+
};
|
|
557
|
+
const clearAllSegments = () => {
|
|
558
|
+
keystrokeSeqRef.current += 1;
|
|
559
|
+
segmentInputBufferRef.current = "";
|
|
560
|
+
maskInsertedRef.current = true;
|
|
561
|
+
setInputValue(maskPlaceholder);
|
|
562
|
+
setTempDate(null);
|
|
563
|
+
setIsTyping(true);
|
|
564
|
+
const inputEl = inputRef.current;
|
|
565
|
+
if (inputEl && tokenSegments.length > 0) {
|
|
566
|
+
inputEl.value = maskPlaceholder;
|
|
567
|
+
lastSegmentIndexRef.current = 0;
|
|
568
|
+
setCurrentSegmentIndex(0);
|
|
569
|
+
restoreSelectionRef.current = { start: tokenSegments[0].start, end: tokenSegments[0].end };
|
|
570
|
+
inputEl.setSelectionRange(tokenSegments[0].start, tokenSegments[0].end);
|
|
571
|
+
}
|
|
572
|
+
};
|
|
578
573
|
const getTokenIndexForCaret = (caret, tokens) => {
|
|
579
574
|
if (tokens.length === 0)
|
|
580
575
|
return 0;
|
|
@@ -610,6 +605,7 @@ function SingleDatePickerInner(props, ref) {
|
|
|
610
605
|
return isSegmentComplete(segment, segmentValue);
|
|
611
606
|
});
|
|
612
607
|
};
|
|
608
|
+
const isMaskEmpty = (value) => !value || value === maskPlaceholder;
|
|
613
609
|
const computeFinalDate = (parsed) => {
|
|
614
610
|
if (minDate && isBefore(parsed, minDate))
|
|
615
611
|
return null;
|
|
@@ -679,7 +675,7 @@ function SingleDatePickerInner(props, ref) {
|
|
|
679
675
|
maskInsertedRef.current = false;
|
|
680
676
|
}
|
|
681
677
|
segmentInputBufferRef.current = "";
|
|
682
|
-
|
|
678
|
+
deferSelectSegment(0);
|
|
683
679
|
};
|
|
684
680
|
const handleInputClick = (e) => {
|
|
685
681
|
var _a;
|
|
@@ -693,9 +689,11 @@ function SingleDatePickerInner(props, ref) {
|
|
|
693
689
|
if (!inputRef.current)
|
|
694
690
|
return;
|
|
695
691
|
const caret = (_a = inputRef.current.selectionStart) !== null && _a !== void 0 ? _a : 0;
|
|
696
|
-
const index =
|
|
692
|
+
const index = isMaskEmpty(inputRef.current.value)
|
|
693
|
+
? 0
|
|
694
|
+
: tokenSegments.findIndex((segment) => caret >= segment.start && caret <= segment.end);
|
|
697
695
|
if (index >= 0) {
|
|
698
|
-
|
|
696
|
+
deferSelectSegment(index);
|
|
699
697
|
}
|
|
700
698
|
};
|
|
701
699
|
const handleKeyDown = (e) => {
|
|
@@ -788,6 +786,11 @@ function SingleDatePickerInner(props, ref) {
|
|
|
788
786
|
}
|
|
789
787
|
if (key === "Backspace" || key === "Delete") {
|
|
790
788
|
e.preventDefault();
|
|
789
|
+
if (inputEl && selStart === 0 && selEnd > 0 && selEnd >= inputEl.value.length) {
|
|
790
|
+
clearAllSegments();
|
|
791
|
+
return;
|
|
792
|
+
}
|
|
793
|
+
keystrokeSeqRef.current += 1;
|
|
791
794
|
const baseValue = ensureMaskValue();
|
|
792
795
|
const newValue = baseValue.slice(0, segment.start) + segment.placeholder + baseValue.slice(segment.end);
|
|
793
796
|
setInputValue(newValue);
|
|
@@ -795,7 +798,7 @@ function SingleDatePickerInner(props, ref) {
|
|
|
795
798
|
maskInsertedRef.current = false;
|
|
796
799
|
segmentInputBufferRef.current = "";
|
|
797
800
|
updateTempDateFromMask(newValue);
|
|
798
|
-
|
|
801
|
+
deferSelectSegment(activeIdx);
|
|
799
802
|
return;
|
|
800
803
|
}
|
|
801
804
|
if (e.ctrlKey || e.metaKey) {
|
|
@@ -804,6 +807,48 @@ function SingleDatePickerInner(props, ref) {
|
|
|
804
807
|
return;
|
|
805
808
|
}
|
|
806
809
|
}
|
|
810
|
+
const isDigit = /^[0-9]$/.test(key);
|
|
811
|
+
if (isDigit || separatorKeys.includes(key)) {
|
|
812
|
+
e.preventDefault();
|
|
813
|
+
keystrokeSeqRef.current += 1;
|
|
814
|
+
const baseValue = ensureMaskValue();
|
|
815
|
+
const buffered = segmentInputBufferRef.current !== "";
|
|
816
|
+
const typingIdx = buffered ? lastSegmentIndexRef.current : isMaskEmpty(baseValue) ? 0 : activeIdx;
|
|
817
|
+
const typingSegment = tokenSegments[typingIdx];
|
|
818
|
+
if (typingSegment.token === "a" || (!isDigit && !buffered))
|
|
819
|
+
return;
|
|
820
|
+
const segmentLength = typingSegment.placeholder.length;
|
|
821
|
+
const buffer = isDigit ? segmentInputBufferRef.current + key : segmentInputBufferRef.current.padStart(segmentLength, "0");
|
|
822
|
+
const { value: activeValue, completed } = normalizeSegmentBuffer(typingSegment.token, buffer, segmentLength, is12Hour);
|
|
823
|
+
const newSegmentValue = activeValue + typingSegment.placeholder.slice(activeValue.length);
|
|
824
|
+
const newValue = baseValue.slice(0, typingSegment.start) + newSegmentValue + baseValue.slice(typingSegment.end);
|
|
825
|
+
setInputValue(newValue);
|
|
826
|
+
setIsTyping(true);
|
|
827
|
+
maskInsertedRef.current = false;
|
|
828
|
+
lastSegmentIndexRef.current = typingIdx;
|
|
829
|
+
updateTempDateFromMask(newValue);
|
|
830
|
+
const inputEl = inputRef.current;
|
|
831
|
+
if (completed) {
|
|
832
|
+
segmentInputBufferRef.current = "";
|
|
833
|
+
const nextIdx = typingIdx + 1;
|
|
834
|
+
if (nextIdx < tokenSegments.length && inputEl) {
|
|
835
|
+
const nextSegment = tokenSegments[nextIdx];
|
|
836
|
+
pendingSegmentNavigationRef.current = nextIdx;
|
|
837
|
+
restoreSelectionRef.current = { start: nextSegment.start, end: nextSegment.end };
|
|
838
|
+
inputEl.value = newValue;
|
|
839
|
+
inputEl.focus();
|
|
840
|
+
inputEl.setSelectionRange(nextSegment.start, nextSegment.end);
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
else {
|
|
844
|
+
segmentInputBufferRef.current = activeValue;
|
|
845
|
+
if (inputEl) {
|
|
846
|
+
inputEl.value = newValue;
|
|
847
|
+
inputEl.setSelectionRange(typingSegment.start + activeValue.length, typingSegment.end);
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
return;
|
|
851
|
+
}
|
|
807
852
|
if (((_e = segment.token) === null || _e === void 0 ? void 0 : _e.toLowerCase()) === "a") {
|
|
808
853
|
const lower = key.toLowerCase();
|
|
809
854
|
if (lower === "a" || lower === "p") {
|
|
@@ -836,48 +881,6 @@ function SingleDatePickerInner(props, ref) {
|
|
|
836
881
|
e.preventDefault();
|
|
837
882
|
return;
|
|
838
883
|
}
|
|
839
|
-
if (/^[0-9]$/.test(key)) {
|
|
840
|
-
e.preventDefault();
|
|
841
|
-
const baseValue = ensureMaskValue();
|
|
842
|
-
const placeholderChars = segment.placeholder.split("");
|
|
843
|
-
const buffer = segmentInputBufferRef.current + key;
|
|
844
|
-
const normalized = normalizeTimeBuffer(segment.token, buffer, segment.placeholder.length, is12Hour);
|
|
845
|
-
const activeValue = normalized.value;
|
|
846
|
-
const completed = normalized.completed;
|
|
847
|
-
placeholderChars.forEach((_char, idx) => {
|
|
848
|
-
if (idx < activeValue.length) {
|
|
849
|
-
placeholderChars[idx] = activeValue[idx];
|
|
850
|
-
}
|
|
851
|
-
});
|
|
852
|
-
const newSegmentValue = placeholderChars.join("");
|
|
853
|
-
const newValue = baseValue.slice(0, segment.start) + newSegmentValue + baseValue.slice(segment.end);
|
|
854
|
-
setInputValue(newValue);
|
|
855
|
-
setIsTyping(true);
|
|
856
|
-
maskInsertedRef.current = false;
|
|
857
|
-
updateTempDateFromMask(newValue);
|
|
858
|
-
if (completed) {
|
|
859
|
-
segmentInputBufferRef.current = "";
|
|
860
|
-
const nextIdx = activeIdx + 1;
|
|
861
|
-
if (nextIdx < tokenSegments.length && inputRef.current) {
|
|
862
|
-
const nextSegment = tokenSegments[nextIdx];
|
|
863
|
-
pendingSegmentNavigationRef.current = nextIdx;
|
|
864
|
-
restoreSelectionRef.current = { start: nextSegment.start, end: nextSegment.end };
|
|
865
|
-
inputRef.current.value = newValue;
|
|
866
|
-
inputRef.current.focus();
|
|
867
|
-
inputRef.current.setSelectionRange(nextSegment.start, nextSegment.end);
|
|
868
|
-
}
|
|
869
|
-
}
|
|
870
|
-
else {
|
|
871
|
-
segmentInputBufferRef.current = activeValue;
|
|
872
|
-
const caretStart = segment.start + activeValue.length;
|
|
873
|
-
requestAnimationFrame(() => {
|
|
874
|
-
if (inputRef.current) {
|
|
875
|
-
inputRef.current.setSelectionRange(caretStart, segment.end);
|
|
876
|
-
}
|
|
877
|
-
});
|
|
878
|
-
}
|
|
879
|
-
return;
|
|
880
|
-
}
|
|
881
884
|
if (key === "ArrowUp" || key === "ArrowDown") {
|
|
882
885
|
e.preventDefault();
|
|
883
886
|
return;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { forwardRef, useState, useRef, useEffect, useMemo, useCallback } from "react";
|
|
3
3
|
import { Box, Input, InputGroup, InputRightElement, useMergeRefs } from "@chakra-ui/react";
|
|
4
|
-
import { format
|
|
4
|
+
import { format } from "date-fns";
|
|
5
5
|
import { ClockIcon } from "lucide-react";
|
|
6
6
|
import { useCustomTheme } from "../../Theme/useCustomTheme.js";
|
|
7
7
|
import { DEFAULT_CONTROL_SIZE } from "../../Theme/tokens/primitives/controlSizes.js";
|
|
@@ -88,6 +88,18 @@ const caretFromDigitIndex = (digitIndex, digitsLen) => {
|
|
|
88
88
|
return 3;
|
|
89
89
|
return digitIndex + 1;
|
|
90
90
|
};
|
|
91
|
+
const pad2 = (n) => n.toString().padStart(2, "0");
|
|
92
|
+
const digitsFromDate = (date, is12Hour) => {
|
|
93
|
+
const hours = date.getHours();
|
|
94
|
+
const displayHour = is12Hour ? (hours % 12 === 0 ? 12 : hours % 12) : hours;
|
|
95
|
+
return `${pad2(displayHour)}${pad2(date.getMinutes())}`;
|
|
96
|
+
};
|
|
97
|
+
const dateFromDigits = (digits, is12Hour, meridiem) => {
|
|
98
|
+
const hour = parseInt(digits.slice(0, 2), 10);
|
|
99
|
+
const hour24 = is12Hour ? (hour % 12) + (meridiem === "PM" ? 12 : 0) : hour;
|
|
100
|
+
const today = new Date();
|
|
101
|
+
return new Date(today.getFullYear(), today.getMonth(), today.getDate(), hour24, parseInt(digits.slice(2, 4), 10), 0, 0);
|
|
102
|
+
};
|
|
91
103
|
const formatToPlaceholder = (formatStr) => formatStr.replace(/h{1,2}/gi, "HH").replace(/m{1,2}/gi, "mm").replace(/a{1,2}/gi, "AM");
|
|
92
104
|
/** Masked HH:mm digit input, no seconds; `ref` is the `<input>`. */
|
|
93
105
|
export const TimePickerInput = forwardRef(function TimePickerInput({ value, onChange, dateFormat, isDisabled: disabled = false, placeholder, id, name, isRequired, describedBy, onFocus, onBlur, error, size = DEFAULT_CONTROL_SIZE, }, ref) {
|
|
@@ -105,16 +117,15 @@ export const TimePickerInput = forwardRef(function TimePickerInput({ value, onCh
|
|
|
105
117
|
const is12Hour = useMemo(() => /h{1,2}/i.test(dateFormat) && !/H{1,2}/.test(dateFormat), [dateFormat]);
|
|
106
118
|
const placeholderText = useMemo(() => placeholder || formatToPlaceholder(dateFormat), [placeholder, dateFormat]);
|
|
107
119
|
const ampmRef = useRef("AM");
|
|
120
|
+
const typedSinceFocusRef = useRef(false);
|
|
108
121
|
const [ampm, setAmpm] = useState("AM");
|
|
109
122
|
const setInputValueAndCaret = useCallback((formatted, caretDigitIndex) => {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
el.setSelectionRange(caretPos, caretPos);
|
|
117
|
-
});
|
|
123
|
+
const el = inputRef.current;
|
|
124
|
+
if (!el)
|
|
125
|
+
return;
|
|
126
|
+
el.value = formatted;
|
|
127
|
+
const caretPos = Math.min(caretFromDigitIndex(caretDigitIndex, digitsRef.current.length), formatted.length);
|
|
128
|
+
el.setSelectionRange(caretPos, caretPos);
|
|
118
129
|
}, []);
|
|
119
130
|
const applyDigits = useCallback((rawDigits, desiredDigitIndex) => {
|
|
120
131
|
const clamped = adjustDigits(rawDigits, is12Hour);
|
|
@@ -125,16 +136,13 @@ export const TimePickerInput = forwardRef(function TimePickerInput({ value, onCh
|
|
|
125
136
|
const nextDigitIndex = Math.max(0, Math.min(desiredDigitIndex, maxDigitIndex));
|
|
126
137
|
setInputValueAndCaret(formatted, nextDigitIndex);
|
|
127
138
|
if (clamped.length === 4) {
|
|
128
|
-
|
|
129
|
-
const parsed = parse(formattedInput, dateFormat, new Date());
|
|
130
|
-
if (isValid(parsed))
|
|
131
|
-
onChange(parsed);
|
|
139
|
+
onChange(dateFromDigits(clamped, is12Hour, ampmRef.current || "AM"));
|
|
132
140
|
}
|
|
133
141
|
else if (clamped.length === 0) {
|
|
134
142
|
onChange(null);
|
|
135
143
|
}
|
|
136
144
|
return { digits: clamped, nextDigitIndex };
|
|
137
|
-
}, [
|
|
145
|
+
}, [is12Hour, onChange, setInputValueAndCaret]);
|
|
138
146
|
const updateAmpmValue = useCallback((next) => {
|
|
139
147
|
if (!is12Hour)
|
|
140
148
|
return;
|
|
@@ -143,9 +151,7 @@ export const TimePickerInput = forwardRef(function TimePickerInput({ value, onCh
|
|
|
143
151
|
ampmRef.current = upperNext;
|
|
144
152
|
setAmpm(upperNext);
|
|
145
153
|
if (digitsRef.current.length === 4) {
|
|
146
|
-
|
|
147
|
-
if (isValid(parsed))
|
|
148
|
-
onChange(parsed);
|
|
154
|
+
onChange(dateFromDigits(digitsRef.current, is12Hour, upperNext));
|
|
149
155
|
}
|
|
150
156
|
const el = inputRef.current;
|
|
151
157
|
if (!el)
|
|
@@ -156,7 +162,7 @@ export const TimePickerInput = forwardRef(function TimePickerInput({ value, onCh
|
|
|
156
162
|
else {
|
|
157
163
|
el.value = formattedDigits ? `${formattedDigits} ${upperNext}` : "";
|
|
158
164
|
}
|
|
159
|
-
}, [
|
|
165
|
+
}, [is12Hour, onChange]);
|
|
160
166
|
const toggleAmpm = useCallback(() => {
|
|
161
167
|
if (!is12Hour)
|
|
162
168
|
return;
|
|
@@ -256,6 +262,8 @@ export const TimePickerInput = forwardRef(function TimePickerInput({ value, onCh
|
|
|
256
262
|
if (disabled)
|
|
257
263
|
return;
|
|
258
264
|
const key = e.key;
|
|
265
|
+
if (/^[0-9]$/.test(key) || key === "Backspace" || key === "Delete")
|
|
266
|
+
typedSinceFocusRef.current = true;
|
|
259
267
|
if (/^[0-9]$/.test(key)) {
|
|
260
268
|
e.preventDefault();
|
|
261
269
|
handleDigitInput(key);
|
|
@@ -304,9 +312,10 @@ export const TimePickerInput = forwardRef(function TimePickerInput({ value, onCh
|
|
|
304
312
|
setIsFocused(true);
|
|
305
313
|
isFocusedRef.current = true;
|
|
306
314
|
setActiveSegment("hour");
|
|
315
|
+
typedSinceFocusRef.current = false;
|
|
307
316
|
requestAnimationFrame(() => {
|
|
308
317
|
const el = inputRef.current;
|
|
309
|
-
if (!el)
|
|
318
|
+
if (!el || typedSinceFocusRef.current)
|
|
310
319
|
return;
|
|
311
320
|
el.value = formatDigits(digitsRef.current);
|
|
312
321
|
el.focus();
|
|
@@ -365,8 +374,7 @@ export const TimePickerInput = forwardRef(function TimePickerInput({ value, onCh
|
|
|
365
374
|
}, [applyDigits]);
|
|
366
375
|
useEffect(() => {
|
|
367
376
|
if (value) {
|
|
368
|
-
const
|
|
369
|
-
const newDigits = adjustDigits(formattedValue.replace(/\D/g, ""), is12Hour);
|
|
377
|
+
const newDigits = digitsFromDate(value, is12Hour);
|
|
370
378
|
digitsRef.current = newDigits;
|
|
371
379
|
setDigits(newDigits);
|
|
372
380
|
if (is12Hour) {
|
|
@@ -396,7 +404,7 @@ export const TimePickerInput = forwardRef(function TimePickerInput({ value, onCh
|
|
|
396
404
|
inputRef.current.value = "";
|
|
397
405
|
}
|
|
398
406
|
}
|
|
399
|
-
}, [value,
|
|
407
|
+
}, [value, is12Hour]);
|
|
400
408
|
useEffect(() => {
|
|
401
409
|
const formatted = formatDigits(digits);
|
|
402
410
|
if (!isFocusedRef.current && inputRef.current) {
|