Skip to content

[New] SingleDatePickerInputController: Allow passing multiple date formats to make validation more permissive#1644

Draft
Floriferous wants to merge 4 commits intoreact-dates:masterfrom
Floriferous:patch-1
Draft

[New] SingleDatePickerInputController: Allow passing multiple date formats to make validation more permissive#1644
Floriferous wants to merge 4 commits intoreact-dates:masterfrom
Floriferous:patch-1

Conversation

@Floriferous
Copy link
Copy Markdown

@Floriferous Floriferous commented May 15, 2019

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:

<SingleDatePicker
  dateFormat={["D MMMM YYYY", "D/M/YYYY", "D.M.YYYY"]}
/>

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 and MM/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!

@Floriferous Floriferous marked this pull request as ready for review July 26, 2019 19:50
@daviddelusenet

This comment was marked as outdated.

@Lukasz-Trzaskowski

This comment was marked as spam.

Copy link
Copy Markdown
Member

@ljharb ljharb left a 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.

Comment on lines +178 to +185

if (typeof displayFormat === 'string') {
return displayFormat;
} else if (Array.isArray(displayFormat)) {
return displayFormat[0];
}

return displayFormat();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,
]);

@ljharb ljharb changed the title Allow passing multiple date formats to make validation more permissive [New] SingleDatePickerInputController: Allow passing multiple date formats to make validation more permissive Mar 4, 2022
@ljharb ljharb added the semver-minor: new stuff Any feature or API addition. label Mar 4, 2022
@ljharb ljharb marked this pull request as draft March 4, 2022 22:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

semver-minor: new stuff Any feature or API addition.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Validate input using multiple formats

4 participants