Skip to content

Commit

Permalink
fix:修复 datepicker 输入日期无效
Browse files Browse the repository at this point in the history
  • Loading branch information
fikyair committed Nov 22, 2021
1 parent fcf610e commit cbee7f5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 34 deletions.
27 changes: 0 additions & 27 deletions src/components/InputDatePicker/README.mdx

This file was deleted.

1 change: 1 addition & 0 deletions src/components/InputDatePicker/inputDatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const InputDatePicker: FC<InputDatePickerProps> = (props) => {

function handleOnChange(e: ChangeEvent<Element>, payload: any) {
onChange && onChange(e, payload);
console.log('payload: ', payload);
if (payload.origin === "PICKER") {
// TODO: 未获取到 ref
ref.current && ref.current.focus();
Expand Down
14 changes: 7 additions & 7 deletions src/components/InputDatePicker/utils/date-extraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function dateToStr(date: Date) {
}

function getDateRegexp(dateFormat: string) {
//MM-DD-YYYY [MM,DD,YYYY]
//MM-dd-YYYY [MM,dd,YYYY]
const dateFormatAsRegexp = dateFormat
.replace(/[A-Za-z]{4}/g, "([0-9]{4})")
.replace(/[A-Za-z]{2}/g, "([0-9]{2})");
Expand All @@ -19,7 +19,7 @@ function getDateRegexp(dateFormat: string) {
}

function DatePickerException(code: string) {
this.code = code;
return code;
}

export function strToDate(
Expand All @@ -31,13 +31,13 @@ export function strToDate(
const dateErrors = [];

if (!dateMatches) {
dateErrors.push(new DatePickerException("INVALID_DATE_FORMAT"));
dateErrors.push(DatePickerException("INVALID_DATE_FORMAT"));
throw dateErrors;
}

const yearIndex = partsOrder.indexOf("YYYY");
const monthIndex = partsOrder.indexOf("MM");
const dayIndex = partsOrder.indexOf("DD");
const dayIndex = partsOrder.indexOf("dd");

const yearString = dateMatches[yearIndex + 1];
const monthString = dateMatches[monthIndex + 1];
Expand All @@ -46,17 +46,17 @@ export function strToDate(
const month = parseInt(monthString, 10);

if (month === 0 || month > 12) {
dateErrors.push(new DatePickerException("INVALID_MONTH_NUMBER"));
dateErrors.push(DatePickerException("INVALID_MONTH_NUMBER"));
}
const day = parseInt(dayString, 10);
if (day === 0) {
dateErrors.push(new DatePickerException("INVALID_DAY_NUMBER"));
dateErrors.push(DatePickerException("INVALID_DAY_NUMBER"));
}
const year = parseInt(yearString, 10);
const monthDate = new Date(year, month - 1);
const lastDay = lastDayOfMonth(monthDate);
if (day > getDate(lastDay)) {
dateErrors.push(new DatePickerException("INVALID_DAY_OF_MONTH"));
dateErrors.push(DatePickerException("INVALID_DAY_OF_MONTH"));
}

if (dateErrors.length > 0) {
Expand Down

0 comments on commit cbee7f5

Please sign in to comment.