Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit f374e2c

Browse files
author
Serhiy Zhovnir
committed
Described missed prompt widget options and added code samples of prompt widget initialization
1 parent 856de21 commit f374e2c

File tree

2 files changed

+311
-1
lines changed

2 files changed

+311
-1
lines changed
53.8 KB
Loading

guides/v2.2/javascript-dev-guide/widgets/widget_prompt.md

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 311 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,311 @@
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+
```javascript
80+
actions: {
81+
confirm: function(){},
82+
cancel: function(){},
83+
always: function(){}
84+
}
85+
```
86+
87+
### autoOpen {#prompt_autoopen}
88+
89+
Automatically open the prompt window when the widget is initialized.
90+
91+
**Type**: Boolean
92+
93+
**Default value**: `false`
94+
95+
### clickableOverlay {#prompt_clickableOverlay}
96+
97+
Close the prompt window when a user clicks on the overlay.
98+
99+
**Type**: Boolean
100+
101+
**Default value**: `true`
102+
103+
### `content` {#prompt_content}
104+
105+
The prompt window content.
106+
107+
**Type**: String.
108+
109+
### `focus` {#prompt_focus}
110+
The selector of the element to be in focus when the prompt window opens.
111+
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`.
112+
113+
**Type**: String.
114+
115+
**Default value**: `''`
116+
117+
### `title` {#prompt_title}
118+
The title of the prompt window.
119+
120+
**Type**: String.
121+
122+
**Default value**: `''`
123+
124+
### `modalClass` {#prompt_modalClass}
125+
The CSS class of the prompt window.
126+
127+
**Type**: String.
128+
129+
**Default value**: `'prompt'`
130+
131+
### `promptContentTmpl` {#prompt_promptContentTmpl}
132+
The template of the prompt popup form.
133+
134+
**Type**: String.
135+
136+
**Default value**:
137+
138+
```html
139+
<form <%= formAttr %>>
140+
<fieldset class="fieldset">
141+
<div class="field">
142+
<% if(data.label){ %>
143+
<label for="prompt-field-<%- data.id %>" class="label">
144+
<span><%= data.label %></span>
145+
</label>
146+
<% } %>
147+
<div class="control">
148+
<input type="text" data-role="promptField" id="prompt-field-<%- data.id %>" class="input-text" <%= inputAttr %>/>
149+
</div>
150+
</div>
151+
</fieldset>
152+
</form>
153+
```
154+
155+
The file with template [`ui/template/modal/modal-prompt-content.html`].
156+
157+
### `value` {#prompt_value}
158+
The value of the prompt field.
159+
160+
**Type**: String.
161+
162+
**Default value**: `''`
163+
164+
### `promptField` {#prompt_promptField}
165+
The prompt field selector.
166+
167+
**Type**: String.
168+
169+
**Default value**: `'[data-role="promptField"]'`
170+
171+
### `attributesForm` {#prompt_attributesForm}
172+
The attributes for the prompt form.
173+
174+
**Type**: Object.
175+
176+
**Default value**: `{}`
177+
178+
### `attributesField` {#prompt_attributesField}
179+
The attributes for the prompt field.
180+
181+
**Type**: Object.
182+
183+
**Default value**: `{}`
184+
185+
### `validation` {#prompt_validation}
186+
Specifies if the prompt form should be validated, when the confirmation button is clicked.
187+
188+
**Type**: Boolean
189+
190+
**Default value**: `false`
191+
192+
### `validationRules` {#prompt_validationRules}
193+
The array of validation classes which will be added to prompt field.
194+
195+
**Type**: Array
196+
197+
**Default value**: `[]`
198+
199+
## Events {#prompt_events}
200+
201+
The prompt widget implements the following events:
202+
203+
- `confirm` callback: called when the confirmation button is clicked. The first argument is the value of the input field.
204+
- `cancel` callback: called when the cancel button is clicked.
205+
- `always` callback: called when the popup is closed.
206+
207+
## Keyboard navigation {#prompt_key_navigation}
208+
209+
The keyboard navigation for the alert windows is similar to the [navigation of the modal widget].
210+
211+
## Code Sample
212+
213+
### Code sample of initialization on an element
214+
215+
```html
216+
<script>
217+
require([
218+
'jquery',
219+
'Magento_Ui/js/modal/prompt'
220+
], function ($) {
221+
'use strict';
222+
223+
$('.prompt-modal-content').prompt({
224+
title: 'Prompt Title',
225+
modalClass: 'prompt',
226+
value: 'Value by default',
227+
validation: true,
228+
promptField: '[data-role="promptField"]',
229+
validationRules: ['required-entry'],
230+
attributesForm: {
231+
novalidate: 'novalidate',
232+
action: ''
233+
},
234+
attributesField: {
235+
name: 'name',
236+
'data-validate': '{required:true}',
237+
maxlength: '255'
238+
}, // attributes for the input field
239+
actions: {
240+
always: function() {
241+
// do something when the modal is closed
242+
},
243+
confirm: function () {
244+
// do something when the confirmation button is clicked
245+
},
246+
cancel: function () {
247+
// do something when the cancel button is clicked
248+
}
249+
}
250+
});
251+
});
252+
</script>
253+
```
254+
255+
### Code sample of standalone initialization
256+
257+
```html
258+
<div class="prompt-modal-content">
259+
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Adipisci amet aut consequuntur culpa cum, distinctio earum harum, iste magnam nobis numquam pariatur tempora ullam vero vitae. Hic ipsam itaque velit.</p>
260+
</div>
261+
262+
<script>
263+
require([
264+
'jquery',
265+
'Magento_Ui/js/modal/prompt'
266+
], function ($, prompt) {
267+
'use strict';
268+
269+
prompt({
270+
title: 'Prompt Title',
271+
content: $('.prompt-modal-content'),
272+
modalClass: 'prompt',
273+
value: 'Value by default',
274+
validation: true,
275+
promptField: '[data-role="promptField"]',
276+
validationRules: ['required-entry'],
277+
attributesForm: {
278+
novalidate: 'novalidate',
279+
action: ''
280+
},
281+
attributesField: {
282+
name: 'name',
283+
'data-validate': '{required:true}',
284+
maxlength: '255'
285+
},
286+
actions: {
287+
always: function() {
288+
// do something when the modal is closed
289+
},
290+
confirm: function () {
291+
// do something when the confirmation button is clicked
292+
},
293+
cancel: function () {
294+
// do something when the cancel button is clicked
295+
}
296+
}
297+
});
298+
});
299+
</script>
300+
```
301+
302+
## Result
303+
304+
![Prompt Widget]({{ site.baseurl }}/common/images/widget/prompt-widget-result.png)
305+
306+
[Magento modal widget]: {{page.baseurl}}/javascript-dev-guide/widgets/widget_modal.html
307+
[`<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
308+
[`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
309+
[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
310+
[JavaScript initialization]: {{page.baseurl}}/javascript-dev-guide/javascript/js_init.html
311+
[navigation of the modal widget]: {{page.baseurl}}/javascript-dev-guide/widgets/widget_modal.html#key_navigation

0 commit comments

Comments
 (0)