[New] SingleDatePickerInputController: Allow passing multiple date formats to make validation more permissive#1644
Draft
Floriferous wants to merge 4 commits intoreact-dates:masterfrom
Draft
[New] SingleDatePickerInputController: Allow passing multiple date formats to make validation more permissive#1644Floriferous wants to merge 4 commits intoreact-dates:masterfrom
SingleDatePickerInputController: Allow passing multiple date formats to make validation more permissive#1644Floriferous wants to merge 4 commits intoreact-dates:masterfrom
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.
ljharb
requested changes
Mar 4, 2022
Member
ljharb
left a comment
There was a problem hiding this comment.
Thanks! We'll need a documentation update, and lots of tests to cover this addition.
Comment on lines
+178
to
+185
|
|
||
| if (typeof displayFormat === 'string') { | ||
| return displayFormat; | ||
| } else if (Array.isArray(displayFormat)) { | ||
| return displayFormat[0]; | ||
| } | ||
|
|
||
| return displayFormat(); |
Member
There was a problem hiding this comment.
Suggested change
| 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; |
Member
There was a problem hiding this comment.
Suggested change
| return displayFormat; | |
| return [].concat(displayFormat); |
this way it's always an array
Comment on lines
+6
to
+14
| 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] | ||
| } |
Member
There was a problem hiding this comment.
Suggested change
| 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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/YYYYandMM/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!