Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(date-picker): 修复数据联动更新问题 #2824

Merged
merged 6 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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 @@
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());
}

Check warning on line 233 in src/packages/__VUE/datepicker/index.vue

View check run for this annotation

Codecov / codecov/patch

src/packages/__VUE/datepicker/index.vue#L232-L233

Added lines #L232 - L233 were not covered by tests
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]));

Check warning on line 245 in src/packages/__VUE/datepicker/index.vue

View check run for this annotation

Codecov / codecov/patch

src/packages/__VUE/datepicker/index.vue#L245

Added line #L245 was not covered by tests
} else if (props.type === 'datehour') {
date = new Date(year, month, day, Number(formatDate[3]));

Check warning on line 247 in src/packages/__VUE/datepicker/index.vue

View check run for this annotation

Codecov / codecov/patch

src/packages/__VUE/datepicker/index.vue#L247

Added line #L247 was not covered by tests
} 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