| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <template>
- <Picker
- v-bind="props"
- :value="value"
- :columns="columns"
- @selectTextChange="() => {}"
- @update:value="updateValue"
- />
- </template>
- <script setup lang="ts">
- import { computed, onMounted } from 'vue';
- import type { PickerProps } from './Picker.vue';
- import type { PickerItem } from './Picker';
- import Picker from './Picker.vue';
- import { DateUtils } from '@imengyu/imengyu-utils';
- // 更新 DateTimePickerProps 接口,添加日期相关属性
- export interface DateTimePickerProps extends Omit<PickerProps, 'columns'|'value'> {
- modelValue?: Date,
- showYears?: boolean,
- showMonths?: boolean,
- showDays?: boolean,
- showHours?: boolean,
- showMinute?: boolean,
- showSecond?: boolean,
- startYear?: number,
- endYear?: number,
- startMonth?: number,
- endMonth?: number,
- startDay?: number,
- endDay?: number,
- minuteText?: string,
- secondText?: string,
- hourText?: string,
- yearText?: string,
- monthText?: string,
- dayText?: string,
- }
- const emit = defineEmits([ 'update:modelValue', 'selectTextChange' ]);
- const props = withDefaults(defineProps<DateTimePickerProps>(), {
- pickerHeight: 300,
- pickerWidth: 750,
- showYears: true,
- showMonths: true,
- showDays: true,
- showHours: true,
- showMinute: true,
- showSecond: true,
- startYear: () => new Date().getFullYear() - 50,
- endYear: () => new Date().getFullYear() + 20,
- startMonth: 1,
- endMonth: 12,
- startDay: 1,
- endDay: 31,
- yearText: '年',
- monthText: '月',
- dayText: '日',
- minuteText: '分',
- secondText: '秒',
- hourText: '时',
- });
- // 计算当前选中的值
- const value = computed(() => {
- const value : number[] = [];
- let date = props.modelValue;
- if (!date)
- date = new Date();
- if (!(date instanceof Date))
- date = typeof date === 'string' ? DateUtils.parseDate(date) : new Date();
- if (props.showYears)
- value.push(date.getFullYear());
- if (props.showMonths)
- value.push(date.getMonth() + 1);
- if (props.showDays)
- value.push(date.getDate());
- if (props.showHours)
- value.push(date.getHours());
- if (props.showMinute)
- value.push(date.getMinutes());
- if (props.showSecond)
- value.push(date.getSeconds());
- return value;
- });
- // 计算选择器的列数据
- const columns = computed(() => {
- const cols = [] as PickerItem[][];
- const currentDate = props.modelValue ?? new Date();
- // 年份列
- if (props.showYears) {
- const years = [] as PickerItem[];
- for (let i = props.startYear!; i <= props.endYear!; i++) {
- years.push({
- text: i.toString() + props.yearText,
- value: i,
- });
- }
- cols.push(years);
- }
- // 月份列
- if (props.showMonths) {
- const months = [] as PickerItem[];
- for (let i = props.startMonth!; i <= props.endMonth!; i++) {
- months.push({
- text: i.toString() + props.monthText,
- value: i,
- });
- }
- cols.push(months);
- }
- // 日期列
- if (props.showDays) {
- const selectedYear = props.showYears ? value.value[0] : currentDate.getFullYear();
- const selectedMonth = props.showMonths ? value.value[1] : currentDate.getMonth() + 1;
- const daysInMonth = DateUtils.getMonthDays(selectedYear, selectedMonth - 1) ?? 0;
- const days = [] as PickerItem[];
- for (let i = props.startDay; i <= daysInMonth; i++) {
- days.push({
- text: i.toString() + props.dayText,
- value: i,
- });
- }
- cols.push(days);
- }
- // 小时列
- if (props.showHours) {
- const hours = [] as PickerItem[];
- for (let i = 0; i < 24; i++) {
- hours.push({
- text: i.toString() + props.hourText,
- value: i,
- });
- }
- cols.push(hours);
- }
- // 分钟列
- if (props.showMinute) {
- const minutes = [] as PickerItem[];
- for (let i = 0; i < 60; i++) {
- minutes.push({
- text: i.toString() + props.minuteText,
- value: i,
- });
- }
- cols.push(minutes);
- }
- // 秒数列
- if (props.showSecond) {
- const seconds = [] as PickerItem[];
- for (let i = 0; i < 60; i++) {
- seconds.push({
- text: i.toString() + props.secondText,
- value: i,
- });
- }
- cols.push(seconds);
- }
- return cols;
- });
- // 更新选中的值
- function updateValue(v: number[]) {
- if (props.modelValue || v.length > 0) {
- const date = new Date(props.modelValue ?? new Date());
- let index = 0;
- if (v.length > 0) {
- if (props.showYears) {
- date.setFullYear(v[index]);
- index++;
- }
- if (props.showMonths) {
- date.setMonth(v[index] - 1);
- index++;
- }
- if (props.showDays) {
- date.setDate(v[index]);
- index++;
- }
- if (props.showHours) {
- date.setHours(v[index]);
- index++;
- }
- if (props.showMinute) {
- date.setMinutes(v[index]);
- index++;
- }
- if (props.showSecond) {
- date.setSeconds(v[index]);
- }
- emit('update:modelValue', date);
- }
- emit('selectTextChange', DateUtils.formatDate(date, 'yyyy-MM-dd HH:mm:ss'));
- } else {
- emit('selectTextChange', '');
- }
- }
- onMounted(() => updateValue([]));
- defineOptions({
- options: {
- styleIsolation: "shared",
- virtualHost: true,
- }
- })
- </script>
|