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

Reset DateInput field value after state change. #5517

Merged
merged 8 commits into from Aug 11, 2021
Merged

Reset DateInput field value after state change. #5517

merged 8 commits into from Aug 11, 2021

Conversation

AlbertoTonegari
Copy link
Contributor

@AlbertoTonegari AlbertoTonegari commented Aug 9, 2021

What does this PR do?

This PR adds the possibility to reset the DateInput field on state change.

Where should the reviewer start?

From DateInput.js

What testing has been done on this PR?

I have added 2 unit tests for the new functionality.

I added 1 story called ResetDate.js and tested the functionality there.

How should this be manually tested?

After reviewing DateInput.js and ResetDate.js the functionality should be tested by opening the ResetDate.js story.

Any background context you want to provide?

What are the relevant issues?

The issue is that on state change, there is no possibility to clear the DateInput field.

Here is a codesandbox showcasing the issue that the DateInput field can't be cleared.
https://codesandbox.io/s/recursing-star-3tnfk

Screenshots (if appropriate)

Do the grommet docs need to be updated?

I don't think so.

Should this PR be mentioned in the release notes?

Yes, absolutely.

Is this change backwards compatible or is it a breaking change?

From my knowledge, I think it's backwards compatible.

@AlbertoTonegari AlbertoTonegari changed the title Reset date Reset DateInput field value after state change. Aug 9, 2021
Copy link
Member

@ericsoderberghp ericsoderberghp left a comment

Choose a reason for hiding this comment

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

Thanks for finding and including a fix. One other thing, can you file an issue in GitHub for this as well? Should take but a moment https://github.com/grommet/grommet/issues/new

src/js/components/DateInput/DateInput.js Outdated Show resolved Hide resolved
@@ -74,8 +74,12 @@ const DateInput = forwardRef(
useEffect(() => {
if (
schema &&
value &&
((Array.isArray(value) && value[0]) || !Array.isArray(value))
(value || value?.length === 0) &&
Copy link
Member

Choose a reason for hiding this comment

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

Could this be simplified to value !== undefined?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the review.
I have simplified that expression to the following:

![undefined, null].includes(value) &&
           (Array.isArray(value) || !Array.isArray(value))

What do you think @ericsoderberghp ?

(
(Array.isArray(value) && value[0])
|| !Array.isArray(value)
|| value?.length === 0
Copy link
Member

Choose a reason for hiding this comment

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

I think was checked on line 77 already. I suspect you mean value[0]?.length === 0? In which case, I think line 79 could be: (Array.isArray(value && value[0] !== undefined) || !Array.isArray(value)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hello, thank you for the review.
Unfortunately, this will not work because I want to reset the date when I pass an [ ] as a value. And in this case value[0] !== undefined will be false.

@ericsoderberghp ericsoderberghp linked an issue Aug 11, 2021 that may be closed by this pull request
value &&
((Array.isArray(value) && value[0]) || !Array.isArray(value))
![undefined, null].includes(value) &&
(Array.isArray(value) || !Array.isArray(value))
Copy link
Member

Choose a reason for hiding this comment

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

Won't this logic always return true, since the value is either an Array or not an Array?

I'm thinking undefined !== value might be enough. That would align with how undefined is what determines whether an input component is controlled or not. In other words, I don't think we need to also allow for null.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are absolutely right. 😅
I have modified it.

@ericsoderberghp ericsoderberghp merged commit cb6cce4 into grommet:master Aug 11, 2021
@ericsoderberghp
Copy link
Member

Thanks for your contribution!

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

Successfully merging this pull request may close these issues.

Can't reset date for DateInput
2 participants