请使用vue3设计一个日期选择框
时间: 2023-06-11 16:06:30 浏览: 135
日期选择框
好的,以下是一个简单的日期选择框的 Vue 3 组件示例:
```vue
<template>
<div class="date-picker">
<input type="text" v-model="selectedDate" @focus="showDatePicker" />
<div v-if="show" class="date-picker-container">
<div class="calendar-header">
<div class="calendar-prev" @click="prevMonth"><</div>
<div class="calendar-title">{{ currentMonth }}</div>
<div class="calendar-next" @click="nextMonth">></div>
</div>
<div class="calendar-body">
<div class="calendar-row">
<div v-for="day in daysOfWeek" class="calendar-cell day-header">
{{ day }}
</div>
</div>
<div v-for="week in weeks" class="calendar-row">
<div v-for="day in week" :class="getDayClasses(day)" @click="selectDate(day)">
{{ day ? day.getDate() : '' }}
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import { ref, computed } from 'vue';
export default {
name: 'DatePicker',
setup() {
const selectedDate = ref('');
const show = ref(false);
const currentDate = new Date();
let currentMonth = new Date(currentDate.getFullYear(), currentDate.getMonth(), 1);
const daysOfWeek = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
const weeks = computed(() => {
const month = currentMonth.getMonth();
const year = currentMonth.getFullYear();
const firstDayOfMonth = new Date(year, month, 1);
const lastDayOfMonth = new Date(year, month + 1, 0);
const daysInMonth = lastDayOfMonth.getDate();
const firstDayOfWeek = firstDayOfMonth.getDay();
const weeks = [[]];
let currentWeek = 0;
for (let i = 0; i < firstDayOfWeek; i++) {
weeks[currentWeek].push(null);
}
for (let i = 1; i <= daysInMonth; i++) {
if (weeks[currentWeek].length === 7) {
currentWeek++;
weeks[currentWeek] = [];
}
weeks[currentWeek].push(new Date(year, month, i));
}
for (let i = weeks[currentWeek].length; i < 7; i++) {
weeks[currentWeek].push(null);
}
return weeks;
});
const getDayClasses = (day) => {
const classes = ['calendar-cell'];
if (!day) return classes;
if (day.getMonth() !== currentMonth.getMonth()) {
classes.push('disabled');
}
if (day.toDateString() === selectedDate.value.toDateString()) {
classes.push('selected');
}
return classes;
};
const selectDate = (day) => {
if (!day || day.getMonth() !== currentMonth.getMonth()) return;
selectedDate.value = day;
show.value = false;
};
const showDatePicker = () => {
show.value = true;
};
const prevMonth = () => {
currentMonth = new Date(currentMonth.getFullYear(), currentMonth.getMonth() - 1, 1);
};
const nextMonth = () => {
currentMonth = new Date(currentMonth.getFullYear(), currentMonth.getMonth() + 1, 1);
};
return {
selectedDate,
show,
currentDate,
currentMonth,
daysOfWeek,
weeks,
getDayClasses,
selectDate,
showDatePicker,
prevMonth,
nextMonth,
};
},
};
</script>
<style scoped>
.date-picker {
position: relative;
}
.date-picker-container {
position: absolute;
top: 30px;
left: 0;
padding: 10px;
border: 1px solid #ccc;
background-color: #fff;
}
.calendar-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 10px;
}
.calendar-prev,
.calendar-next {
font-size: 20px;
cursor: pointer;
}
.calendar-title {
font-weight: bold;
}
.calendar-row {
display: flex;
justify-content: space-between;
margin-bottom: 10px;
}
.calendar-cell {
width: calc(100% / 7);
text-align: center;
cursor: pointer;
}
.calendar-cell.day-header {
font-weight: bold;
}
.calendar-cell.disabled {
color: #ccc;
cursor: not-allowed;
}
.calendar-cell.selected {
background-color: #ccc;
}
</style>
```
使用方法:
```vue
<template>
<div>
<date-picker v-model="selectedDate" />
<p>Selected date: {{ selectedDate }}</p>
</div>
</template>
<script>
import DatePicker from './DatePicker.vue';
export default {
name: 'App',
components: {
DatePicker,
},
data() {
return {
selectedDate: '',
};
},
};
</script>
```
在上面的示例中,我们使用了 `ref` 和 `computed` 来处理组件中的状态和计算属性。我们还使用了 `v-model` 来实现日期选择框的双向绑定。在样式方面,我们使用了 `scoped` 属性,将样式限制在组件内部。
该组件的功能包括:
- 点击输入框弹出日期选择框
- 可以选择当前月份内的日期
- 选择的日期会在输入框中显示
- 可以通过左右箭头切换月份
- 当前月份外的日期会被禁用并显示为灰色
- 选中的日期会高亮显示为灰色
当然,该组件还可以根据不同的需求进行进一步的定制和优化。
阅读全文