@@ -180,10 +180,6 @@ export class MatDatepickerContent<S, D = ExtractDateTypeFromSelection<S>>
180
180
}
181
181
182
182
ngOnInit ( ) {
183
- // If we have actions, clone the model so that we have the ability to cancel the selection,
184
- // otherwise update the global model directly. Note that we want to assign this as soon as
185
- // possible, but `_actionsPortal` isn't available in the constructor so we do it in `ngOnInit`.
186
- this . _model = this . _actionsPortal ? this . _globalModel . clone ( ) : this . _globalModel ;
187
183
this . _animationState = this . datepicker . touchUi ? 'enter-dialog' : 'enter-dropdown' ;
188
184
}
189
185
@@ -246,6 +242,25 @@ export class MatDatepickerContent<S, D = ExtractDateTypeFromSelection<S>>
246
242
this . _globalModel . updateSelection ( this . _model . selection , this ) ;
247
243
}
248
244
}
245
+
246
+ /**
247
+ * Assigns a new portal containing the datepicker actions.
248
+ * @param portal Portal with the actions to be assigned.
249
+ * @param forceRerender Whether a re-render of the portal should be triggered. This isn't
250
+ * necessary if the portal is assigned during initialization, but it may be required if it's
251
+ * added at a later point.
252
+ */
253
+ _assignActions ( portal : TemplatePortal < any > | null , forceRerender : boolean ) {
254
+ // If we have actions, clone the model so that we have the ability to cancel the selection,
255
+ // otherwise update the global model directly. Note that we want to assign this as soon as
256
+ // possible, but `_actionsPortal` isn't available in the constructor so we do it in `ngOnInit`.
257
+ this . _model = portal ? this . _globalModel . clone ( ) : this . _globalModel ;
258
+ this . _actionsPortal = portal ;
259
+
260
+ if ( forceRerender ) {
261
+ this . _changeDetectorRef . detectChanges ( ) ;
262
+ }
263
+ }
249
264
}
250
265
251
266
/** Form control that can be associated with a datepicker. */
@@ -556,6 +571,7 @@ export abstract class MatDatepickerBase<
556
571
throw Error ( 'A MatDatepicker can only be associated with a single actions row.' ) ;
557
572
}
558
573
this . _actionsPortal = portal ;
574
+ this . _componentRef ?. instance . _assignActions ( portal , true ) ;
559
575
}
560
576
561
577
/**
@@ -565,6 +581,7 @@ export abstract class MatDatepickerBase<
565
581
removeActions ( portal : TemplatePortal ) : void {
566
582
if ( portal === this . _actionsPortal ) {
567
583
this . _actionsPortal = null ;
584
+ this . _componentRef ?. instance . _assignActions ( null , true ) ;
568
585
}
569
586
}
570
587
@@ -632,8 +649,8 @@ export abstract class MatDatepickerBase<
632
649
protected _forwardContentValues ( instance : MatDatepickerContent < S , D > ) {
633
650
instance . datepicker = this ;
634
651
instance . color = this . color ;
635
- instance . _actionsPortal = this . _actionsPortal ;
636
652
instance . _dialogLabelId = this . datepickerInput . getOverlayLabelId ( ) ;
653
+ instance . _assignActions ( this . _actionsPortal , false ) ;
637
654
}
638
655
639
656
/** Opens the overlay with the calendar. */
0 commit comments