修正时间滚轮滑动选中

This commit is contained in:
2026-07-02 12:14:14 +08:00
parent 475df5fe13
commit fed6a99aa1
3 changed files with 59 additions and 9 deletions
@@ -2,7 +2,7 @@
<BaseModal :model-value="open" :title="title" @update:model-value="handleOpenChange">
<div class="time-picker-sheet">
<div class="time-picker-sheet__wheels">
<div ref="hourColumn" class="time-picker-sheet__column" aria-label="小时">
<div ref="hourColumn" class="time-picker-sheet__column" aria-label="小时" @scroll.passive="handleHourScroll">
<button
v-for="hour in hourOptions"
:key="hour"
@@ -13,7 +13,7 @@
{{ hour }}
</button>
</div>
<div ref="minuteColumn" class="time-picker-sheet__column" aria-label="分钟">
<div ref="minuteColumn" class="time-picker-sheet__column" aria-label="分钟" @scroll.passive="handleMinuteScroll">
<button
v-for="minute in minuteOptions"
:key="minute"
@@ -41,6 +41,7 @@ import {
createPickerDraft,
joinTimeParts,
normalizeTimeParts,
resolveWheelOption,
updatePickerDraft,
type PickerDraft
} from '@/utils/picker-options'
@@ -59,6 +60,7 @@ const emit = defineEmits<{
const hourOptions = buildHourOptions()
const minuteOptions = buildMinuteOptions()
const wheelItemHeight = 44
const draft = ref<PickerDraft>(createPickerDraft(props.modelValue, '00:00'))
const draftHour = ref(normalizeTimeParts(draft.value.draftValue).hour)
const draftMinute = ref(normalizeTimeParts(draft.value.draftValue).minute)
@@ -66,17 +68,20 @@ const hourColumn = ref<HTMLElement | null>(null)
const minuteColumn = ref<HTMLElement | null>(null)
const draftTime = computed(() => joinTimeParts(draftHour.value, draftMinute.value))
const scrollColumnToSelected = (column: HTMLElement | null): void => {
const selected = column?.querySelector('.is-selected')
if (selected instanceof HTMLElement) {
selected.scrollIntoView({ block: 'center' })
const scrollColumnToValue = (column: HTMLElement | null, options: string[], value: string): void => {
if (!column) {
return
}
const index = options.indexOf(value)
if (index >= 0) {
column.scrollTop = index * wheelItemHeight
}
}
const scrollSelectedIntoView = async (): Promise<void> => {
await nextTick()
scrollColumnToSelected(hourColumn.value)
scrollColumnToSelected(minuteColumn.value)
scrollColumnToValue(hourColumn.value, hourOptions, draftHour.value)
scrollColumnToValue(minuteColumn.value, minuteOptions, draftMinute.value)
}
const syncDraftFromModel = (): void => {
@@ -90,6 +95,17 @@ const syncDraftTime = (): void => {
draft.value = updatePickerDraft(draft.value, draftTime.value)
}
const syncWheelValue = (target: EventTarget | null, options: string[], applyValue: (value: string) => void): void => {
if (!(target instanceof HTMLElement)) {
return
}
const value = resolveWheelOption(options, target.scrollTop, wheelItemHeight)
if (value) {
applyValue(value)
syncDraftTime()
}
}
const handleOpenChange = (value: boolean): void => {
emit('update:open', value)
}
@@ -97,11 +113,25 @@ const handleOpenChange = (value: boolean): void => {
const selectHour = (value: string): void => {
draftHour.value = value
syncDraftTime()
scrollColumnToValue(hourColumn.value, hourOptions, value)
}
const selectMinute = (value: string): void => {
draftMinute.value = value
syncDraftTime()
scrollColumnToValue(minuteColumn.value, minuteOptions, value)
}
const handleHourScroll = (event: Event): void => {
syncWheelValue(event.currentTarget, hourOptions, value => {
draftHour.value = value
})
}
const handleMinuteScroll = (event: Event): void => {
syncWheelValue(event.currentTarget, minuteOptions, value => {
draftMinute.value = value
})
}
const confirmSelection = (): void => {
@@ -156,9 +186,11 @@ watch(() => props.modelValue, () => {
.time-picker-sheet__column {
display: grid;
grid-auto-rows: 44px;
box-sizing: border-box;
max-height: 236px;
padding: 96px 0;
overflow-y: auto;
scroll-snap-type: y proximity;
scroll-snap-type: y mandatory;
scrollbar-width: none;
}
@@ -8,6 +8,7 @@ import {
createPickerDraft,
joinTimeParts,
normalizeTimeParts,
resolveWheelOption,
updatePickerDraft
} from '@/utils/picker-options'
@@ -49,4 +50,12 @@ describe('picker options', () => {
expect(cancelPickerDraft(changedDraft)).toBe('2026-07-02')
expect(confirmPickerDraft(changedDraft)).toBe('2026-07-08')
})
it('resolves wheel selection from scroll position', () => {
const options = ['00', '01', '02', '03']
expect(resolveWheelOption(options, 0, 44)).toBe('00')
expect(resolveWheelOption(options, 46, 44)).toBe('01')
expect(resolveWheelOption(options, 1000, 44)).toBe('03')
})
})
@@ -37,6 +37,15 @@ export const buildMinuteOptions = (): string[] => {
return Array.from({ length: 60 }, (_item, index) => pad(index))
}
export const resolveWheelOption = (options: string[], scrollTop: number, itemHeight: number): string => {
if (options.length === 0) {
return ''
}
const rawIndex = Math.round(scrollTop / itemHeight)
const index = Math.min(Math.max(rawIndex, 0), options.length - 1)
return options[index] || ''
}
export const normalizeTimeParts = (value: string): { hour: string; minute: string } => {
const [rawHour, rawMinute] = String(value || '00:00').split(':')
const hourNumber = Number(rawHour)