-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle error for values of invalid data types in DateField #9119
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,7 +61,18 @@ const DateFieldImpl = < | |
} | ||
|
||
const value = get(record, source); | ||
if (value == null || value === '') { | ||
const parsedDate = (value: any) => { | ||
if (value == null || value === '') return undefined; | ||
|
||
return value instanceof Date | ||
? value | ||
: typeof value === 'string' || typeof value === 'number' | ||
? new Date(value) | ||
: undefined; | ||
}; | ||
const date = parsedDate(value); | ||
|
||
if (typeof date === 'undefined') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please keep the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if I understood this request properly: Are you asking to have BOTH The way I see it the null check is handled before (in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No you're right, the check is already made in parsedDate, you can keep your code as is for this part. |
||
return emptyText ? ( | ||
<Typography | ||
component="span" | ||
|
@@ -74,13 +85,6 @@ const DateFieldImpl = < | |
) : null; | ||
} | ||
|
||
const date = | ||
value instanceof Date | ||
? value | ||
: typeof value === 'string' || typeof value === 'number' | ||
? new Date(value) | ||
: undefined; | ||
|
||
let dateOptions = options; | ||
if ( | ||
typeof value === 'string' && | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I doin't understand why you extracted to a function just to call it afterwards.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did it to encapsulate the logic for validating whether the value can represent a Date or not. Probably a matter of style and personal preference, I can change it to inline code obviously.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, please inline this code