Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
userkks committed May 31, 2023
1 parent 2107f40 commit 063f973
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
29 changes: 20 additions & 9 deletions components/lib/calendar/Calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
}

Expand Down
13 changes: 10 additions & 3 deletions components/lib/utils/ObjectUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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');
}
}

0 comments on commit 063f973

Please sign in to comment.