marmelab / ng-admin

Add an AngularJS admin GUI to any RESTful API
http://ng-admin-book.marmelab.com/
MIT License
3.95k stars 725 forks source link

Date field cannot be typed in #1305

Closed Joe-Edwards closed 7 years ago

Joe-Edwards commented 7 years ago

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 https://github.com/marmelab/ng-admin/issues/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:

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


This is related to https://github.com/marmelab/ng-admin/issues/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 commented 7 years ago

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 https://github.com/marmelab/ng-admin/issues/899, so https://github.com/marmelab/ng-admin/pull/1196 should be reverted once.

aikizoku commented 7 years ago

Please fix this. (T_T)

matheo commented 7 years ago

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 commented 7 years ago

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

matheo commented 7 years ago

Done ;) https://github.com/marmelab/ng-admin/pull/1317

Kmaschta commented 7 years ago

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

It will be published soon!

Kmaschta commented 7 years ago

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 !

jamespsterling commented 7 years ago

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

ng-admin-datepicker-strangeness