Skip to content

Commit

Permalink
fix(primeng/calendar): yearpicker wont show value with reactive forms
Browse files Browse the repository at this point in the history
  • Loading branch information
volvachev authored and Egor Volvachev committed Jun 1, 2022
1 parent 8cc17f7 commit 91e4c32
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
22 changes: 15 additions & 7 deletions src/app/components/calendar/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export interface LocaleSettings {
weekHeader?: string;
}

export type CalendarTypeView = 'date' | 'month' | 'year';

@Component({
selector: 'p-calendar',
template: `
Expand Down Expand Up @@ -466,15 +468,15 @@ export class Calendar implements OnInit,OnDestroy,ControlValueAccessor {

_firstDayOfWeek: number;

_view: string = 'date';
_view: CalendarTypeView = 'date';

preventFocus: boolean;

@Input() get view(): string {
@Input() get view(): CalendarTypeView {
return this._view;
};

set view(view: string) {
set view(view: CalendarTypeView) {
this._view = view;
this.currentView = this._view;
}
Expand Down Expand Up @@ -1063,7 +1065,7 @@ export class Calendar implements OnInit,OnDestroy,ControlValueAccessor {
}
}

setCurrentView(currentView: string) {
setCurrentView(currentView: CalendarTypeView) {
this.currentView = currentView;
this.cd.detectChanges();
this.alignOverlay();
Expand Down Expand Up @@ -2857,10 +2859,16 @@ export class Calendar implements OnInit,OnDestroy,ControlValueAccessor {
} while (true);
}

if (this.view === 'year') {
month = month === - 1 ? 1 : month;
day = day === - 1 ? 1 : day;
}

date = this.daylightSavingAdjust(new Date(year, month - 1, day));
if (date.getFullYear() !== year || date.getMonth() + 1 !== month || date.getDate() !== day) {
throw "Invalid date"; // E.g. 31/02/00
}

if (date.getFullYear() !== year || date.getMonth() + 1 !== month || date.getDate() !== day) {
throw "Invalid date"; // E.g. 31/02/00
}

return date;
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/showcase/components/calendar/calendardemo.html
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ <h5>Properties</h5>
</tr>
<tr>
<td>view</td>
<td>string</td>
<td>'date' | 'month' | 'year'</td>
<td>date</td>
<td>Type of view to display, valid values are "date" for datepicker and "month" for month picker.</td>
</tr>
Expand Down

0 comments on commit 91e4c32

Please sign in to comment.