fix(DatetimePicker): infinite loop when use formatted text is unnumeric (#4485)

This commit is contained in:
neverland 2019-09-18 14:22:04 +08:00 committed by GitHub
parent 2038d135bc
commit 31ceba6c38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 7 deletions

View File

@ -129,14 +129,18 @@ export default createComponent({
}, },
onChange(picker) { onChange(picker) {
const values = picker.getValues(); const indexes = picker.getIndexes();
const year = getTrueValue(values[0]); const getValue = index => getTrueValue(this.originColumns[index].values[indexes[index]]);
const month = getTrueValue(values[1]);
const year = getValue(0);
const month = getValue(1);
const maxDate = getMonthEndDay(year, month); const maxDate = getMonthEndDay(year, month);
let date = getTrueValue(values[2]); let date;
if (this.type === 'year-month') { if (this.type === 'year-month') {
date = 1; date = 1;
} else {
date = getValue(2);
} }
date = date > maxDate ? maxDate : date; date = date > maxDate ? maxDate : date;
@ -145,8 +149,8 @@ export default createComponent({
let minute = 0; let minute = 0;
if (this.type === 'datetime') { if (this.type === 'datetime') {
hour = getTrueValue(values[3]); hour = getValue(3);
minute = getTrueValue(values[4]); minute = getValue(4);
} }
const value = new Date(year, month - 1, date, hour, minute); const value = new Date(year, month - 1, date, hour, minute);

View File

@ -17,7 +17,11 @@ export function getTrueValue(value: string | undefined): number | undefined {
} }
while (isNaN(parseInt(value, 10))) { while (isNaN(parseInt(value, 10))) {
value = value.slice(1); if (value.length > 1) {
value = value.slice(1);
} else {
return;
}
} }
return parseInt(value, 10); return parseInt(value, 10);