diff --git a/components/lib/calendar/Calendar.js b/components/lib/calendar/Calendar.js index 897ba64fa2..63f98946be 100644 --- a/components/lib/calendar/Calendar.js +++ b/components/lib/calendar/Calendar.js @@ -3178,23 +3178,34 @@ export const Calendar = React.memo( const changeDateWithCursor = (e, dateFieldMethod, monthFieldMethod, yearFieldMethod) => { const cursorPosition = e.target.selectionStart; - if (cursorPosition <= 2) { dateFieldMethod(); e.target.setSelectionRange(0, 2); } - else if (cursorPosition <= 5) { monthFieldMethod(); e.target.setSelectionRange(3, 5); } - else { yearFieldMethod(); e.target.setSelectionRange(6, 10); } + if (cursorPosition <= 2) { dateFieldMethod(); Promise.resolve().then(() => e.target.setSelectionRange(0, 2)) } + else if (cursorPosition <= 5) { monthFieldMethod(); Promise.resolve().then(() => e.target.setSelectionRange(3, 5)) } + else { yearFieldMethod(); Promise.resolve().then(() => e.target.setSelectionRange(6, 10)) } } const onInputChange = (e) => { - console.log(e); let dateFieldMethod, monthFieldMethod, yearFieldMethod; - const [day, month, year] = e.target.value.split('-'); + const [day, month, year] = inputValue.split('-'); let newDay = day, newMonth = month, newYear = year; // for numbers + const keyValue = Number(e.nativeEvent.data); - if (e.which >= 48 && e.which <= 57) { + if (keyValue >= 0 && keyValue <= 9) { dateFieldMethod = () => { - if (day === 'DD') newDay = `0${e.target.value}`; - else if (Number(day) < 3 || (Number(day) === 3 && e.target.value < 2)) newDay = `${Number(day) * 10 + e.target.value}` - else newDay = '31' + if (day === 'DD' || day > 9) newDay = ObjectUtils.normalizeNumberDigits(keyValue, 2); + else if (Number(day) < 3 || (Number(day) === 3 && keyValue < 2)) newDay = `${ObjectUtils.normalizeNumberDigits(Number(day) * 10 + keyValue, 2)}`; + else newDay = '31'; + }; + + monthFieldMethod = () => { + if (month === 'MM' || month > 9) newMonth = ObjectUtils.normalizeNumberDigits(keyValue, 2); + else if (Number(month) === 0 || (Number(month) === 1 && keyValue < 3)) newMonth = `${ObjectUtils.normalizeNumberDigits(Number(month) * 10 + keyValue, 2)}`; + else newMonth = '12'; + }; + + yearFieldMethod = () => { + if (year === 'YYYY' || year > 999) newYear = ObjectUtils.normalizeNumberDigits(keyValue, 4); + else newYear = ObjectUtils.normalizeNumberDigits(Number(year) * 10 + keyValue, 4); }; } diff --git a/components/lib/utils/ObjectUtils.js b/components/lib/utils/ObjectUtils.js index d88cc0b9e2..b0ed640ed8 100644 --- a/components/lib/utils/ObjectUtils.js +++ b/components/lib/utils/ObjectUtils.js @@ -17,7 +17,7 @@ export default class ObjectUtils { if (arrA && arrB) { length = a.length; if (length !== b.length) return false; - for (i = length; i-- !== 0; ) if (!this.deepEquals(a[i], b[i])) return false; + for (i = length; i-- !== 0;) if (!this.deepEquals(a[i], b[i])) return false; return true; } @@ -42,9 +42,9 @@ export default class ObjectUtils { if (length !== Object.keys(b).length) return false; - for (i = length; i-- !== 0; ) if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false; + for (i = length; i-- !== 0;) if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false; - for (i = length; i-- !== 0; ) { + for (i = length; i-- !== 0;) { key = keys[i]; if (!this.deepEquals(a[key], b[key])) return false; } @@ -298,4 +298,11 @@ export default class ObjectUtils { return result; } + + static normalizeNumberDigits(num, digit) { + if (isNaN(num)) throw new Error(`Expected Number: got ${typeof num}`); + if (Number(num).toString().length > digit) return '0'.padStart(digit, '0'); + + return Number(num).toString().padStart(digit, '0'); + } }