Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Default to previous value or 0 when invalid decimal separator (#8904)

Aleix Suau68959740952f
diff --git a/frontend/src/app/modules/fields/edit/field-types/duration-edit-field.component.ts b/frontend/src/app/modules/fields/edit/field-types/duration-edit-field.component.ts
index d6899bcc35d..4d6bb61da28 100644
--- a/frontend/src/app/modules/fields/edit/field-types/duration-edit-field.component.ts
+++ b/frontend/src/app/modules/fields/edit/field-types/duration-edit-field.component.ts
@@ -37,9 +37,10 @@ import {InjectField} from "core-app/helpers/angular/inject-field.decorator";
<input type="number"
step="0.01"
class="inline-edit--field"
+ #input
[attr.aria-required]="required"
[ngModel]="formatter(value)"
- (ngModelChange)="value = parser($event)"
+ (ngModelChange)="value = parser($event, input)"
[attr.required]="required"
(keydown)="handler.handleUserKeydown($event)"
[disabled]="inFlight"
@@ -49,13 +50,24 @@ import {InjectField} from "core-app/helpers/angular/inject-field.decorator";
export class DurationEditFieldComponent extends EditFieldComponent {
@InjectField() TimezoneService:TimezoneService;
- public parser(value:any) {
- if (!isNaN(value)) {
- let floatValue = parseFloat(value);
- return moment.duration(floatValue, 'hours');
+ public parser(value:any, input:any) {
+ // Managing decimal separators in a multi-language app is a complex topic:
+ // https://www.ctrl.blog/entry/html5-input-number-localization.html
+ // Depending on the locale of the OS, the browser or the app itself,
+ // a decimal separator could be considered valid or invalid.
+ // When a decimal operator is considered invalid (e.g: 1. in Chrome with
+ // 'en' locale), the input emits null as a value and its state is marked
+ // not valid, but the value remains in the input. Adding a value after the
+ // 'invalid' separator (e.g: 1.2) emits a valid value.
+ // In order to allow both decimal separator (period and comma) in any
+ // context, we check the validity of the input and, if it's not valid, we
+ // default to the previous value, emulating the way the browsers work with
+ // valid separators (e.g: introducing 1. would set 1 as a value).
+ if (value == null && !input.validity.valid) {
+ value = this.value || 0;
}
- return value;
+ return moment.duration(value, 'hours');
}
public formatter(value:any) {
@@ -64,7 +76,7 @@ export class DurationEditFieldComponent extends EditFieldComponent {
protected parseValue(val:moment.Moment | null) {
if (val === null) {
- return val
+ return val;
}
let parsedValue;