-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[New] SingleDatePickerInputController
: Allow passing multiple date formats to make validation more permissive
#1644
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
base: master
Are you sure you want to change the base?
Conversation
If an array is passed to dateFormat, it will try to validate for each of them
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as spam.
This comment was marked as spam.
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.
Thanks! We'll need a documentation update, and lots of tests to cover this addition.
|
||
if (typeof displayFormat === 'string') { | ||
return displayFormat; | ||
} else if (Array.isArray(displayFormat)) { | ||
return displayFormat[0]; | ||
} | ||
|
||
return displayFormat(); |
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.
if (typeof displayFormat === 'string') { | |
return displayFormat; | |
} else if (Array.isArray(displayFormat)) { | |
return displayFormat[0]; | |
} | |
return displayFormat(); | |
return typeof displayFormat === 'function' ? displayFormat() : [].concat(displayFormat)[0]; |
return displayFormat(); | ||
} | ||
|
||
return displayFormat; |
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.
return displayFormat; | |
return [].concat(displayFormat); |
this way it's always an array
let dateFormats = []; | ||
|
||
if (Array.isArray(customFormat)) { | ||
dateFormats = [...customFormat, DISPLAY_FORMAT, ISO_FORMAT] | ||
} else if (customFormat) { | ||
dateFormats = [customFormat, DISPLAY_FORMAT, ISO_FORMAT] | ||
} else { | ||
dateFormats = [DISPLAY_FORMAT, ISO_FORMAT] | ||
} |
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.
let dateFormats = []; | |
if (Array.isArray(customFormat)) { | |
dateFormats = [...customFormat, DISPLAY_FORMAT, ISO_FORMAT] | |
} else if (customFormat) { | |
dateFormats = [customFormat, DISPLAY_FORMAT, ISO_FORMAT] | |
} else { | |
dateFormats = [DISPLAY_FORMAT, ISO_FORMAT] | |
} | |
const dateFormats = [].concat( | |
customFormat || [], | |
DISPLAY_FORMAT, | |
ISO_FORMAT, | |
]); |
SingleDatePickerInputController
: Allow passing multiple date formats to make validation more permissive
Closes #345.
This is a proposal at the moment, I didn't yet look at all the other places this needs attention for. So I'd like to get some feedback before going in deeper.
The basic idea is this:
The first format in the array will be used to display dates, and all the other ones will be used to validate it while typing, to improve the user experience, as fewer inputs will be rejected (there is also no error shown, leaving users stranded).
The main issue with this pattern, is that you can provide conflicting date formats, such as
DD/MM/YYYY
andMM/DD/YYYY
, in which case it's unclear what will happen. I could try to write a helper to throw when this happens, but documentation might be enough.However I believe the benefit outweighs the potential misuse, as just my example above with a couple more formats will help a lot!