优化预约作品选择和日期时间选择
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
<template>
|
||||
<button class="field-value picker-trigger" type="button" @click="openSheet">
|
||||
{{ displayValue }}
|
||||
</button>
|
||||
<DatePickerSheet
|
||||
:model-value="modelValue"
|
||||
:open="sheetOpen"
|
||||
:title="title"
|
||||
@update:model-value="updateValue"
|
||||
@update:open="updateOpen"
|
||||
@confirm="confirmValue"
|
||||
/>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, ref } from 'vue'
|
||||
import DatePickerSheet from '@/components/DatePickerSheet.vue'
|
||||
|
||||
const props = defineProps<{
|
||||
modelValue: string
|
||||
placeholder: string
|
||||
title: string
|
||||
}>()
|
||||
|
||||
const emit = defineEmits<{
|
||||
'update:modelValue': [value: string]
|
||||
confirm: [value: string]
|
||||
}>()
|
||||
|
||||
const sheetOpen = ref(false)
|
||||
const displayValue = computed(() => props.modelValue || props.placeholder)
|
||||
|
||||
const openSheet = (): void => {
|
||||
sheetOpen.value = true
|
||||
}
|
||||
|
||||
const updateOpen = (value: boolean): void => {
|
||||
sheetOpen.value = value
|
||||
}
|
||||
|
||||
const updateValue = (value: string): void => {
|
||||
emit('update:modelValue', value)
|
||||
}
|
||||
|
||||
const confirmValue = (value: string): void => {
|
||||
emit('confirm', value)
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.picker-trigger {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
min-height: 40px;
|
||||
padding: 0;
|
||||
text-align: left;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,175 @@
|
||||
<template>
|
||||
<BaseModal :model-value="open" :title="title" @update:model-value="handleOpenChange">
|
||||
<div class="date-picker-sheet">
|
||||
<div class="date-picker-sheet__month">
|
||||
<button type="button" aria-label="上个月" @click="moveMonth(-1)">
|
||||
<ChevronLeft :size="18" aria-hidden="true" />
|
||||
</button>
|
||||
<strong>{{ monthTitle }}</strong>
|
||||
<button type="button" aria-label="下个月" @click="moveMonth(1)">
|
||||
<ChevronRight :size="18" aria-hidden="true" />
|
||||
</button>
|
||||
</div>
|
||||
<div class="date-picker-sheet__week">
|
||||
<span v-for="weekday in weekdays" :key="weekday">{{ weekday }}</span>
|
||||
</div>
|
||||
<div class="date-picker-sheet__grid">
|
||||
<button
|
||||
v-for="day in monthDays"
|
||||
:key="day.date"
|
||||
:class="{
|
||||
'is-muted': !day.isCurrentMonth,
|
||||
'is-selected': day.isSelected
|
||||
}"
|
||||
type="button"
|
||||
@click="selectDate(day.date)"
|
||||
>
|
||||
{{ day.day }}
|
||||
</button>
|
||||
</div>
|
||||
<div class="date-picker-sheet__value">{{ draft.draftValue }}</div>
|
||||
<button class="primary-btn" type="button" @click="confirmSelection">确定</button>
|
||||
</div>
|
||||
</BaseModal>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, ref, watch } from 'vue'
|
||||
import { ChevronLeft, ChevronRight } from 'lucide-vue-next'
|
||||
import BaseModal from '@/components/BaseModal.vue'
|
||||
import { addMonths, formatDate, formatMonth } from '@/utils/date'
|
||||
import {
|
||||
buildDatePickerMonth,
|
||||
confirmPickerDraft,
|
||||
createPickerDraft,
|
||||
resolveMonthCursor,
|
||||
updatePickerDraft,
|
||||
type PickerDraft
|
||||
} from '@/utils/picker-options'
|
||||
|
||||
const props = defineProps<{
|
||||
modelValue: string
|
||||
open: boolean
|
||||
title: string
|
||||
}>()
|
||||
|
||||
const emit = defineEmits<{
|
||||
'update:modelValue': [value: string]
|
||||
'update:open': [value: boolean]
|
||||
confirm: [value: string]
|
||||
}>()
|
||||
|
||||
const today = formatDate(new Date())
|
||||
const weekdays = ['日', '一', '二', '三', '四', '五', '六']
|
||||
const draft = ref<PickerDraft>(createPickerDraft(props.modelValue, today))
|
||||
const monthCursor = ref(resolveMonthCursor(draft.value.draftValue))
|
||||
|
||||
const monthTitle = computed(() => formatMonth(monthCursor.value))
|
||||
const monthDays = computed(() => buildDatePickerMonth(formatDate(monthCursor.value), draft.value.draftValue))
|
||||
|
||||
const syncDraftFromModel = (): void => {
|
||||
draft.value = createPickerDraft(props.modelValue, today)
|
||||
monthCursor.value = resolveMonthCursor(draft.value.draftValue)
|
||||
}
|
||||
|
||||
const handleOpenChange = (value: boolean): void => {
|
||||
emit('update:open', value)
|
||||
}
|
||||
|
||||
const moveMonth = (months: number): void => {
|
||||
monthCursor.value = addMonths(monthCursor.value, months)
|
||||
}
|
||||
|
||||
const selectDate = (value: string): void => {
|
||||
draft.value = updatePickerDraft(draft.value, value)
|
||||
monthCursor.value = resolveMonthCursor(value)
|
||||
}
|
||||
|
||||
const confirmSelection = (): void => {
|
||||
const value = confirmPickerDraft(draft.value)
|
||||
emit('update:modelValue', value)
|
||||
emit('confirm', value)
|
||||
emit('update:open', false)
|
||||
}
|
||||
|
||||
watch(() => props.open, value => {
|
||||
if (value) {
|
||||
syncDraftFromModel()
|
||||
}
|
||||
})
|
||||
|
||||
watch(() => props.modelValue, () => {
|
||||
if (!props.open) {
|
||||
syncDraftFromModel()
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.date-picker-sheet {
|
||||
display: grid;
|
||||
gap: 12px;
|
||||
padding: 14px;
|
||||
}
|
||||
|
||||
.date-picker-sheet__month {
|
||||
display: grid;
|
||||
grid-template-columns: 44px 1fr 44px;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.date-picker-sheet__month strong {
|
||||
text-align: center;
|
||||
font-size: 15px;
|
||||
}
|
||||
|
||||
.date-picker-sheet__month button {
|
||||
display: grid;
|
||||
place-items: center;
|
||||
width: 44px;
|
||||
height: 36px;
|
||||
border-radius: 10px;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.date-picker-sheet__week,
|
||||
.date-picker-sheet__grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(7, minmax(0, 1fr));
|
||||
}
|
||||
|
||||
.date-picker-sheet__week {
|
||||
color: var(--muted);
|
||||
font-size: 12px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.date-picker-sheet__grid {
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.date-picker-sheet__grid button {
|
||||
aspect-ratio: 1;
|
||||
min-width: 0;
|
||||
border-radius: 10px;
|
||||
color: var(--ink);
|
||||
font-weight: 760;
|
||||
}
|
||||
|
||||
.date-picker-sheet__grid button.is-muted {
|
||||
color: #bbb4aa;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.date-picker-sheet__grid button.is-selected {
|
||||
background: var(--accent);
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.date-picker-sheet__value {
|
||||
color: var(--muted);
|
||||
font-size: 12px;
|
||||
text-align: center;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,58 @@
|
||||
<template>
|
||||
<button class="field-value picker-trigger" type="button" @click="openSheet">
|
||||
{{ displayValue }}
|
||||
</button>
|
||||
<TimePickerSheet
|
||||
:model-value="modelValue"
|
||||
:open="sheetOpen"
|
||||
:title="title"
|
||||
@update:model-value="updateValue"
|
||||
@update:open="updateOpen"
|
||||
@confirm="confirmValue"
|
||||
/>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, ref } from 'vue'
|
||||
import TimePickerSheet from '@/components/TimePickerSheet.vue'
|
||||
|
||||
const props = defineProps<{
|
||||
modelValue: string
|
||||
placeholder: string
|
||||
title: string
|
||||
}>()
|
||||
|
||||
const emit = defineEmits<{
|
||||
'update:modelValue': [value: string]
|
||||
confirm: [value: string]
|
||||
}>()
|
||||
|
||||
const sheetOpen = ref(false)
|
||||
const displayValue = computed(() => props.modelValue || props.placeholder)
|
||||
|
||||
const openSheet = (): void => {
|
||||
sheetOpen.value = true
|
||||
}
|
||||
|
||||
const updateOpen = (value: boolean): void => {
|
||||
sheetOpen.value = value
|
||||
}
|
||||
|
||||
const updateValue = (value: string): void => {
|
||||
emit('update:modelValue', value)
|
||||
}
|
||||
|
||||
const confirmValue = (value: string): void => {
|
||||
emit('confirm', value)
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.picker-trigger {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
min-height: 40px;
|
||||
padding: 0;
|
||||
text-align: left;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,186 @@
|
||||
<template>
|
||||
<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="小时">
|
||||
<button
|
||||
v-for="hour in hourOptions"
|
||||
:key="hour"
|
||||
:class="{ 'is-selected': hour === draftHour }"
|
||||
type="button"
|
||||
@click="selectHour(hour)"
|
||||
>
|
||||
{{ hour }}
|
||||
</button>
|
||||
</div>
|
||||
<div ref="minuteColumn" class="time-picker-sheet__column" aria-label="分钟">
|
||||
<button
|
||||
v-for="minute in minuteOptions"
|
||||
:key="minute"
|
||||
:class="{ 'is-selected': minute === draftMinute }"
|
||||
type="button"
|
||||
@click="selectMinute(minute)"
|
||||
>
|
||||
{{ minute }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="time-picker-sheet__value">{{ draftTime }}</div>
|
||||
<button class="primary-btn" type="button" @click="confirmSelection">确认</button>
|
||||
</div>
|
||||
</BaseModal>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, nextTick, ref, watch } from 'vue'
|
||||
import BaseModal from '@/components/BaseModal.vue'
|
||||
import {
|
||||
buildHourOptions,
|
||||
buildMinuteOptions,
|
||||
confirmPickerDraft,
|
||||
createPickerDraft,
|
||||
joinTimeParts,
|
||||
normalizeTimeParts,
|
||||
updatePickerDraft,
|
||||
type PickerDraft
|
||||
} from '@/utils/picker-options'
|
||||
|
||||
const props = defineProps<{
|
||||
modelValue: string
|
||||
open: boolean
|
||||
title: string
|
||||
}>()
|
||||
|
||||
const emit = defineEmits<{
|
||||
'update:modelValue': [value: string]
|
||||
'update:open': [value: boolean]
|
||||
confirm: [value: string]
|
||||
}>()
|
||||
|
||||
const hourOptions = buildHourOptions()
|
||||
const minuteOptions = buildMinuteOptions()
|
||||
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)
|
||||
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 scrollSelectedIntoView = async (): Promise<void> => {
|
||||
await nextTick()
|
||||
scrollColumnToSelected(hourColumn.value)
|
||||
scrollColumnToSelected(minuteColumn.value)
|
||||
}
|
||||
|
||||
const syncDraftFromModel = (): void => {
|
||||
draft.value = createPickerDraft(props.modelValue, '00:00')
|
||||
const parts = normalizeTimeParts(draft.value.draftValue)
|
||||
draftHour.value = parts.hour
|
||||
draftMinute.value = parts.minute
|
||||
}
|
||||
|
||||
const syncDraftTime = (): void => {
|
||||
draft.value = updatePickerDraft(draft.value, draftTime.value)
|
||||
}
|
||||
|
||||
const handleOpenChange = (value: boolean): void => {
|
||||
emit('update:open', value)
|
||||
}
|
||||
|
||||
const selectHour = (value: string): void => {
|
||||
draftHour.value = value
|
||||
syncDraftTime()
|
||||
}
|
||||
|
||||
const selectMinute = (value: string): void => {
|
||||
draftMinute.value = value
|
||||
syncDraftTime()
|
||||
}
|
||||
|
||||
const confirmSelection = (): void => {
|
||||
syncDraftTime()
|
||||
const value = confirmPickerDraft(draft.value)
|
||||
emit('update:modelValue', value)
|
||||
emit('confirm', value)
|
||||
emit('update:open', false)
|
||||
}
|
||||
|
||||
watch(() => props.open, value => {
|
||||
if (value) {
|
||||
syncDraftFromModel()
|
||||
void scrollSelectedIntoView()
|
||||
}
|
||||
})
|
||||
|
||||
watch(() => props.modelValue, () => {
|
||||
if (!props.open) {
|
||||
syncDraftFromModel()
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.time-picker-sheet {
|
||||
display: grid;
|
||||
gap: 12px;
|
||||
padding: 14px;
|
||||
}
|
||||
|
||||
.time-picker-sheet__wheels {
|
||||
position: relative;
|
||||
display: grid;
|
||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||
gap: 12px;
|
||||
min-height: 236px;
|
||||
}
|
||||
|
||||
.time-picker-sheet__wheels::before {
|
||||
position: absolute;
|
||||
top: 96px;
|
||||
right: 0;
|
||||
left: 0;
|
||||
height: 44px;
|
||||
border-top: 1px solid var(--border);
|
||||
border-bottom: 1px solid var(--border);
|
||||
content: "";
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.time-picker-sheet__column {
|
||||
display: grid;
|
||||
grid-auto-rows: 44px;
|
||||
max-height: 236px;
|
||||
overflow-y: auto;
|
||||
scroll-snap-type: y proximity;
|
||||
scrollbar-width: none;
|
||||
}
|
||||
|
||||
.time-picker-sheet__column::-webkit-scrollbar {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.time-picker-sheet__column button {
|
||||
color: #aaa39b;
|
||||
font-size: 16px;
|
||||
scroll-snap-align: center;
|
||||
}
|
||||
|
||||
.time-picker-sheet__column button.is-selected {
|
||||
color: var(--ink);
|
||||
font-size: 18px;
|
||||
font-weight: 860;
|
||||
}
|
||||
|
||||
.time-picker-sheet__value {
|
||||
color: var(--muted);
|
||||
font-size: 12px;
|
||||
text-align: center;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user