修正时间滚轮滑动选中
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user