Fix:修复日期时间编辑器箭头按钮不立即更新 (#2310)
Co-authored-by: staff <staff@qimaos-MacBook-Pro.local>
This commit is contained in:
1 parent
74165cc045
commit
4780966d21
3 files changed
+82
-10
No files matched your search
@@ -43,6 +43,29 @@ export function parseTemporalInputValue(value: string, kind: TemporalCellEditorK
|
||||
return date && normalizedTime ? `${date} ${normalizedTime}` : text.replace("T", " ");
|
||||
}
|
||||
|
||||
export type TemporalCellEditorPart = "year" | "month" | "day" | "hour" | "minute" | "second";
|
||||
|
||||
export function stepTemporalInputValue(value: string, kind: TemporalCellEditorKind, part: TemporalCellEditorPart, delta: number): string {
|
||||
const dateParts = temporalDateParts(value);
|
||||
const timeParts = temporalTimeParts(value, kind);
|
||||
|
||||
if (part === "year") dateParts.year = Math.max(1, Math.min(9999, dateParts.year + delta));
|
||||
else if (part === "month") dateParts.month = Math.max(1, Math.min(12, dateParts.month + delta));
|
||||
else if (part === "day") dateParts.day = Math.max(1, Math.min(31, dateParts.day + delta));
|
||||
else {
|
||||
const max = part === "hour" ? 23 : 59;
|
||||
timeParts[part] = (timeParts[part] + delta + max + 1) % (max + 1);
|
||||
}
|
||||
|
||||
dateParts.day = Math.max(1, Math.min(daysInMonth(dateParts.year, dateParts.month), dateParts.day));
|
||||
const dateText = [String(dateParts.year).padStart(4, "0"), String(dateParts.month).padStart(2, "0"), String(dateParts.day).padStart(2, "0")].join("-");
|
||||
const timeText = [timeParts.hour, timeParts.minute, timeParts.second].map((item) => String(item).padStart(2, "0")).join(":");
|
||||
|
||||
if (kind === "date") return dateText;
|
||||
if (kind === "time") return timeText;
|
||||
return `${dateText} ${timeText}`;
|
||||
}
|
||||
|
||||
function normalizeTemporalType(dataType: string | undefined): string {
|
||||
return (dataType ?? "").toLowerCase().replace(/_/g, "").replace(/[(),]/g, " ").replace(/\s+/g, " ").trim();
|
||||
}
|
||||
@@ -52,3 +75,23 @@ function normalizeTimeInput(text: string): string {
|
||||
if (!match) return "";
|
||||
return `${match[1]}:${match[2] ?? "00"}`;
|
||||
}
|
||||
|
||||
function temporalDateParts(value: string): { year: number; month: number; day: number } {
|
||||
const text = formatTemporalInputValue(value, "date");
|
||||
const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(text);
|
||||
if (!match) {
|
||||
const now = new Date();
|
||||
return { year: now.getFullYear(), month: now.getMonth() + 1, day: now.getDate() };
|
||||
}
|
||||
return { year: Number(match[1]), month: Number(match[2]), day: Number(match[3]) };
|
||||
}
|
||||
|
||||
function temporalTimeParts(value: string, kind: TemporalCellEditorKind): { hour: number; minute: number; second: number } {
|
||||
const time = kind === "time" ? formatTemporalInputValue(value, "time") || "00:00:00" : formatTemporalInputValue(value, "datetime").split("T")[1] || "00:00:00";
|
||||
const [hour = "00", minute = "00", second = "00"] = time.split(":");
|
||||
return { hour: Number(hour) || 0, minute: Number(minute) || 0, second: Number(second) || 0 };
|
||||
}
|
||||
|
||||
function daysInMonth(year: number, month: number): number {
|
||||
return new Date(year, month, 0).getDate();
|
||||
}
|
||||
Reference in new issue
Block a user