|
| 1 | +--- |
| 2 | +group: javascript-developer-guide |
| 3 | +subgroup: 3_Widgets |
| 4 | +title: Prompt widget |
| 5 | +--- |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +The Magento prompt [widget](https://glossary.magento.com/widget) implements a modal pop-up window with an input field, and a cancel and a confirmation button. |
| 10 | + |
| 11 | +It extends the [Magento modal widget]. |
| 12 | + |
| 13 | +The prompt widget source is [`<Magento_Ui_module_dir>/view/base/web/js/modal/prompt.js`]. |
| 14 | + |
| 15 | +The widget can be used for implementing prompt windows for both, admin and [storefront](https://glossary.magento.com/storefront). The design patterns for the modal pop-up windows in the admin are described in the [Magento Admin Pattern Library, the Slide-out Panels, Modal Windows, and Overlays topic.] |
| 16 | + |
| 17 | +## Initialize the prompt widget {#prompt_init} |
| 18 | + |
| 19 | +The prompt widget can be initialized with or without binding to a certain element. |
| 20 | + |
| 21 | +**Example 1**: initialization on an element |
| 22 | + |
| 23 | +```javascript |
| 24 | +$('#prompt_init').prompt({ |
| 25 | + title: 'Prompt title', |
| 26 | + actions: { |
| 27 | + confirm: function(){}, //callback on 'Ok' button click |
| 28 | + cancel: function(){}, //callback on 'Cancel' button click |
| 29 | + always: function(){} |
| 30 | + } |
| 31 | +}); |
| 32 | +``` |
| 33 | + |
| 34 | +**Example 2**: standalone initialization |
| 35 | + |
| 36 | +```javascript |
| 37 | +require([ |
| 38 | + 'Magento_Ui/js/modal/prompt' |
| 39 | +], function(prompt) { // Variable that represents the `prompt` function |
| 40 | + |
| 41 | + prompt({ |
| 42 | + title: 'Some title', |
| 43 | + content: 'Some content', |
| 44 | + actions: { |
| 45 | + confirm: function(){}, |
| 46 | + cancel: function(){}, |
| 47 | + always: function(){} |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | +}); |
| 52 | +``` |
| 53 | + |
| 54 | +For details about how to initialize a widget in a `.phtml` template, refer to the [JavaScript initialization] topic. |
| 55 | + |
| 56 | +## Options {#prompt_options} |
| 57 | + |
| 58 | +- [actions](#prompt_actions) |
| 59 | +- [autoOpen](#prompt_autoopen) |
| 60 | +- [clickableOverlay](#prompt_clickableOverlay) |
| 61 | +- [content](#prompt_content) |
| 62 | +- [focus](#prompt_focus) |
| 63 | +- [title](#prompt_title) |
| 64 | +- [modalClass](#prompt_modalClass) |
| 65 | +- [promptContentTmpl](#prompt_promptContentTmpl) |
| 66 | +- [value](#prompt_value) |
| 67 | +- [promptField](#prompt_promptField) |
| 68 | +- [attributesForm](#prompt_attributesForm) |
| 69 | +- [attributesField](#prompt_attributesField) |
| 70 | +- [validation](#prompt_validation) |
| 71 | +- [validationRules](#prompt_validationRules) |
| 72 | + |
| 73 | +### `actions` {#prompt_actions} |
| 74 | +Widget callbacks. |
| 75 | + |
| 76 | +**Type**: Object. |
| 77 | + |
| 78 | +**Default value**: |
| 79 | + |
| 80 | +```javascript |
| 81 | +actions: { |
| 82 | + confirm: function(){}, |
| 83 | + cancel: function(){}, |
| 84 | + always: function(){} |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +### autoOpen {#prompt_autoopen} |
| 89 | + |
| 90 | +Automatically open the prompt window when the widget is initialized. |
| 91 | + |
| 92 | +**Type**: Boolean |
| 93 | + |
| 94 | +**Default value**: `false` |
| 95 | + |
| 96 | +### clickableOverlay {#prompt_clickableOverlay} |
| 97 | + |
| 98 | +Close the prompt window when a user clicks on the overlay. |
| 99 | + |
| 100 | +**Type**: Boolean |
| 101 | + |
| 102 | +**Default value**: `true` |
| 103 | + |
| 104 | +### `content` {#prompt_content} |
| 105 | + |
| 106 | +The prompt window content. |
| 107 | + |
| 108 | +**Type**: String. |
| 109 | + |
| 110 | +### `focus` {#prompt_focus} |
| 111 | +The selector of the element to be in focus when the prompt window opens. |
| 112 | +If `focus` is not specified or set to empty string, the focus is on the close button. If focusing is not required, set `focus` to `none`. |
| 113 | + |
| 114 | +**Type**: String. |
| 115 | + |
| 116 | +**Default value**: `''` |
| 117 | + |
| 118 | +### `title` {#prompt_title} |
| 119 | +The title of the prompt window. |
| 120 | + |
| 121 | +**Type**: String. |
| 122 | + |
| 123 | +**Default value**: `''` |
| 124 | + |
| 125 | +### `modalClass` {#prompt_modalClass} |
| 126 | +The CSS class of the prompt window. |
| 127 | + |
| 128 | +**Type**: String. |
| 129 | + |
| 130 | +**Default value**: `'prompt'` |
| 131 | + |
| 132 | +### `promptContentTmpl` {#prompt_promptContentTmpl} |
| 133 | +The template of the prompt popup form. |
| 134 | + |
| 135 | +**Type**: String. |
| 136 | + |
| 137 | +**Default value**: |
| 138 | + |
| 139 | +```html |
| 140 | +<form <%= formAttr %>> |
| 141 | + <fieldset class="fieldset"> |
| 142 | + <div class="field"> |
| 143 | + <% if(data.label){ %> |
| 144 | + <label for="prompt-field-<%- data.id %>" class="label"> |
| 145 | + <span><%= data.label %></span> |
| 146 | + </label> |
| 147 | + <% } %> |
| 148 | + <div class="control"> |
| 149 | + <input type="text" data-role="promptField" id="prompt-field-<%- data.id %>" class="input-text" <%= inputAttr %>/> |
| 150 | + </div> |
| 151 | + </div> |
| 152 | + </fieldset> |
| 153 | +</form> |
| 154 | +``` |
| 155 | + |
| 156 | +The file with template [`ui/template/modal/modal-prompt-content.html`]. |
| 157 | + |
| 158 | +### `value` {#prompt_value} |
| 159 | +The value of the prompt field. |
| 160 | + |
| 161 | +**Type**: String. |
| 162 | + |
| 163 | +**Default value**: `''` |
| 164 | + |
| 165 | +### `promptField` {#prompt_promptField} |
| 166 | +The prompt field selector. |
| 167 | + |
| 168 | +**Type**: String. |
| 169 | + |
| 170 | +**Default value**: `'[data-role="promptField"]'` |
| 171 | + |
| 172 | +### `attributesForm` {#prompt_attributesForm} |
| 173 | +The attributes for the prompt form. |
| 174 | + |
| 175 | +**Type**: Object. |
| 176 | + |
| 177 | +**Default value**: `{}` |
| 178 | + |
| 179 | +### `attributesField` {#prompt_attributesField} |
| 180 | +The attributes for the prompt field. |
| 181 | + |
| 182 | +**Type**: Object. |
| 183 | + |
| 184 | +**Default value**: `{}` |
| 185 | + |
| 186 | +### `validation` {#prompt_validation} |
| 187 | +Specifies if the prompt form should be validated, when the confirmation button is clicked. |
| 188 | + |
| 189 | +**Type**: Boolean |
| 190 | + |
| 191 | +**Default value**: `false` |
| 192 | + |
| 193 | +### `validationRules` {#prompt_validationRules} |
| 194 | +The array of validation classes which will be added to prompt field. |
| 195 | + |
| 196 | +**Type**: Array |
| 197 | + |
| 198 | +**Default value**: `[]` |
| 199 | + |
| 200 | +## Events {#prompt_events} |
| 201 | + |
| 202 | +The prompt widget implements the following events: |
| 203 | + |
| 204 | +- `confirm` callback: called when the confirmation button is clicked. The first argument is the value of the input field. |
| 205 | +- `cancel` callback: called when the cancel button is clicked. |
| 206 | +- `always` callback: called when the popup is closed. |
| 207 | + |
| 208 | +## Keyboard navigation {#prompt_key_navigation} |
| 209 | + |
| 210 | +The keyboard navigation for the alert windows is similar to the [navigation of the modal widget]. |
| 211 | + |
| 212 | +## Code Sample |
| 213 | + |
| 214 | +### Code sample of initialization on an element |
| 215 | + |
| 216 | +```html |
| 217 | +<script> |
| 218 | + require([ |
| 219 | + 'jquery', |
| 220 | + 'Magento_Ui/js/modal/prompt' |
| 221 | + ], function ($) { |
| 222 | + 'use strict'; |
| 223 | +
|
| 224 | + $('.prompt-modal-content').prompt({ |
| 225 | + title: 'Prompt Title', |
| 226 | + modalClass: 'prompt', |
| 227 | + value: 'Value by default', |
| 228 | + validation: true, |
| 229 | + promptField: '[data-role="promptField"]', |
| 230 | + validationRules: ['required-entry'], |
| 231 | + attributesForm: { |
| 232 | + novalidate: 'novalidate', |
| 233 | + action: '' |
| 234 | + }, |
| 235 | + attributesField: { |
| 236 | + name: 'name', |
| 237 | + 'data-validate': '{required:true}', |
| 238 | + maxlength: '255' |
| 239 | + }, // attributes for the input field |
| 240 | + actions: { |
| 241 | + always: function() { |
| 242 | + // do something when the modal is closed |
| 243 | + }, |
| 244 | + confirm: function () { |
| 245 | + // do something when the confirmation button is clicked |
| 246 | + }, |
| 247 | + cancel: function () { |
| 248 | + // do something when the cancel button is clicked |
| 249 | + } |
| 250 | + } |
| 251 | + }); |
| 252 | + }); |
| 253 | +</script> |
| 254 | +``` |
| 255 | + |
| 256 | +### Code sample of standalone initialization |
| 257 | + |
| 258 | +```html |
| 259 | +<div class="prompt-modal-content"> |
| 260 | + <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p> |
| 261 | +</div> |
| 262 | + |
| 263 | +<script> |
| 264 | + require([ |
| 265 | + 'jquery', |
| 266 | + 'Magento_Ui/js/modal/prompt' |
| 267 | + ], function ($, prompt) { |
| 268 | + 'use strict'; |
| 269 | +
|
| 270 | + prompt({ |
| 271 | + title: 'Prompt Title', |
| 272 | + content: $('.prompt-modal-content'), |
| 273 | + modalClass: 'prompt', |
| 274 | + value: 'Value by default', |
| 275 | + validation: true, |
| 276 | + promptField: '[data-role="promptField"]', |
| 277 | + validationRules: ['required-entry'], |
| 278 | + attributesForm: { |
| 279 | + novalidate: 'novalidate', |
| 280 | + action: '' |
| 281 | + }, |
| 282 | + attributesField: { |
| 283 | + name: 'name', |
| 284 | + 'data-validate': '{required:true}', |
| 285 | + maxlength: '255' |
| 286 | + }, |
| 287 | + actions: { |
| 288 | + always: function() { |
| 289 | + // do something when the modal is closed |
| 290 | + }, |
| 291 | + confirm: function () { |
| 292 | + // do something when the confirmation button is clicked |
| 293 | + }, |
| 294 | + cancel: function () { |
| 295 | + // do something when the cancel button is clicked |
| 296 | + } |
| 297 | + } |
| 298 | + }); |
| 299 | + }); |
| 300 | +</script> |
| 301 | +``` |
| 302 | + |
| 303 | +## Result |
| 304 | + |
| 305 | + |
| 306 | + |
| 307 | +[Magento modal widget]: {{page.baseurl}}/javascript-dev-guide/widgets/widget_modal.html |
| 308 | +[`<Magento_Ui_module_dir>/view/base/web/js/modal/prompt.js`]: {{ site.mage2bloburl }}/{{ page.guide_version }}/app/code/Magento/Ui/view/base/web/js/modal/prompt.js |
| 309 | +[`ui/template/modal/modal-prompt-content.html`]: {{ site.mage2bloburl }}/{{ page.guide_version }}/app/code/Magento/Ui/view/base/web/templates/modal/modal-prompt-content.html |
| 310 | +[Magento Admin Pattern Library, the Slide-out Panels, Modal Windows, and Overlays topic.]: {{page.baseurl}}/pattern-library/containers/slideouts-modals-overlays/slideouts-modals-overalys.html#modals |
| 311 | +[JavaScript initialization]: {{page.baseurl}}/javascript-dev-guide/javascript/js_init.html |
| 312 | +[navigation of the modal widget]: {{page.baseurl}}/javascript-dev-guide/widgets/widget_modal.html#key_navigation |
0 commit comments