优化预约作品选择和日期时间选择

This commit is contained in:
2026-07-02 11:49:57 +08:00
parent 687d355246
commit 475df5fe13
14 changed files with 1039 additions and 94 deletions
@@ -0,0 +1,52 @@
import { describe, expect, it } from 'vitest'
import {
buildDatePickerMonth,
buildHourOptions,
buildMinuteOptions,
cancelPickerDraft,
confirmPickerDraft,
createPickerDraft,
joinTimeParts,
normalizeTimeParts,
updatePickerDraft
} from '@/utils/picker-options'
describe('picker options', () => {
it('builds a sunday-first month grid with selected date', () => {
const days = buildDatePickerMonth('2026-07-02', '2026-07-02')
expect(days).toHaveLength(42)
expect(days.slice(0, 7).map(item => item.date)).toEqual([
'2026-06-28',
'2026-06-29',
'2026-06-30',
'2026-07-01',
'2026-07-02',
'2026-07-03',
'2026-07-04'
])
expect(days.find(item => item.date === '2026-07-02')?.isSelected).toBe(true)
expect(days.find(item => item.date === '2026-06-30')?.isCurrentMonth).toBe(false)
})
it('builds hour and minute options', () => {
expect(buildHourOptions()[0]).toBe('00')
expect(buildHourOptions()[23]).toBe('23')
expect(buildMinuteOptions()[0]).toBe('00')
expect(buildMinuteOptions()[59]).toBe('59')
})
it('normalizes and joins time values', () => {
expect(normalizeTimeParts('7:5')).toEqual({ hour: '07', minute: '05' })
expect(normalizeTimeParts('30:90')).toEqual({ hour: '23', minute: '59' })
expect(joinTimeParts('9', '8')).toBe('09:08')
})
it('keeps committed value unchanged until confirm', () => {
const draft = createPickerDraft('2026-07-02', '2026-07-01')
const changedDraft = updatePickerDraft(draft, '2026-07-08')
expect(cancelPickerDraft(changedDraft)).toBe('2026-07-02')
expect(confirmPickerDraft(changedDraft)).toBe('2026-07-08')
})
})
@@ -0,0 +1,78 @@
import { addDays, formatDate, startOfMonth, toDate } from '@/utils/date'
export interface DatePickerDay {
date: string
day: number
isCurrentMonth: boolean
isSelected: boolean
}
export interface PickerDraft {
committedValue: string
draftValue: string
}
const pad = (value: number): string => String(value).padStart(2, '0')
export const buildDatePickerMonth = (monthValue: string, selectedDate: string): DatePickerDay[] => {
const first = startOfMonth(monthValue)
const gridStart = addDays(first, -first.getDay())
return Array.from({ length: 42 }, (_item, index) => {
const current = addDays(gridStart, index)
const date = formatDate(current)
return {
date,
day: current.getDate(),
isCurrentMonth: current.getMonth() === first.getMonth(),
isSelected: date === selectedDate
}
})
}
export const buildHourOptions = (): string[] => {
return Array.from({ length: 24 }, (_item, index) => pad(index))
}
export const buildMinuteOptions = (): string[] => {
return Array.from({ length: 60 }, (_item, index) => pad(index))
}
export const normalizeTimeParts = (value: string): { hour: string; minute: string } => {
const [rawHour, rawMinute] = String(value || '00:00').split(':')
const hourNumber = Number(rawHour)
const minuteNumber = Number(rawMinute)
const hour = Number.isFinite(hourNumber) ? Math.min(Math.max(hourNumber, 0), 23) : 0
const minute = Number.isFinite(minuteNumber) ? Math.min(Math.max(minuteNumber, 0), 59) : 0
return {
hour: pad(hour),
minute: pad(minute)
}
}
export const joinTimeParts = (hour: string, minute: string): string => {
const normalized = normalizeTimeParts(`${hour}:${minute}`)
return `${normalized.hour}:${normalized.minute}`
}
export const createPickerDraft = (modelValue: string, fallbackValue: string): PickerDraft => {
const committedValue = modelValue || fallbackValue
return {
committedValue,
draftValue: committedValue
}
}
export const updatePickerDraft = (draft: PickerDraft, draftValue: string): PickerDraft => {
return {
committedValue: draft.committedValue,
draftValue
}
}
export const confirmPickerDraft = (draft: PickerDraft): string => draft.draftValue
export const cancelPickerDraft = (draft: PickerDraft): string => draft.committedValue
export const resolveMonthCursor = (value: string): Date => {
return toDate(value || formatDate(new Date()))
}