Skip to content
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

DateInput default settings can easily crash application in dev mode #4251

Open
adidahiya opened this issue Jul 29, 2020 · 2 comments
Open

DateInput default settings can easily crash application in dev mode #4251

adidahiya opened this issue Jul 29, 2020 · 2 comments

Comments

@adidahiya
Copy link
Contributor

Summarizing a collection of grievances in #3394, #2965, #3407, and other issues:

Environment

  • Package version(s): datetime 3.18.5
  • Operating System: macOS
  • Browser name and version: any

Code Sandbox

From @barrymay in #3394 (comment):

https://codesandbox.io/s/angry-morse-hn3rm?file=/src/App.js

To see the problem:

  • Enter 1/1/1999. Hit 'enter'

Actual behavior

Page crashes during validateProps() lifecycle method

Expected behavior

Page does not crash, instead we get some nicer out-of-the-box error handling like the docs example:

2020-07-29 13 52 03

Possible solution

The developer has provided a valid parseDate function, but since they are using the default values of minDate and maxDate set by the component, they are not able to properly validate whether the entered date is out of bounds. Another option would be to wrap the component in an ErrorBoundary, but that's clunky and heavy-handed.

In general I think users should be setting their own minDate and maxDate bounds. But since we have already set the defaults in the API I think we should improve the developer experience here without breaking the API.

@barrymay
Copy link

barrymay commented Jul 29, 2020

Thanks for collecting these together.

On your comment above, I don't mind setting minDate and maxDate (I'm likely to do that). But even when I do that, there's no workaround way I can see to get to the 'Out of range' message from the demo when the user types in a bad (out of range) date and hits enter, at least not in dev NODE_ENV mode, due the impact of validateProps.

My workaround at present it is to wrap the component and test the min/max in the parse date function to return a null (empty result) when that occurs, but that's still not as good as the demo result.

@Ajaybhardwaj7
Copy link

Ajaybhardwaj7 commented Aug 14, 2020

This can help atleast crashing and falling back to values instead "invalid date" for out of range message. This also have some caveats.

function dateOptions(format: string, minDate: Date, maxDate : Date): IDateFormatProps {
	return {
		formatDate: (date) => moment(date).format(format),
		parseDate: (str) => {
            const momentDate = moment(str, format);
            const isValidInput = momentDate.isValid();
            if(isValidInput && momentDate.isBefore(minDate)){
                return minDate;
            } else if(isValidInput && momentDate.isAfter(maxDate)){
                return maxDate;
            } else if(isValidInput){
                return momentDate.toDate();
            }else {
                return false;
            }
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants