Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add readonly option on input and allow component to be used in a reactive from with an empty date #292

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/ng-datepicker/ng-datepicker.component.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="ngx-datepicker-container">
<input type="text" *ngIf="!headless" class="ngx-datepicker-input" [(ngModel)]="displayValue" readonly (click)="toggle()">
<input type="text" *ngIf="!headless" class="ngx-datepicker-input" [(ngModel)]="displayValue" [readonly]="readonly" (click)="toggle()">
<ng-content></ng-content>
<div class="ngx-datepicker-calendar-container ngx-datepicker-position-{{position}}" *ngIf="isOpened">
<div class="topbar-container">
Expand Down
9 changes: 6 additions & 3 deletions src/ng-datepicker/ng-datepicker.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface DatepickerOptions {
barTitleFormat?: string; // default: 'MMMM YYYY'
firstCalendarDay?: number; // 0 = Sunday (default), 1 = Monday, ..
locale?: object;
readonly?: boolean;
minDate?: Date;
maxDate?: Date;
}
Expand Down Expand Up @@ -69,6 +70,7 @@ export class NgDatepickerComponent implements ControlValueAccessor, OnInit, OnCh
private positions = ['bottom-left', 'bottom-right', 'top-left', 'top-right'];

innerValue: Date;
readonly: boolean;
displayValue: string;
displayFormat: string;
date: Date;
Expand Down Expand Up @@ -148,6 +150,7 @@ export class NgDatepickerComponent implements ControlValueAccessor, OnInit, OnCh
this.barTitleFormat = this.options && this.options.barTitleFormat || 'MMMM YYYY';
this.firstCalendarDay = this.options && this.options.firstCalendarDay || 0;
this.locale = this.options && { locale: this.options.locale } || {};
this.readonly = this.options && this.options.readonly || false;
}

nextMonth(): void {
Expand Down Expand Up @@ -199,8 +202,8 @@ export class NgDatepickerComponent implements ControlValueAccessor, OnInit, OnCh
}

init(): void {
const start = startOfMonth(this.date);
const end = endOfMonth(this.date);
const start = startOfMonth(this.date || new Date(Date.now()));
const end = endOfMonth(this.date || new Date(Date.now()));

this.days = eachDay(start, end).map(date => {
return {
Expand Down Expand Up @@ -266,7 +269,7 @@ export class NgDatepickerComponent implements ControlValueAccessor, OnInit, OnCh
this.date = val;
this.innerValue = val;
this.init();
this.displayValue = format(this.innerValue, this.displayFormat, this.locale);
this.displayValue = this.innerValue ? format(this.innerValue, this.displayFormat, this.locale) : '';
this.barTitle = format(startOfMonth(val), this.barTitleFormat, this.locale);
}
}
Expand Down