当前位置: 首页 > news >正文

vue3 ts 写一个滑动选择的日期选择器组件

vue3 ts 写一个滑动选择的日期选择器组件

在这里插入图片描述

DatePicker.vue

<template><div class="date-picker-container"><div class="date-picker"><select v-model="selectedYear" @change="updateDays"><option value="">Year</option><option v-for="year in years" :key="year" :value="year">{{ year }}</option></select>-<select v-model="selectedMonth" @change="updateDays"><option value="">Month</option><option v-for="month in months" :key="month" :value="month">{{ String(month).padStart(2, "0") }}</option>-</select>-<select v-model="selectedDay"><option value="">Day</option><option v-for="day in days" :key="day" :value="day">{{ String(day).padStart(2, "0") }}</option></select></div></div>
</template><script lang="ts" setup>
import { ref, computed, watch, onMounted } from "vue";const props = defineProps({modelValue: {type: String,default: "",},
});const emit = defineEmits(["update:modelValue"]);const years = ref<number[]>([]);
const months = ref<number[]>([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
const days = ref<number[]>([]);const selectedYear = ref<string>("");
const selectedMonth = ref<string>("");
const selectedDay = ref<string>("");const currentYear = new Date().getFullYear();
years.value = Array.from({ length: 150 }, (_, i) => currentYear - 100 + i);const updateDays = () => {if (!selectedYear.value || !selectedMonth.value) {days.value = [];return;}const year = parseInt(selectedYear.value, 10);const month = parseInt(selectedMonth.value, 10);const daysInMonth = new Date(year, month, 0).getDate();days.value = Array.from({ length: daysInMonth }, (_, i) => i + 1);
};const selectedDate = computed(() => {if (selectedYear.value && selectedMonth.value && selectedDay.value) {return `${selectedYear.value}-${String(selectedMonth.value).padStart(2,"0")}-${String(selectedDay.value).padStart(2, "0")}`;}return "";
});// 初始化值
onMounted(() => {});
// 改进的初始化函数
const initFromDateString = (dateStr: string) => {if (!dateStr) return;// 尝试解析各种可能的日期格式const parts = dateStr.split("-");if (parts.length === 3) {selectedYear.value = parts[0];// 检查 parts[1] 是否以 '0' 开头if (parts[1].startsWith("0")) {// 转换为数字selectedMonth.value = Number(parts[1]).toString(); // 或 +parts[1] 或 Number(parts[1])} else {// 如果不以 '0' 开头,保持原样或进行其他处理selectedMonth.value = parts[1];}if (parts[2].startsWith("0")) {// 转换为数字selectedDay.value = Number(parts[2]).toString(); // 或 +parts[1] 或 Number(parts[1])} else {// 如果不以 '0' 开头,保持原样或进行其他处理selectedDay.value = parts[2];}}updateDays();
};
// 监听 modelValue 变化
watch(() => props.modelValue,(newVal) => {initFromDateString(newVal);},{ immediate: true }
);
// 监听选择的变化
watch([selectedYear, selectedMonth, selectedDay],() => {const dateStr = selectedDate.value;emit("update:modelValue", dateStr);},{ immediate: true }
);
</script><style scoped>
.date-picker-container {display: flex;flex-direction: column;gap: 10px;width: 100%;
}
.date-picker {display: flex;gap: 10px;
}
select {width: 33%;padding: 5px;font-size: 16px;border-radius: 5px;border: 1px solid #dcdfe6;background-color: #fff;color: #606266;
}
</style>

在需要的地方引入

<template><DatePicker v-model="ruleForm.Dateofbirth" />
<template>
...
<script lang="ts" setup name="FormInfo">import DatePicker from "./DatePicker.vue";
</script>

相关文章:

  • 值拷贝、浅拷贝和深拷贝
  • 一、每日Github软件分享----QuickGo外链直达工具​
  • LeetCode面试题 17.21 直方图的水量
  • ABP vNext + EF Core 实战性能调优指南
  • 浏览器自动化与网络爬虫实战:工具对比与选型指南
  • Liunx ContOS7 安装部署 Docker
  • Vue——Axios
  • ESP32开发入门(七):HTTP开发实践
  • Spring框架(1)
  • Idea Code Templates配置
  • CCDO|企业数字化转型:机制革新与人才培育的双重引擎
  • 【库(Library)、包(Package)和模块(Module)解析】
  • 关系模式-无损连接和保持函数依赖的判断
  • WPF 3D图形编程核心技术解析
  • 互联网大厂Java求职面试:基于AI的实时异常检测系统设计与实现
  • JAVA将一个同步方法改为异步执行
  • Altera系列FPGA纯verilog视频图像去雾,基于暗通道先验算法实现,提供4套Quartus工程源码和技术支持
  • 基 LabVIEW 的多轴电机控制系统
  • 如何训练deepseek语言大模型
  • Vue3 中 ref 与 reactive 的区别及底层原理详解
  • 沃旭能源因成本上升放弃英国海上风电项目,或损失近40亿元
  • 外交部:习近平主席同普京总统达成许多新的重要共识
  • 援藏博士张兴堂已任西藏农牧学院党委书记、副校长
  • 河南省平顶山市副市长许红兵主动投案,接受审查调查
  • 欧盟委员会计划对950亿欧元美国进口产品采取反制措施
  • 司法部谈民营经济促进法:对违规异地执法问题作出禁止性规定