prompt
The prompt function wraps the <mdui-dialog> component and replaces the native window.prompt function. With this function, you can open a text input dialog without writing any HTML.
<mdui-button class="example-button">open</mdui-button>
<script type="module">
import { prompt } from "mdui/functions/prompt.js";
const button = document.querySelector(".example-button");
button.addEventListener("click", () => {
prompt({
headline: "Prompt Title",
description: "Prompt description",
confirmText: "OK",
cancelText: "Cancel",
onConfirm: (value) => console.log("confirmed: " + value),
onCancel: () => console.log("canceled"),
});
});
</script>API
prompt(options: Options): Promise<string>
The function takes an Options object and returns a Promise. The Promise resolves with the value entered in the text field when the dialog is closed by clicking the confirm button and rejects otherwise.
Options
| Property | Type | Default |
|---|---|---|
headline |
string |
- |
| The title of the dialog. | ||
description |
string |
- |
| The description of the dialog. | ||
icon |
string |
- |
| The Material Icons name displayed at the top of the dialog. | ||
closeOnEsc |
boolean |
false |
Whether the dialog can be closed by pressing the Esc key. If set to true, the dialog closes when the Esc key is pressed. |
||
closeOnOverlayClick |
boolean |
false |
| Whether the dialog can be closed by clicking on the overlay. | ||
confirmText |
string |
OK |
| The text for the confirm button. | ||
cancelText |
string |
Cancel |
| The text for the cancel button. | ||
stackedActions |
boolean |
false |
| Whether to stack the bottom action buttons vertically. | ||
queue |
string |
- |
|
The queue name. Queues are disabled by default. If this function is called multiple times, multiple dialogs will appear simultaneously. If you provide a queue name, dialogs with the same name open one after another. The |
||
onConfirm |
(value: string, dialog: Dialog) => void | boolean | Promise<void> |
- |
|
Called when the confirm button is clicked. The callback receives the text field value and the dialog instance as its arguments, and By default, clicking the confirm button closes the dialog. If the return value is |
||
onCancel |
(value: string, dialog: Dialog) => void | boolean | Promise<void> |
- |
|
Called when the cancel button is clicked. The callback receives the text field value and the dialog instance as its arguments, and By default, clicking the cancel button closes the dialog. If the return value is |
||
onOpen |
(dialog: Dialog) => void |
- |
|
Called when the dialog starts to open. The callback receives the dialog instance as its argument, and |
||
onOpened |
(dialog: Dialog) => void |
- |
|
Called when the dialog's opening animation completes. The callback receives the dialog instance as its argument, and |
||
onClose |
(dialog: Dialog) => void |
- |
|
Called when the dialog starts to close. The callback receives the dialog instance as its argument, and |
||
onClosed |
(dialog: Dialog) => void |
- |
|
Called when the dialog's closing animation completes. The callback receives the dialog instance as its argument, and |
||
onOverlayClick |
(dialog: Dialog) => void |
- |
|
Called when the overlay is clicked. The callback receives the dialog instance as its argument, and |
||
validator |
(value: string) => boolean | string | Promise<void> |
- |
|
A validation function for the text field that takes the field value as its parameter. It runs only after native browser validation succeeds. If it returns a If it returns a string, a non-empty string indicates a validation failure, with the returned string serving as the error message. If it returns a Promise, a resolved Promise indicates success and a rejected Promise indicates failure. The rejection reason is used as the error message. |
||
textFieldOptions |
Partial<TextField> |
- |
The internal text field is a <mdui-text-field> component. You can configure the <mdui-text-field> component using this parameter. |
||