Skip to content

Commit 35573f1

Browse files
committed
feat(datepicker): add opened input binding
* Adds support for the `opened` input binding. Closes #8094
1 parent 8dfe470 commit 35573f1

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/lib/datepicker/datepicker.spec.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,20 @@ describe('MatDatepicker', () => {
113113
.not.toBeNull();
114114
});
115115

116+
it('should open datepicker if opened input is set to true', async(() => {
117+
testComponent.opened = true;
118+
fixture.detectChanges();
119+
120+
expect(document.querySelector('.mat-datepicker-content')).not.toBeNull();
121+
122+
testComponent.opened = false;
123+
fixture.detectChanges();
124+
125+
fixture.whenStable().then(() => {
126+
expect(document.querySelector('.mat-datepicker-content')).toBeNull();
127+
});
128+
}));
129+
116130
it('open in disabled mode should not open the calendar', () => {
117131
testComponent.disabled = true;
118132
fixture.detectChanges();
@@ -1096,10 +1110,11 @@ describe('MatDatepicker', () => {
10961110
@Component({
10971111
template: `
10981112
<input [matDatepicker]="d" [value]="date">
1099-
<mat-datepicker #d [touchUi]="touch" [disabled]="disabled"></mat-datepicker>
1113+
<mat-datepicker #d [touchUi]="touch" [disabled]="disabled" [opened]="opened"></mat-datepicker>
11001114
`,
11011115
})
11021116
class StandardDatepicker {
1117+
opened = false;
11031118
touch = false;
11041119
disabled = false;
11051120
date: Date | null = new Date(2020, JAN, 1);

src/lib/datepicker/datepicker.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,18 @@ export class MatDatepicker<D> implements OnDestroy {
186186
@Output('closed') closedStream: EventEmitter<void> = new EventEmitter<void>();
187187

188188
/** Whether the calendar is open. */
189-
opened = false;
189+
@Input()
190+
get opened(): boolean { return this._opened; }
191+
set opened(value: boolean) {
192+
const shouldOpen = coerceBooleanProperty(value);
193+
194+
if (shouldOpen) {
195+
this.open();
196+
} else {
197+
this.close();
198+
}
199+
}
200+
private _opened = false;
190201

191202
/** The id for the datepicker calendar. */
192203
id = `mat-datepicker-${datepickerUid++}`;
@@ -277,7 +288,7 @@ export class MatDatepicker<D> implements OnDestroy {
277288

278289
/** Open the calendar. */
279290
open(): void {
280-
if (this.opened || this.disabled) {
291+
if (this._opened || this.disabled) {
281292
return;
282293
}
283294
if (!this._datepickerInput) {
@@ -288,13 +299,13 @@ export class MatDatepicker<D> implements OnDestroy {
288299
}
289300

290301
this.touchUi ? this._openAsDialog() : this._openAsPopup();
291-
this.opened = true;
302+
this._opened = true;
292303
this.openedStream.emit();
293304
}
294305

295306
/** Close the calendar. */
296307
close(): void {
297-
if (!this.opened) {
308+
if (!this._opened) {
298309
return;
299310
}
300311
if (this._popupRef && this._popupRef.hasAttached()) {
@@ -314,7 +325,7 @@ export class MatDatepicker<D> implements OnDestroy {
314325
this._focusedElementBeforeOpen = null;
315326
}
316327

317-
this.opened = false;
328+
this._opened = false;
318329
this.closedStream.emit();
319330
}
320331

0 commit comments

Comments
 (0)