Date field cannot be typed in
See original GitHub issueIf 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:
$watch('rawValue'
seesfoo
. This is not a date object, soparse
leaves it unchanged, andvalue
is set tofoo
.$watch('value'
seesfoo
and attempts to construct a new Date object using it. This ends up settingrawValue
to anInvalid Date
object.$watch('rawValue'
seesInvalid Date
. This is a date object, soparse
tries to manipulate it, but fails and ends up setting the value tonull
.$watch('value'
seesnull
and correspondingly setsrawValue
tonull
$watch('rawValue'
seesnull
and correspondingly setsvalue
tonull
$watch('value'
seesnull
, 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 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).
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:8 (5 by maintainers)
@matheo, if your patch fix both issues, can you create a PR for it please ?
Also when you use the datepicker and then click into another field, the date changes ‘magically’.