Skip to content

Commit

Permalink
fix(core/forms): prints date even when formcontrol changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sara-gnucoop authored and robzan8 committed Sep 15, 2023
1 parent e211733 commit 1a61104
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
8 changes: 7 additions & 1 deletion projects/core/forms/src/read-only-date-field.html
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
<span *ngIf="date|async as dateVal">{{dateVal}}</span>
<ng-container *ngIf="control|async as ctrl">

<span *ngIf="date|async as dateVal; else dateCtrl">{{dateVal}}</span>
<ng-template #dateCtrl>
{{formatDateField(ctrl.value)}}
</ng-template>
</ng-container>
34 changes: 18 additions & 16 deletions projects/core/forms/src/read-only-date-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,30 @@ export class AjfReadOnlyDateFieldComponent extends CoreComponent {
filter(control => control != null),
map(ctrl => {
if (ctrl) {
const val = ctrl.value;
if (val == null) {
return '';
}

let dt = null;
if (typeof val === 'string') {
dt = parse(val, 'yyyy-MM-dd', new Date());
} else {
dt = toDate(val);
}
if (!isNaN(dt.valueOf())) {
const datePipe = new DatePipe(this._getCurrentLocale());
return datePipe.transform(dt, 'shortDate') as string;
}
return '';
this.formatDateField(ctrl.value);
}
return '';
}),
);
}

formatDateField(val: any): string {
if (val == null) {
return '';
}
let dt = null;
if (typeof val === 'string') {
dt = parse(val, 'yyyy-MM-dd', new Date());
} else {
dt = toDate(val);
}
if (!isNaN(dt.valueOf())) {
const datePipe = new DatePipe(this._getCurrentLocale());
return datePipe.transform(dt, 'shortDate') as string;
}
return '';
}

private _getCurrentLocale(): string {
const lang = this._ts.getActiveLang();
switch (lang) {
Expand Down

0 comments on commit 1a61104

Please sign in to comment.