Skip to content

Commit 9cbcb5f

Browse files
authored
Merge pull request #23915 from abpframework/10.0-sweetalert-prompt
Add 'prompt' functionality to `abp.message` object
2 parents 97cd7b1 + 47ae17d commit 9cbcb5f

File tree

4 files changed

+100
-2
lines changed

4 files changed

+100
-2
lines changed

docs/en/framework/ui/mvc-razor-pages/javascript-api/message.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,58 @@ abp.message.confirm(
8888
});
8989
````
9090

91+
## Prompt Message
92+
93+
`abp.message.prompt(...)` can be used to get a text (or other input) from the user.
94+
95+
Simple usage:
96+
```js
97+
abp.message.prompt('Please enter a name:', (value)=> console.log('Entered name:', value));
98+
```
99+
100+
**Example (then):**
101+
102+
````js
103+
let options = { title: "Name Change" ,icon: "info", inputValue: "John Doe"};
104+
abp.message.prompt('Enter a new name to change your name', options)
105+
.then(function(value){
106+
if (value) {
107+
console.log('Entered name:', value);
108+
}
109+
});
110+
````
111+
112+
**Example (await):**
113+
114+
````js
115+
(async function(){
116+
let options = { title: "Email Registration" ,icon: "info", input: "email"};
117+
const email = await abp.message.prompt('Enter your email:', options);
118+
if (email) {
119+
console.log('Entered email:', email);
120+
}
121+
})();
122+
````
123+
124+
![js-message-prompt](../../../../images/js-message-prompt.png)
125+
126+
### The Return Value
127+
128+
The return value of the `abp.message.prompt(...)` function is a promise that resolves to the entered value (`string`) or `null` if the dialog is canceled/dismissed.
129+
130+
### Parameters
131+
132+
`abp.message.prompt(...)` function has the following parameters:
133+
134+
* `message`: A message (`string`) to show to the user.
135+
* `titleOrOptionsOrCallback` (optional):
136+
* a `string` title, or
137+
* an `object` containing SweetAlert options (e.g., `input`, `inputAttributes`, `inputPlaceholder`), or
138+
* a `function` callback that receives the resulting value (or `null`).
139+
* `callback` (optional): If you've passed a title as the second parameter, you can pass your callback function as the 3rd parameter.
140+
141+
Passing a callback function is an alternative to using the returned promise.
142+
91143
## SweetAlert Configuration
92144

93145
The Message API is implemented using the [SweetAlert](https://sweetalert.js.org/) library by default. If you want to change its configuration, you can set the options in the `abp.libs.sweetAlert.config` object. The default configuration object is shown below:
@@ -125,4 +177,3 @@ abp.libs.sweetAlert.config.warn.icon = 'error';
125177
````
126178

127179
See the [SweetAlert document](https://sweetalert.js.org/) for all the configuration options.
128-
6.58 KB
Loading

framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/wwwroot/libs/abp/aspnetcore-mvc-ui-theme-shared/sweetalert2/abp-sweetalert2.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ var abp = abp || {};
2929
title: 'Are you sure?',
3030
showCancelButton: true,
3131
reverseButtons: true
32+
},
33+
prompt: {
34+
icon: 'question',
35+
input: 'text',
36+
showCancelButton: true,
37+
reverseButtons: true
3238
}
3339
}
3440
};
@@ -92,13 +98,43 @@ var abp = abp || {};
9298
);
9399

94100
return $.Deferred(function ($dfd) {
95-
Swal.fire(opts).then(result => {
101+
Swal.fire(opts).then(result => {
96102
callback && callback(result.value);
97103
$dfd.resolve(result.value);
98104
})
99105
});
100106
};
101107

108+
abp.message.prompt = function (message, titleOrOptionsOrCallback, callback) {
109+
110+
var userOpts = {
111+
html: abp.utils.htmlEscape(message).replace(/\n/g, '<br>')
112+
};
113+
114+
if ($.isFunction(titleOrOptionsOrCallback)) {
115+
callback = titleOrOptionsOrCallback;
116+
} else if (typeof titleOrOptionsOrCallback === 'string') {
117+
userOpts.title = titleOrOptionsOrCallback;
118+
} else if ($.isPlainObject(titleOrOptionsOrCallback)) {
119+
userOpts = $.extend(userOpts, titleOrOptionsOrCallback);
120+
}
121+
122+
var opts = $.extend(
123+
{},
124+
abp.libs.sweetAlert.config['default'],
125+
abp.libs.sweetAlert.config.prompt,
126+
userOpts
127+
);
128+
129+
return $.Deferred(function ($dfd) {
130+
Swal.fire(opts).then(function (result) {
131+
var value = result && result.isConfirmed ? result.value : null;
132+
callback && callback(value);
133+
$dfd.resolve(value);
134+
});
135+
});
136+
};
137+
102138
abp.event.on('abp.configurationInitialized', function () {
103139
var l = abp.localization.getResource('AbpUi');
104140

npm/packs/core/src/abp.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,17 @@ var abp = abp || {};
340340
callback && callback(result);
341341
};
342342

343+
abp.message.prompt = function (message, titleOrOptionsOrCallback, callback) {
344+
abp.log.warn('abp.message.prompt is not properly implemented!');
345+
346+
if (titleOrOptionsOrCallback && !(typeof titleOrOptionsOrCallback == 'string')) {
347+
callback = titleOrOptionsOrCallback;
348+
}
349+
350+
var result = prompt(message);
351+
callback && callback(result);
352+
};
353+
343354
/* UI *******************************************************/
344355

345356
abp.ui = abp.ui || {};

0 commit comments

Comments
 (0)