basicModal() {
this.openModal();
}
+
+ customCancelModal() {
+ this.openModal({
+ onCancel: () => (this.returnValue = 'If you have todo something besides hiding the modal.')
+ });
+ }
}
describe('ConfirmationModalComponent', () => {
);
});
});
+
+ describe('custom cancel action', () => {
+ const expectCancelValue = () =>
+ expectReturnValue('If you have todo something besides hiding the modal.');
+
+ beforeEach(() => {
+ mockComponent.customCancelModal();
+ });
+
+ it('should use custom cancel action', () => {
+ fh.clickElement('.tc_backButton');
+ expectCancelValue();
+ });
+
+ it('should use custom cancel action if escape was pressed', () => {
+ hide('esc');
+ expectCancelValue();
+ });
+
+ it('should use custom cancel action if clicked outside the modal', () => {
+ hide('backdrop-click');
+ expectCancelValue();
+ });
+
+ it('should unsubscribe on destroy', () => {
+ hide('backdrop-click');
+ expectCancelValue();
+ const s = 'This value will not be changed.';
+ mockComponent.returnValue = s;
+ component.ngOnDestroy();
+ hide('backdrop-click');
+ expectReturnValue(s);
+ });
+ });
});
-import { Component, OnInit, TemplateRef } from '@angular/core';
+import { Component, OnDestroy, OnInit, TemplateRef } from '@angular/core';
import { FormGroup } from '@angular/forms';
-import { BsModalRef } from 'ngx-bootstrap/modal';
+import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
+import { Subscription } from 'rxjs';
@Component({
selector: 'cd-confirmation-modal',
// Component only
boundCancel = this.cancel.bind(this);
confirmationForm: FormGroup;
+ private onHide: Subscription;
+ private canceled = false;
- constructor(public modalRef: BsModalRef) {
+ constructor(public modalRef: BsModalRef, private modalService: BsModalService) {
this.confirmationForm = new FormGroup({});
+ this.onHide = this.modalService.onHide.subscribe((e) => {
+ if (this.onCancel && (e || this.canceled)) {
+ this.onCancel();
+ }
+ });
}
ngOnInit() {
}
}
+ ngOnDestroy() {
+ this.onHide.unsubscribe();
+ }
+
cancel() {
+ this.canceled = true;
this.modalRef.hide();
- if (this.onCancel) {
- this.onCancel();
- }
}
stopLoadingSpinner() {