Skip to content

Commit

Permalink
fix(date-picker): 修复数据联动更新问题 (#2824)
Browse files Browse the repository at this point in the history
  • Loading branch information
eiinu committed Jan 10, 2024
1 parent fe8fc9e commit 89747d8
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 61 deletions.
67 changes: 36 additions & 31 deletions src/packages/__VUE/datepicker/index.taro.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { PickerOption } from '../picker/types';
import { createComponent } from '@/packages/utils/create';
import { Formatter, Filter } from './type';
import { padZero, isDate as isDateU } from '@/packages/utils/util';
import { nextTick } from '@tarojs/taro';
const { create } = createComponent('date-picker');
const currentYear = new Date().getFullYear();
Expand Down Expand Up @@ -148,7 +149,7 @@ export default create({
hour = 23;
minute = 59;
}
const seconds = minute;
let seconds = minute;
if (value.getFullYear() === year) {
month = boundary.getMonth() + 1;
if (value.getMonth() + 1 === month) {
Expand All @@ -157,6 +158,9 @@ export default create({
hour = boundary.getHours();
if (value.getHours() === hour) {
minute = boundary.getMinutes();
if (value.getMinutes() === minute) {
seconds = boundary.getSeconds();
}
}
}
}
Expand Down Expand Up @@ -221,37 +225,35 @@ export default create({
selectedValue: (string | number)[];
selectedOptions: PickerOption[];
}) => {
if (['date', 'datetime', 'datehour', 'month-day', 'year-month', 'hour-minute'].includes(props.type)) {
let formatDate: (number | string)[] = [];
selectedValue.forEach((item) => {
formatDate.push(item);
});
if (props.type == 'month-day' && formatDate.length < 3) {
formatDate.unshift(new Date(state.currentDate || props.minDate || props.maxDate).getFullYear());
}
if (props.type == 'year-month' && formatDate.length < 3) {
formatDate.push(new Date(state.currentDate || props.minDate || props.maxDate).getDate());
}
let formatDate: (number | string)[] = [];
selectedValue.forEach((item) => {
formatDate.push(item);
});
if (props.type == 'month-day' && formatDate.length < 3) {
formatDate.unshift(new Date(state.currentDate || props.minDate || props.maxDate).getFullYear());
}
if (props.type == 'year-month' && formatDate.length < 3) {
formatDate.push(new Date(state.currentDate || props.minDate || props.maxDate).getDate());
}
const year = Number(formatDate[0]);
const month = Number(formatDate[1]) - 1;
const day = Math.min(Number(formatDate[2]), getMonthEndDay(Number(formatDate[0]), Number(formatDate[1])));
let date: Date | null = null;
if (props.type === 'date' || props.type === 'month-day' || props.type === 'year-month') {
date = new Date(year, month, day);
} else if (props.type === 'datetime') {
date = new Date(year, month, day, Number(formatDate[3]), Number(formatDate[4]));
} else if (props.type === 'datehour') {
date = new Date(year, month, day, Number(formatDate[3]));
} else if (props.type === 'hour-minute') {
date = new Date(state.currentDate);
const year = date.getFullYear();
const month = date.getMonth();
const day = date.getDate();
date = new Date(year, month, day, Number(formatDate[0]), Number(formatDate[1]));
}
state.currentDate = formatValue(date as Date);
const year = Number(formatDate[0]);
const month = Number(formatDate[1]) - 1;
const day = Math.min(Number(formatDate[2]), getMonthEndDay(Number(formatDate[0]), Number(formatDate[1])));
let date: Date | null = null;
if (props.type === 'date' || props.type === 'month-day' || props.type === 'year-month') {
date = new Date(year, month, day);
} else if (props.type === 'datetime') {
date = new Date(year, month, day, Number(formatDate[3]), Number(formatDate[4]));
} else if (props.type === 'datehour') {
date = new Date(year, month, day, Number(formatDate[3]));
} else if (props.type === 'hour-minute' || props.type === 'time') {
date = new Date(state.currentDate);
const year = date.getFullYear();
const month = date.getMonth();
const day = date.getDate();
date = new Date(year, month, day, Number(formatDate[0]), Number(formatDate[1]), Number(formatDate[2] || 0));
}
state.currentDate = formatValue(date as Date);
emit('change', { columnIndex, selectedValue, selectedOptions });
};
Expand Down Expand Up @@ -282,7 +284,7 @@ export default create({
min++;
}
if (min <= +val) {
if (min <= Number(val)) {
index++;
}
}
Expand Down Expand Up @@ -376,6 +378,9 @@ export default create({
const isSameValue = JSON.stringify(newValues) === JSON.stringify(props.modelValue);
if (!isSameValue) {
emit('update:modelValue', newValues);
nextTick(() => {
state.selectedValue = getSelectedValue(newValues);
});
}
}
);
Expand Down
61 changes: 31 additions & 30 deletions src/packages/__VUE/datepicker/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export default create({
hour = 23;
minute = 59;
}
const seconds = minute;
let seconds = minute;
if (value.getFullYear() === year) {
month = boundary.getMonth() + 1;
if (value.getMonth() + 1 === month) {
Expand All @@ -157,6 +157,9 @@ export default create({
hour = boundary.getHours();
if (value.getHours() === hour) {
minute = boundary.getMinutes();
if (value.getMinutes() === minute) {
seconds = boundary.getSeconds();
}
}
}
}
Expand Down Expand Up @@ -221,37 +224,35 @@ export default create({
selectedValue: (string | number)[];
selectedOptions: PickerOption[];
}) => {
if (['date', 'datetime', 'datehour', 'month-day', 'year-month', 'hour-minute'].includes(props.type)) {
let formatDate: (number | string)[] = [];
selectedValue.forEach((item) => {
formatDate.push(item);
});
if (props.type == 'month-day' && formatDate.length < 3) {
formatDate.unshift(new Date(state.currentDate || props.minDate || props.maxDate).getFullYear());
}
if (props.type == 'year-month' && formatDate.length < 3) {
formatDate.push(new Date(state.currentDate || props.minDate || props.maxDate).getDate());
}
let formatDate: (number | string)[] = [];
selectedValue.forEach((item) => {
formatDate.push(item);
});
if (props.type == 'month-day' && formatDate.length < 3) {
formatDate.unshift(new Date(state.currentDate || props.minDate || props.maxDate).getFullYear());
}
if (props.type == 'year-month' && formatDate.length < 3) {
formatDate.push(new Date(state.currentDate || props.minDate || props.maxDate).getDate());
}
const year = Number(formatDate[0]);
const month = Number(formatDate[1]) - 1;
const day = Math.min(Number(formatDate[2]), getMonthEndDay(Number(formatDate[0]), Number(formatDate[1])));
let date: Date | null = null;
if (props.type === 'date' || props.type === 'month-day' || props.type === 'year-month') {
date = new Date(year, month, day);
} else if (props.type === 'datetime') {
date = new Date(year, month, day, Number(formatDate[3]), Number(formatDate[4]));
} else if (props.type === 'datehour') {
date = new Date(year, month, day, Number(formatDate[3]));
} else if (props.type === 'hour-minute') {
date = new Date(state.currentDate);
const year = date.getFullYear();
const month = date.getMonth();
const day = date.getDate();
date = new Date(year, month, day, Number(formatDate[0]), Number(formatDate[1]));
}
state.currentDate = formatValue(date as Date);
const year = Number(formatDate[0]);
const month = Number(formatDate[1]) - 1;
const day = Math.min(Number(formatDate[2]), getMonthEndDay(Number(formatDate[0]), Number(formatDate[1])));
let date: Date | null = null;
if (props.type === 'date' || props.type === 'month-day' || props.type === 'year-month') {
date = new Date(year, month, day);
} else if (props.type === 'datetime') {
date = new Date(year, month, day, Number(formatDate[3]), Number(formatDate[4]));
} else if (props.type === 'datehour') {
date = new Date(year, month, day, Number(formatDate[3]));
} else if (props.type === 'hour-minute' || props.type === 'time') {
date = new Date(state.currentDate);
const year = date.getFullYear();
const month = date.getMonth();
const day = date.getDate();
date = new Date(year, month, day, Number(formatDate[0]), Number(formatDate[1]), Number(formatDate[2] || 0));
}
state.currentDate = formatValue(date as Date);
emit('change', { columnIndex, selectedValue, selectedOptions });
};
Expand Down

0 comments on commit 89747d8

Please sign in to comment.