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

Date field cannot be typed in #1305

Closed
Joe-Edwards opened this issue Feb 8, 2017 · 8 comments · Fixed by #1334
Closed

Date field cannot be typed in #1305

Joe-Edwards opened this issue Feb 8, 2017 · 8 comments · Fixed by #1334

Comments

@Joe-Edwards
Copy link

Joe-Edwards commented Feb 8, 2017

If you have a 'date' field, you can no longer type the date in it explicitly. Attempting to modify an existing date also just deletes it.

You can see the behaviour in the ng-admin demo - https://marmelab.com/ng-admin-demo/index.html#/customers/edit/485 - try changing the birthday without opening the picker.

I think this was broken by #899 - it used to work in an older version of ng-admin.

The problem is because maDateField watches both value and rawValue:

            scope.$watch('rawValue', function(newValue) {
                scope.value = field.parse()(newValue);
            });

            scope.$watch('value', (newValue, oldValue) => {
                if (newValue === oldValue) {
                    return;
                }

                if (!newValue) {
                    scope.rawValue = null;
                    return;
                }

                scope.rawValue = scope.value instanceof Date ? scope.value : new Date(scope.value);
            });

The problem is, if at any point an invalid date string is in the box - suppose the string 'foo'. The following sequence happens:

  • $watch('rawValue' sees foo. This is not a date object, so parse leaves it unchanged, and value is set to foo.
  • $watch('value' sees foo and attempts to construct a new Date object using it. This ends up setting rawValue to an Invalid Date object.
  • $watch('rawValue' sees Invalid Date. This is a date object, so parse tries to manipulate it, but fails and ends up setting the value to null.
  • $watch('value' sees null and correspondingly sets rawValue to null
  • $watch('rawValue' sees null and correspondingly sets value to null
  • $watch('value' sees null, this is the same as the last value, and the cycle ends!

The net result is null - which removes the text you tried to write from the box.


This is related to #1271 which will be caused by a similar loop. However in that case, if you are in a timezone, the value/rawValue watches will loop, and because the 'parse' method applies a timezone correction the value never stabilises (resulting in the infinite loop).

@mosasiru
Copy link

mosasiru commented Feb 13, 2017

In many use cases, operators want to manage hour and minutes, but it is not enabled now by the degration.
These cases are more popular than #899, so #1196 should be reverted once.

@aikizoku
Copy link

aikizoku commented Feb 23, 2017

Please fix this. (T_T)

@matheo
Copy link
Contributor

matheo commented Mar 3, 2017

I patched my DateField to be able to work with the date selector fixing the timezone diff:

scope.rawValue = scope.value instanceof Date ? scope.value : new Date(new Date(scope.value).getTime() + (new Date().getTimezoneOffset() * 60 * 1000));

https://github.com/marmelab/ng-admin/blob/master/src/javascripts/ng-admin/Crud/field/maDateField.js#L32

@Phocea
Copy link
Contributor

Phocea commented Mar 7, 2017

@matheo, if your patch fix both issues, can you create a PR for it please ?

@matheo
Copy link
Contributor

matheo commented Mar 8, 2017

Done ;) #1317

@Kmaschta
Copy link
Contributor

Kmaschta commented Mar 8, 2017

Yup, the infinite loop is now fixed with my PR #1318.

It will be published soon!

@Kmaschta
Copy link
Contributor

I misunderstood the issue, it's about manually edit the date field.
The relation with the PR #1317 was confusing.

I re-open the issue, sorry for the inconvenience !

@Kmaschta Kmaschta reopened this Mar 16, 2017
@Kmaschta Kmaschta removed their assignment Mar 16, 2017
@jamespsterling
Copy link

Also when you use the datepicker and then click into another field, the date changes 'magically'.

ng-admin-datepicker-strangeness

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 a pull request may close this issue.

7 participants