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

Feat add container to modal options #6571

Open
wants to merge 4 commits into
base: development
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
5 changes: 5 additions & 0 deletions apps/ngx-bootstrap-docs/src/ng-api-doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2746,6 +2746,11 @@ export const ngdoc: any = {
"type": "string",
"description": "<p>Css class for opened modal</p>\n"
},
{
"name": "container",
"type": "string | ElementRef",
"description": "<p>The CSS Selector or ElementRef for where the modal window will be injected into the page.</p>\n"
},
{
"name": "closeInterceptor",
"type": "CloseInterceptorFn",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ <h3 *ngIf="item.title" [attr.id]="item.anchor" class="d-flex justify-content-bet
</p>
</h3>
<p *ngIf="item.description" [innerHTML]="item.description"></p>
<ng-sample-box [ts]="item?.component?.default" [html]="item?.html?.default" [style]="item.style ">
<ng-sample-box [ts]="item?.component?.default" [html]="item?.html?.default" [style]="item.style?.default">
<ng-container *ngComponentOutlet="item.outlet"></ng-container>
</ng-sample-box>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface ComponentExample {
description?: string;
component?: SourceCodeModel;
html?: SourceCodeModel;
style?: string;
style?: SourceCodeModel;
css?: string;
outlet?: any; // ToDo: Component<T>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.profit {
color: green;
font-weight: 700;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ export class DemoModalServiceFromComponent {

openModalWithComponent() {
const initialState: ModalOptions = {
container: 'bs-demo',
initialState: {
list: [
'Open a modal with component',
'Pass your data',
'Do something else',
'...'
'...',
'Push button to find out!'
],
title: 'Modal with component'
}
Expand All @@ -32,6 +34,7 @@ export class DemoModalServiceFromComponent {
@Component({
// eslint-disable-next-line @angular-eslint/component-selector
selector: 'modal-content',
styleUrls: ['./service-component.css'],
template: `
<div class="modal-header">
<h4 class="modal-title pull-left">{{title}}</h4>
Expand All @@ -41,23 +44,26 @@ export class DemoModalServiceFromComponent {
</div>
<div class="modal-body">
<ul *ngIf="list.length">
<li *ngFor="let item of list">{{item}}</li>
<li *ngFor="let item of list" [ngClass]="{ 'profit' : item === 'PROFIT!!!'}">{{item}}</li>
</ul>
</div>
<div class="modal-footer">
<button *ngIf="!profitBtnPressed" type="button" class="btn btn-default" (click)="showProfit()">Find Out</button>
<button type="button" class="btn btn-default" (click)="bsModalRef.hide()">{{closeBtnName}}</button>
</div>
`
})

export class ModalContentComponent implements OnInit {
export class ModalContentComponent {
title?: string;
profitBtnPressed: boolean = false;
closeBtnName?: string;
list: any[] = [];

constructor(public bsModalRef: BsModalRef) {}

ngOnInit() {
this.list.push('PROFIT!!!');
showProfit() {
this.profitBtnPressed = true;
this.list[this.list.length - 1] = 'PROFIT!!!';
}
}
1 change: 1 addition & 0 deletions libs/doc-pages/modal/src/lib/modal-section.list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const demoComponentContent: ContentSection[] = [
anchor: 'service-component',
component: require('!!raw-loader!./demos/service-component/service-component.ts'),
html: require('!!raw-loader!./demos/service-component/service-component.html'),
style: require('!!raw-loader!./demos/service-component/service-component.css'),
description: `<p>Creating a modal with component just as easy as it is with template. Just pass your component
in <code>.show()</code> method as in example, and don't forget to include your component to
<code>entryComponents</code> of your <code>NgModule</code><br> If you passed a component
Expand Down
4 changes: 2 additions & 2 deletions src/modal/bs-modal.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class BsModalService {
if (isBackdropEnabled && isBackdropInDOM) {
this._backdropLoader
.attach(ModalBackdropComponent)
.to('body')
.to(this.config.container)
.show({ isAnimated: this.config.animated });
this.backdropRef = this._backdropLoader._componentRef;
}
Expand Down Expand Up @@ -140,7 +140,7 @@ export class BsModalService {
.provide({ provide: ModalOptions, useValue: this.config })
.provide({ provide: BsModalRef, useValue: bsModalRef })
.attach(ModalContainerComponent)
.to('body');
.to(this.config.container);
bsModalRef.hide = () => this.hide(bsModalRef.id);
bsModalRef.setClass = (newClass: string) => {
if (modalContainerRef.instance) {
Expand Down
7 changes: 6 additions & 1 deletion src/modal/modal-options.class.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, StaticProvider, InjectionToken } from '@angular/core';
import { ElementRef, Injectable, StaticProvider, InjectionToken } from '@angular/core';
import { ClassName, CloseInterceptorFn, DismissReasons, Selector, TransitionDurations } from './models';

@Injectable({providedIn: 'platform'})
Expand Down Expand Up @@ -30,6 +30,10 @@ export class ModalOptions<T = Record<string, unknown>> {
* Css class for opened modal
*/
class?: string;
/**
* CSS selector or ElementRef to which the modal is appended
*/
container?: string | ElementRef;
/**
* Toggle animation
*/
Expand Down Expand Up @@ -63,6 +67,7 @@ export const modalConfigDefaults: ModalOptions = {
show: false,
ignoreBackdropClick: false,
class: '',
container: 'body',
animated: true,
initialState: {},
closeInterceptor: void 0
Expand Down
2 changes: 1 addition & 1 deletion src/modal/modal.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ export class ModalDirective implements OnDestroy, OnInit {
this.removeBackdrop();
this._backdrop
.attach(ModalBackdropComponent)
.to('body')
.to(this._config.container)
.show({ isAnimated: this._config.animated });
this.backdrop = this._backdrop._componentRef;

Expand Down
20 changes: 19 additions & 1 deletion src/modal/testing/modal.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { pairwise, tap } from 'rxjs/operators';

import { BsModalService, ModalModule } from '../index';

@Component({ template: '<div>Dummy Component</div>' })
@Component({
selector: 'dummy-component',
template: '<div>Dummy Component</div>' })
class DummyComponent {
// eslint-disable-next-line @typescript-eslint/no-empty-function,@typescript-eslint/no-unused-vars
constructor(modalService: BsModalService) { }
Expand Down Expand Up @@ -130,4 +132,20 @@ describe('Modal service', () => {
jest.runAllTimers();
expect(onHiddenSpy).toHaveBeenCalledWith({ id });
});

it('should render in <body> element by default', done => {
modalService.onShown.subscribe((data) => {
expect(document.querySelector('dummy-component modal-container')).toBeDefined();
done();
})
modalService.show(TestModalComponent);
})

it('should render in the container selector provided', done => {
modalService.onShown.subscribe((data) => {
expect(document.querySelector('dummy-component modal-container')).toBeDefined();
done();
})
modalService.show(TestModalComponent, { container: 'dummy-component'});
})
});