2022-09-07 05:58:59 +00:00
|
|
|
<template>
|
2022-10-07 02:54:09 +00:00
|
|
|
<div ref="label" class="dropdown dropdown-end dropdown-top w-full">
|
2022-09-09 22:46:53 +00:00
|
|
|
<FormTextField v-model="dateText" tabindex="0" label="Date" :inline="inline" readonly />
|
2022-10-12 22:07:11 +00:00
|
|
|
<div tabindex="0" class="card compact dropdown-content shadow bg-base-100 rounded-box w-64" @blur="resetTime">
|
2022-09-07 05:58:59 +00:00
|
|
|
<div class="card-body">
|
|
|
|
<div class="grid grid-cols-7 gap-2">
|
2022-09-09 22:46:53 +00:00
|
|
|
<div v-for="d in daysIdx" :key="d">
|
2022-09-07 05:58:59 +00:00
|
|
|
<p class="text-center">
|
|
|
|
{{ d }}
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<template v-for="day in days">
|
|
|
|
<button
|
|
|
|
v-if="day.number != ''"
|
2022-09-09 22:46:53 +00:00
|
|
|
:key="day.number"
|
2022-09-07 05:58:59 +00:00
|
|
|
class="text-center btn-xs btn btn-outline"
|
|
|
|
@click="select($event, day.date)"
|
|
|
|
>
|
|
|
|
{{ day.number }}
|
|
|
|
</button>
|
2022-09-09 22:46:53 +00:00
|
|
|
<div v-else :key="`${day.number}-empty`"></div>
|
2022-09-07 05:58:59 +00:00
|
|
|
</template>
|
|
|
|
</div>
|
2022-10-12 22:07:11 +00:00
|
|
|
<div class="flex justify-between mt-1 items-center">
|
|
|
|
<button class="btn btn-xs" @click="prevMonth">
|
|
|
|
<Icon class="h-5 w-5" name="mdi-arrow-left"></Icon>
|
|
|
|
</button>
|
|
|
|
<p class="text-center">{{ month }} {{ year }}</p>
|
|
|
|
<button class="btn btn-xs" @click="nextMonth">
|
|
|
|
<Icon class="h-5 w-5" name="mdi-arrow-right"></Icon>
|
|
|
|
</button>
|
|
|
|
</div>
|
2022-09-07 05:58:59 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
2022-09-09 22:46:53 +00:00
|
|
|
const emit = defineEmits(["update:modelValue", "update:text"]);
|
2022-09-07 05:58:59 +00:00
|
|
|
|
|
|
|
const props = defineProps({
|
|
|
|
modelValue: {
|
|
|
|
type: Date,
|
|
|
|
required: false,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
inline: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-09-09 22:46:53 +00:00
|
|
|
const selected = useVModel(props, "modelValue", emit);
|
2022-09-07 05:58:59 +00:00
|
|
|
const dateText = computed(() => {
|
2022-09-12 22:47:27 +00:00
|
|
|
if (!validDate(selected.value)) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-09-07 05:58:59 +00:00
|
|
|
if (selected.value) {
|
|
|
|
return selected.value.toLocaleDateString();
|
|
|
|
}
|
2022-09-12 22:47:27 +00:00
|
|
|
|
2022-09-09 22:46:53 +00:00
|
|
|
return "";
|
2022-09-07 05:58:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const time = ref(new Date());
|
|
|
|
function resetTime() {
|
|
|
|
time.value = new Date();
|
|
|
|
}
|
|
|
|
|
|
|
|
const label = ref<HTMLElement>();
|
|
|
|
onClickOutside(label, () => {
|
|
|
|
resetTime();
|
|
|
|
});
|
|
|
|
|
|
|
|
const month = computed(() => {
|
2022-09-09 22:46:53 +00:00
|
|
|
return time.value.toLocaleString("default", { month: "long" });
|
2022-09-07 05:58:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const year = computed(() => {
|
|
|
|
return time.value.getFullYear();
|
|
|
|
});
|
|
|
|
|
|
|
|
function nextMonth() {
|
|
|
|
time.value.setMonth(time.value.getMonth() + 1);
|
|
|
|
time.value = new Date(time.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
function prevMonth() {
|
|
|
|
time.value.setMonth(time.value.getMonth() - 1);
|
|
|
|
time.value = new Date(time.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
const daysIdx = computed(() => {
|
2022-09-09 22:46:53 +00:00
|
|
|
return ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
|
2022-09-07 05:58:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function select(e: MouseEvent, day: Date) {
|
|
|
|
selected.value = day;
|
2022-09-09 22:46:53 +00:00
|
|
|
// @ts-ignore - this is a vue3 bug
|
2022-09-07 05:58:59 +00:00
|
|
|
e.target.blur();
|
|
|
|
resetTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
type DayEntry = {
|
|
|
|
number: number | string;
|
|
|
|
date: Date;
|
|
|
|
};
|
|
|
|
|
|
|
|
function daysInMonth(month: number, year: number) {
|
|
|
|
return new Date(year, month, 0).getDate();
|
|
|
|
}
|
|
|
|
|
|
|
|
const days = computed<DayEntry[]>(() => {
|
|
|
|
const days = [];
|
|
|
|
|
|
|
|
const totalDays = daysInMonth(time.value.getMonth() + 1, time.value.getFullYear());
|
|
|
|
|
|
|
|
const firstDay = new Date(time.value.getFullYear(), time.value.getMonth(), 1).getDay();
|
|
|
|
|
|
|
|
for (let i = 0; i < firstDay; i++) {
|
|
|
|
days.push({
|
2022-09-09 22:46:53 +00:00
|
|
|
number: "",
|
2022-09-07 05:58:59 +00:00
|
|
|
date: new Date(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 1; i <= totalDays; i++) {
|
|
|
|
days.push({
|
|
|
|
number: i,
|
|
|
|
date: new Date(time.value.getFullYear(), time.value.getMonth(), i),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return days;
|
|
|
|
});
|
|
|
|
</script>
|