|
1 | 1 | ---
|
2 | 2 | title: PromptDialog
|
3 | 3 | apiRef: https://docs.nativescript.org/api-reference/modules/_ui_dialogs_#prompt
|
4 |
| -contributors: [MisterBrownRSA, rigor789] |
| 4 | +contributors: [MisterBrownRSA, rigor789, ikoevska] |
5 | 5 | ---
|
6 | 6 |
|
7 |
| -The PromptDialog allows you to prompt the user for input. |
| 7 | +The `prompt()` method shows a dialog with a single-line field for user input. |
8 | 8 |
|
9 |
| ---- |
| 9 | +The method is part of the [`dialogs` module](https://docs.nativescript.org/api-reference/modules/_ui_dialogs_). |
10 | 10 |
|
11 |
| -```javascript |
12 |
| -const dialogs = require('tns-core-modules/ui/dialogs') |
| 11 | +--- |
13 | 12 |
|
14 |
| -module.exports = { |
15 |
| - mounted() { |
16 |
| - dialogs.prompt('How are you?', 'Amazing!') |
17 |
| - .then(result => { |
18 |
| - console.log(`Dialog result: ${result.result}, text: ${result.text}`) |
19 |
| - }) |
20 |
| - } |
21 |
| -} |
22 |
| -``` |
| 13 | +## Basic use |
23 | 14 |
|
24 |
| -The PromptDialog is also available globally, so instead of importing it from the dialogs module, you can simply call |
| 15 | +The `prompt()` method is available globally. You can call it anywhere in your app. |
25 | 16 |
|
26 |
| -```javascript |
27 |
| -prompt('How are you?', 'Amazing!') |
| 17 | +```JavaScript |
| 18 | +prompt('Your message to the user', 'Suggested user input') |
28 | 19 | .then(result => {
|
29 | 20 | console.log(`Dialog result: ${result.result}, text: ${result.text}`)
|
30 | 21 | })
|
31 | 22 | ```
|
32 | 23 |
|
33 |
| -anywhere in your code. |
| 24 | +## Configure dialog options |
| 25 | + |
| 26 | +```JavaScript |
| 27 | +prompt({ |
| 28 | + title: "Your dialog title", |
| 29 | + message: "Your message", |
| 30 | + okButtonText: "Your OK button text", |
| 31 | + cancelButtonText: "Your Cancel button text", |
| 32 | + defaultText: "Suggested user input", |
| 33 | +}).then(result => { |
| 34 | + console.log(`Dialog result: ${result.result}, text: ${result.text}`) |
| 35 | +}); |
| 36 | +``` |
| 37 | + |
| 38 | +## Configure input type |
| 39 | + |
| 40 | +You can also configure the input type using `inputType`. You can choose between plain text (`text`), email-enabled input (`email`), and password-like hidden input (`password`). |
| 41 | + |
| 42 | +```JavaScript |
| 43 | +inputType: dialogs.inputType.text |
| 44 | +inputType: dialogs.inputType.email |
| 45 | +inputType: dialogs.inputType.password |
| 46 | +``` |
| 47 | + |
| 48 | +**NOTE:** This option is not globally available and you need to require the `dialogs` module in your app before using `inputType`. |
| 49 | + |
| 50 | +```JavaScript |
| 51 | +const dialogs = require('tns-core-modules/ui/dialogs') |
| 52 | +``` |
| 53 | + |
| 54 | +### Example |
| 55 | + |
| 56 | +```JavaScript |
| 57 | +const dialogs = require('tns-core-modules/ui/dialogs') |
| 58 | + |
| 59 | +prompt({ |
| 60 | + title: "Email Prompt", |
| 61 | + message: "Provide your email address:", |
| 62 | + okButtonText: "OK", |
| 63 | + cancelButtonText: "Cancel", |
| 64 | + |
| 65 | + inputType: dialogs.inputType.email |
| 66 | +}).then(result => { |
| 67 | + console.log(`Dialog result: ${result.result}, text: ${result.text}`) |
| 68 | +}); |
| 69 | +``` |
34 | 70 |
|
35 |
| -[> screenshots for=PromptDialog <] |
| 71 | +[> screenshots for=PromptDialog <] |
0 commit comments