feat: implement v-model support for SingleDatePicker and enhance layout styling

This commit is contained in:
Astrian Zheng 2025-02-24 13:41:16 +11:00
parent b6dc7c3223
commit f5f0889548
Signed by: Astrian
SSH Key Fingerprint: SHA256:rVnhx3DAKjujCwWE13aDl7uV6+9U1MvydLkNRXJrBiA
3 changed files with 23 additions and 3 deletions

View File

@ -1,10 +1,14 @@
<script setup lang="ts"> <script setup lang="ts">
import {SingleDatePicker} from '../src' import {SingleDatePicker} from '../src'
import {ref} from 'vue'
const date = ref(new Date())
</script> </script>
<template> <template>
<div>{{date.toDateString()}}</div>
<div class="container"> <div class="container">
<SingleDatePicker /> <SingleDatePicker v-model="date" />
</div> </div>
</template> </template>

View File

@ -5,6 +5,8 @@ body {
width: 100vw; width: 100vw;
height: 100vh; height: 100vh;
display: flex; display: flex;
flex-direction: column;
gap: 0.5rem;
justify-content: center; justify-content: center;
align-items: center; align-items: center;
} }

View File

@ -28,9 +28,14 @@
localization: { localization: {
type: String, type: String,
required: false, required: false,
},
modelValue: {
type: Date,
required: false,
} }
}) })
const emit = defineEmits(['update:modelValue'])
const selectMonth = ref(false) const selectMonth = ref(false)
const uniqueId = generateUniqueId() const uniqueId = generateUniqueId()
const currentMonth = ref(new Date().getMonth()) const currentMonth = ref(new Date().getMonth())
@ -51,6 +56,14 @@
onMounted(() => { onMounted(() => {
applyColor(uniqueId, colorScheme.value) applyColor(uniqueId, colorScheme.value)
l10nDays.value = getL10Weekday(localization?.value || navigator.languages[0]) l10nDays.value = getL10Weekday(localization?.value || navigator.languages[0])
if (props.modelValue) {
currentMonth.value = props.modelValue.getMonth()
currentYear.value = props.modelValue.getFullYear()
} else {
currentMonth.value = new Date().getMonth()
currentYear.value = new Date().getFullYear()
}
dates.value = getCalendarDates(currentMonth.value, currentYear.value) dates.value = getCalendarDates(currentMonth.value, currentYear.value)
}) })
@ -79,6 +92,7 @@
function selectDate(date: Date) { function selectDate(date: Date) {
console.log(date) console.log(date)
emit('update:modelValue', date)
} }
</script> </script>
@ -97,10 +111,10 @@
<div class='__datenel_body'> <div class='__datenel_body'>
<div class='__datenel_calendar-view-body __datenel_grid' aria-live="polite"> <div class='__datenel_calendar-view-body __datenel_grid' aria-live="polite">
<div class='__datenel_item __datenel_day-indicator' v-for="(day, index) in l10nDays" :key="index">{{ day }}</div> <div class='__datenel_item __datenel_day-indicator' v-for="(day, index) in l10nDays" :key="index">{{ day }}</div>
<!-- ${selectedDate.toDateString() === date.toDateString() && '__datenel_active'} --> <!-- -->
<button <button
v-for="date in dates" v-for="date in dates"
:class="`__datenel_item __datenel_date ${notAvailable(date) && '__datenel_not-available'} `" :class="`__datenel_item __datenel_date ${notAvailable(date) && '__datenel_not-available'} ${modelValue?.toDateString() === date.toDateString() && '__datenel_active'}`"
:key="date.toISOString()" :key="date.toISOString()"
@click="selectDate(date)" @click="selectDate(date)"
:aria-label="`${date.toLocaleString(localization, { dateStyle: 'full' })}${date.toDateString() === new Date().toDateString() ? ', this is today' : ''}, click to select this date`" :aria-label="`${date.toLocaleString(localization, { dateStyle: 'full' })}${date.toDateString() === new Date().toDateString() ? ', this is today' : ''}, click to select this date`"