mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-24 02:16:12 +08:00
[bugfix] DatetimePicker: value uncorrect when use formatter (#1591)
This commit is contained in:
parent
fe5aae7515
commit
58eab6f2df
@ -32,7 +32,6 @@ export default create({
|
|||||||
value: {},
|
value: {},
|
||||||
title: String,
|
title: String,
|
||||||
itemHeight: Number,
|
itemHeight: Number,
|
||||||
formatter: Function,
|
|
||||||
visibleItemCount: Number,
|
visibleItemCount: Number,
|
||||||
confirmButtonText: String,
|
confirmButtonText: String,
|
||||||
cancelButtonText: String,
|
cancelButtonText: String,
|
||||||
@ -48,6 +47,10 @@ export default create({
|
|||||||
type: String,
|
type: String,
|
||||||
default: 'YYYY.MM.DD HH时 mm分'
|
default: 'YYYY.MM.DD HH时 mm分'
|
||||||
},
|
},
|
||||||
|
formatter: {
|
||||||
|
type: Function,
|
||||||
|
default: (type, value) => value
|
||||||
|
},
|
||||||
minDate: {
|
minDate: {
|
||||||
type: Date,
|
type: Date,
|
||||||
default: () => new Date(currentYear - 10, 0, 1),
|
default: () => new Date(currentYear - 10, 0, 1),
|
||||||
@ -146,7 +149,7 @@ export default create({
|
|||||||
const values = this.times(range[1] - range[0] + 1, index => {
|
const values = this.times(range[1] - range[0] + 1, index => {
|
||||||
let value = range[0] + index;
|
let value = range[0] + index;
|
||||||
value = value < 10 ? `0${value}` : `${value}`;
|
value = value < 10 ? `0${value}` : `${value}`;
|
||||||
return this.formatter ? this.formatter(type, value) : value;
|
return this.formatter(type, value);
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -159,6 +162,10 @@ export default create({
|
|||||||
},
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
|
pad(val) {
|
||||||
|
return `00${val}`.slice(-2);
|
||||||
|
},
|
||||||
|
|
||||||
correctValue(value) {
|
correctValue(value) {
|
||||||
// validate value
|
// validate value
|
||||||
const isDateType = this.type !== 'time';
|
const isDateType = this.type !== 'time';
|
||||||
@ -173,8 +180,7 @@ export default create({
|
|||||||
if (!isDateType) {
|
if (!isDateType) {
|
||||||
const [hour, minute] = value.split(':');
|
const [hour, minute] = value.split(':');
|
||||||
let correctedHour = Math.max(hour, this.minHour);
|
let correctedHour = Math.max(hour, this.minHour);
|
||||||
correctedHour = Math.min(correctedHour, this.maxHour);
|
correctedHour = this.pad(Math.min(correctedHour, this.maxHour));
|
||||||
correctedHour = `00${correctedHour}`.slice(-2);
|
|
||||||
|
|
||||||
return `${correctedHour}:${minute}`;
|
return `${correctedHour}:${minute}`;
|
||||||
}
|
}
|
||||||
@ -302,22 +308,24 @@ export default create({
|
|||||||
|
|
||||||
updateColumnValue(value) {
|
updateColumnValue(value) {
|
||||||
let values = [];
|
let values = [];
|
||||||
|
const { formatter, pad } = this;
|
||||||
|
|
||||||
if (this.type === 'time') {
|
if (this.type === 'time') {
|
||||||
const currentValue = value.split(':');
|
const currentValue = value.split(':');
|
||||||
values = [
|
values = [
|
||||||
currentValue[0],
|
formatter('hour', currentValue[0]),
|
||||||
currentValue[1]
|
formatter('minute', currentValue[1])
|
||||||
];
|
];
|
||||||
} else {
|
} else {
|
||||||
values = [
|
values = [
|
||||||
`${value.getFullYear()}`,
|
formatter('year', `${value.getFullYear()}`),
|
||||||
`0${value.getMonth() + 1}`.slice(-2),
|
formatter('month', pad(value.getMonth() + 1)),
|
||||||
`0${value.getDate()}`.slice(-2)
|
formatter('day', pad(value.getDate()))
|
||||||
];
|
];
|
||||||
if (this.type === 'datetime') {
|
if (this.type === 'datetime') {
|
||||||
values.push(
|
values.push(
|
||||||
`0${value.getHours()}`.slice(-2),
|
formatter('hour', pad(value.getHours())),
|
||||||
`0${value.getMinutes()}`.slice(-2)
|
formatter('minute', pad(value.getMinutes()))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (this.type === 'year-month') {
|
if (this.type === 'year-month') {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user