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

Pass through original values for invalid dates #1949

Merged
merged 4 commits into from
Jun 26, 2022
Merged

Pass through original values for invalid dates #1949

merged 4 commits into from
Jun 26, 2022

Conversation

sunnysingh
Copy link
Contributor

This is a follow-up to the following community discussion: Showing errors for badly formatted date and time fields.

The current behavior attempts to format date input values with the dayjs library, and if the formatted string returns Invalid Date then it will call handleChange with an undefined value. This not only causes dates to not show up in the final data object, but it also makes it impossible to show validation errors for optional fields.

This PR introduces a bugfix to pass through the original keyboardInputValue, which triggers validation:

Screen Shot 2022-06-02 at 11 43 03 AM

This is my first contribution so please let me know if I'm missing anything.

@sunnysingh sunnysingh marked this pull request as ready for review June 2, 2022 15:47
@CLAassistant
Copy link

CLAassistant commented Jun 2, 2022

CLA assistant check
All committers have signed the CLA.

@sdirix
Copy link
Member

sdirix commented Jun 3, 2022

Thank you very much. We're gonna take a look very soon ❤️

@coveralls
Copy link

coveralls commented Jun 3, 2022

Coverage Status

Coverage decreased (-0.2%) to 84.098% when pulling 3e01b86 on sunnysingh:bugfix/material-invalid-date-onchange into 83e7003 on eclipsesource:master.

Copy link
Member

@sdirix sdirix left a comment

Choose a reason for hiding this comment

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

The PR improves the situation but there are still problems. It seems that dayjs is very very lenient in regards of format and only reports Invalid Date when it can't match at all.

For example aaabbb1sad3asdasd00 in the time picker is parsed to 01:03:00 which might be unexpected for the user.

I would like to suggest to add an onBlur handler to the date, time and date-time renderers which makes sure that once the user leaves the input, the text-input is reset to reflect the actual data.

Do you agree? Can you add this?

packages/material/src/util/datejs.ts Outdated Show resolved Hide resolved
packages/material/src/util/datejs.ts Outdated Show resolved Hide resolved
@sunnysingh
Copy link
Contributor Author

The PR improves the situation but there are still problems. It seems that dayjs is very very lenient in regards of format and only reports Invalid Date when it can't match at all.

For example aaabbb1sad3asdasd00 in the time picker is parsed to 01:03:00 which might be unexpected for the user.

I would like to suggest to add an onBlur handler to the date, time and date-time renderers which makes sure that once the user leaves the input, the text-input is reset to reflect the actual data.

Do you agree? Can you add this?

Thank you for reviewing. I noticed this issue too and wasn't sure how to tackle it, but I like the "set value to parsed data on blur" idea, will add this in.

@sunnysingh
Copy link
Contributor Author

@sdirix I got a chance to try and implement the "sync input values with parsed data on blur" improvement. However, it doesn't seem quite right especially with the failing unit tests (some of the errors are expected, but others seem to provide very different actual values from the expected values). When you get a chance, do you mind taking a look at this?

Copy link
Member

@sdirix sdirix left a comment

Choose a reason for hiding this comment

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

There are some issues with the current approach as for example the pickers no longer update the text input.

data: any;
onBlur: FormEventHandler<HTMLInputElement | HTMLTextAreaElement> | undefined;
}) => {
const [value, setValue] = useState(props.data);
Copy link
Member

Choose a reason for hiding this comment

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

This does not take into account cases where the data is changed differently, for example it will not update when data is changed via the pickers themselves or when the user hands in new data when externally updated.

Comment on lines 52 to 58
const onBlur = useMemo(
() => (event: DateInputFormEvent) => {
setValue(props.data);
if (props.onBlur) props.onBlur(event);
},
[props.data, props.onBlur]
);
Copy link
Member

Choose a reason for hiding this comment

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

We can use onBlur directly however it's already used for focused. So instead of injecting ourselves into onBlur we can just check the value of focused.

Comment on lines 60 to 65
const createOnChangeHandler = (
onChange: (event: DateInputFormEvent) => void
) => (event: DateInputFormEvent) => {
setValue((event.target as HTMLInputElement | HTMLTextAreaElement).value);
if (onChange) onChange(event);
};
Copy link
Member

Choose a reason for hiding this comment

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

This way we completely bypass the current change semantics. It can certainly work but we need to keep all edge cases in mind. It's probably easier to "simply" transform the value handed in by the picker by ignoring it when we're not focused. This also makes it overall simpler as we don't manage another explicit state.

@sdirix
Copy link
Member

sdirix commented Jun 21, 2022

@sunnysingh I had a look and fixed the issues I found during review. Please test on your side whether it behaves the way you want to see.

@LukasBoll
Copy link
Contributor

Hi,
I also reviewed the pull request. Code looks good to me and everything behaves like expected! 👍

Copy link
Member

@sdirix sdirix left a comment

Choose a reason for hiding this comment

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

LGTM

@sdirix sdirix merged commit d2bf053 into eclipsesource:master Jun 26, 2022
@sdirix
Copy link
Member

sdirix commented Jun 26, 2022

@sunnysingh I merged this now in. Please let me know if there is something which could be improved from your point of view.

@sdirix sdirix added this to the 3.0 milestone Jun 26, 2022
@sunnysingh
Copy link
Contributor Author

Thank you for fixing and merging @sdirix. The ResettableTextField component looks good to me as well, and I agree that it's more straightforward to use the focused state from useFocus.

Also my apologies for not looking sooner, I had a few other things come up last week.

@sunnysingh sunnysingh deleted the bugfix/material-invalid-date-onchange branch June 27, 2022 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants