Skip to content

Commit cb3ca8e

Browse files
committed
(fix) child action buttons
1 parent 1d44439 commit cb3ca8e

File tree

3 files changed

+70
-36
lines changed

3 files changed

+70
-36
lines changed

README.md

Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Modal dialog uses `ComponentFactoryResolver` to inject the given child component
2828
[ModalDialogService](#modaldialogservice) makes sure that only one instance of a modal dialog is opened at a time.
2929
With [IModalDialogOptions](#imodaldialogoptions) you can define which component will be rendered inside the dialog and configure it based on your needs.
3030

31+
You can further use action buttons to control modal dialog from external component or child component. If action performed on button click is successful, modal dialog will close. Otherwise it will alert user.
32+
3133
## Styles and visuals
3234

3335
`Ngx-modal-dialog` is intended to be used with Bootstrap 4, however you can apply your custom styles from your desired UI framework by providing class names in [IModalDialogSettings](#imodaldialogsettings).
@@ -61,6 +63,26 @@ openNewDialog() {
6163
});
6264
}
6365
```
66+
4. Arbitrary define `actionButtons` in modal dialog options or child component to control modal dialog.
67+
68+
```ts
69+
class MyModalComponent implements IModalDialog {
70+
actionButtons: IModalDialogButton[];
71+
72+
constructor() {
73+
this.actionButtons = [
74+
{ text: 'Close' }, // no special processing here
75+
{ text: 'I will always close', onAction: () => true },
76+
{ text: 'I never close', onAction: () => false }
77+
];
78+
}
79+
80+
dialogInit(reference: ComponentRef<IModalDialog>, options?: IModalDialogOptions) {
81+
// no processing needed
82+
}
83+
}
84+
```
85+
6486
## API
6587

6688
### ModalDialogService
@@ -70,10 +92,19 @@ openNewDialog() {
7092
### IModalDialog
7193
Every component that is used as modal dialog must implement `IModalDialog`.
7294
#### Methods:
73-
- `dialogInit(reference: ComponentRef<IModalDialog>, options?: IModalDialogOptions) => void`: This method is called after initialization of child component. Purpose of the method is to pass necessary information from outer scope to child component.
95+
- `dialogInit(reference: ComponentRef<IModalDialog>, options?: IModalDialogOptions) => void`
96+
Mandatory: `true`
97+
Default: -
98+
This method is called after initialization of child component. Purpose of the method is to pass necessary information from outer scope to child component.
99+
#### Properties:
100+
- `actionButtons`
101+
Mandatory: `false`
102+
Default: -
103+
Type: `string`
104+
Modal heading text
74105

75106
### IModalDialogOptions
76-
#### Interface
107+
#### Interface:
77108
```ts
78109
interface IModalDialogOptions {
79110
title?: string;
@@ -84,66 +115,67 @@ interface IModalDialogOptions {
84115
settings?: IModalDialogSettings;
85116
}
86117
```
87-
#### Properties
88-
##### title
118+
#### Properties:
119+
- `title`
89120
Mandatory: `false`
90121
Default: -
91122
Type: `string`
92123
Modal heading text
93124

94-
##### childComponent
95-
Mandatory: `false`
125+
- `childComponent`
126+
Mandatory: `false`
96127
Default: -
97128
Type: `any`
98129
Component type that will be rendered as a content of modal dialog. Component must implement `IModalDialog` interface.
99130

100-
##### onClose () => Promise<any> | Observable<any> | boolean
101-
Mandatory: `false`
131+
- `onClose()`
132+
Mandatory: `false`
102133
Default: -
103-
Type: `function`
134+
Type: `function`
104135
Input: -
105136
ReturnType: `Promise<any>` or `Observable<any>` or `boolean`
106137
Function to be called on close button click. In case of Promise and Observable, modal dialog will not close unless successful resolve happens. In case of boolean, modal dialog will close only if result is `truthful`.
107138

108-
##### actionButtons
139+
- `actionButtons`
109140
Mandatory: `false`
110141
Default: -
111-
Type: `Array of IModalDialogButton`
142+
Type: `Array<IModalDialogButton>`
112143
Footer action buttons for control of modal dialog. See [IModalDialogButton](#imodaldialogbutton).
144+
Action buttons defined in child component have priority over action buttons defined via options.
113145

114-
##### data
146+
- `data`
115147
Mandatory: `false`
116148
Default: -
117149
Type: -
118150
Arbitrary data that will be passed to child component via `dialogInit` method.
119151

120-
##### settings
152+
- `settings`
121153
Mandatory: `false`
122154
Default: -
123155
Type: `IModalDialogSettings`
124156
Additional settings for granular configuration of modal dialog. See [IModalDialogSettings](#imodaldialogsettings).
125157

126158
### IModalDialogButton
127-
#### Interface
159+
#### Interface:
128160
```ts
129161
interface IModalDialogButton {
130162
text: string;
131163
buttonClass?: string;
132164
onAction?: () => Promise<any> | Observable<any> | boolean;
133165
}
134166
```
135-
#### Properties
136-
##### text
167+
#### Properties:
168+
- `text`
137169
Mandatory: `true`
138170
Default: -
139171
Type: `string`
140172
Caption/text on the button
141-
##### buttonClass
173+
- `buttonClass`
142174
Mandatory: `false`
143175
Default: `btn btn-primary`
144176
Type: `string`
145177
Class name of button
146-
##### onAction
178+
- `onAction()`
147179
Mandatory: `false`
148180
Default: -
149181
Type: `function`
@@ -171,68 +203,68 @@ interface IModalDialogSettings {
171203
}
172204
```
173205

174-
#### Properties
175-
##### overlayClass
206+
#### Properties:
207+
- `overlayClass`
176208
Mandatory: `false`
177209
Default: `modal-backdrop fade show`
178210
Type: `string`
179211
Style of the backdrop overlay layer
180-
##### modalClass
212+
- `modalClass`
181213
Mandatory: `false`
182214
Default: `modal fade show`
183215
Type: `string`
184216
Style of modal wrapper
185-
##### contentClass
217+
- `contentClass`
186218
Mandatory: `false`
187219
Default: `modal-content`
188220
Type: `string`
189221
Modal dialog inner content class
190-
##### headerClass
222+
- `headerClass`
191223
Mandatory: `false`
192224
Default: `modal-header`
193225
Type: `string`
194226
Modal dialog header class
195-
##### headerTitleClass
227+
- `headerTitleClass`
196228
Mandatory: `false`
197229
Default: `modal-title`
198230
Type: `string`
199231
Modal dialog header title class
200-
##### closeButtonClass
232+
- `closeButtonClass`
201233
Mandatory: `false`
202234
Default: `close glyphicon glyphicon-remove`
203235
Type: `string`
204236
Modal dialog header close button class
205-
##### closeButtonTitle
237+
- `closeButtonTitle`
206238
Mandatory: `false`
207239
Default: `CLOSE`
208240
Type: `string`
209241
Close button title
210-
##### bodyClass
242+
- `bodyClass`
211243
Mandatory: `false`
212244
Default: `modal-body`
213245
Type: `string`
214246
Modal dialog body class
215-
##### footerClass
247+
- `footerClass`
216248
Mandatory: `false`
217249
Default: `modal-footer`
218250
Type: `string`
219251
Modal dialog footer class
220-
##### alertClass
252+
- `alertClass`
221253
Mandatory: `false`
222254
Default: `shake`
223255
Type: `string`
224256
Style to be appended to dialog once alert happens
225-
##### alertDuration
257+
- `alertDuration`
226258
Mandatory: `false`
227259
Default: `250`
228260
Type: `number`
229261
Duration of alert animation
230-
##### buttonClass
262+
- `buttonClass`
231263
Mandatory: `false`
232264
Default: `btn btn-primary`
233265
Type: `string`
234266
Style of footer action buttons
235-
##### notifyWithAlert
267+
- `notifyWithAlert`
236268
Mandatory: `false`
237269
Default: `true`
238270
Type: `boolean`

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-modal-dialog",
3-
"version": "0.5.2",
3+
"version": "0.5.3",
44
"description": "Dynamic modal dialog for Angular",
55
"scripts": {
66
"test": "karma start",

src/modal-dialog.component.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ export class ModalDialogComponent implements IModalDialog, OnDestroy {
120120

121121
private _inProgress = false;
122122
private _alertTimeout: number;
123+
private _childInstance: any;
123124

124125
/**
125126
* CTOR
@@ -139,9 +140,9 @@ export class ModalDialogComponent implements IModalDialog, OnDestroy {
139140
if (options && options.childComponent) {
140141
let factory = this.componentFactoryResolver.resolveComponentFactory(options.childComponent);
141142
let componentRef = this.dynamicComponentTarget.createComponent(factory) as ComponentRef<IModalDialog>;
142-
let childInstance = componentRef.instance as IModalDialog;
143-
childInstance['dialogInit'](componentRef, options);
143+
this._childInstance = componentRef.instance as IModalDialog;
144144

145+
this._childInstance['dialogInit'](componentRef, options);
145146
(document.activeElement as HTMLElement).blur();
146147
}
147148
// set options
@@ -203,7 +204,8 @@ export class ModalDialogComponent implements IModalDialog, OnDestroy {
203204
// set references
204205
this.title = (options && options.title) || '';
205206
this.onClose = (options && options.onClose) || null;
206-
this.actionButtons = (options && options.actionButtons) || null;
207+
this.actionButtons = (this._childInstance && this._childInstance['actionButtons']) ||
208+
(options && options.actionButtons) || null;
207209
if (options && options.settings) {
208210
Object.assign(this.settings, options.settings);
209211
}

0 commit comments

Comments
 (0)