• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# <dialog> Development
2
3
4The **<dialog>** component is custom pop-up container for showing critical information or calling for an action. For details, see [dialog](../reference/arkui-js/js-components-container-dialog.md).
5
6
7## Creating a <dialog> Component
8
9Create a **<dialog>** component in the .hml file under **pages/index** and add **<button>** components to trigger the **<dialog>**. The **<dialog>** component supports only the **width**, **height**, **margin**, **margin-[left|top|right|bottom]**, and **margin-[start|end]** styles.
10
11```
12<!-- xxx.hml -->
13<div class="doc-page">
14  <dialog class="dialogClass" id="dialogId" dragable="true">
15    <div class="content">
16      <text>this is a dialog</text>
17    </div>
18  </dialog>
19  <button value="click me" onclick="openDialog"></button>
20</div>
21```
22
23
24```
25/* xxx.css */
26.doc-page {
27  width:100%;
28  height:100%;
29  flex-direction: column;
30  align-items: center;
31  justify-content: center;
32  background-color: #F1F3F5;
33}
34.dialogClass{
35  width: 80%;
36  height: 250px;
37  margin-start: 1%;
38}
39.content{
40  width: 100%;
41  height: 250px;
42  justify-content: center;
43  background-color: #e8ebec;
44  border-radius: 20px;
45}
46text{
47  width: 100%;
48  height: 100%;
49  text-align: center;
50}
51button{
52  width: 70%;
53  height: 60px;
54}
55```
56
57
58```
59/* xxx.js */
60export default {
61  //Touch to open the dialog box.
62  openDialog(){
63    this.$element('dialogId').show()
64  },
65}
66```
67
68![en-us_image_0000001267767893](figures/en-us_image_0000001267767893.gif)
69
70
71## Setting Dialog Box Response
72
73Add a **cancel** event that is triggered when a user touches a non-dialog area to cancel the pop-up dialog box. Add the **show** and **close** methods to display and close the dialog box, respectively.
74
75
76
77```
78<!-- xxx.hml -->
79<div class="doc-page">
80  <dialog class="dialogClass" id="dialogId" oncancel="canceldialog">
81    <div class="dialogDiv">
82      <text>dialog</text>
83      <button value="confirm" onclick="confirmClick"></button>
84    </div>
85  </dialog>
86  <button value="click me" onclick="openDialog"></button>
87</div>
88```
89
90
91
92```
93/* xxx.css */
94.doc-page {
95  width:100%;
96  height:100%;
97  flex-direction: column;
98  align-items: center;
99  justify-content: center;
100  background-color: #F1F3F5;
101}
102.dialogClass{
103  width: 80%;
104  height: 300px;
105  margin-start: 1%;
106}
107.dialogDiv{
108  width: 100%;
109  flex-direction: column;
110  justify-content: center;
111  align-self: center;
112}
113text{
114  height: 100px;
115  align-self: center;
116}
117button{
118  align-self: center;
119  margin-top: 20px;
120  width: 60%;
121  height: 80px;
122}
123```
124
125
126
127```
128/* xxx.js */
129import prompt from '@system.prompt';
130export default {
131  canceldialog(e){
132    prompt.showToast({
133      message: 'dialogCancel'
134    })
135  },
136  openDialog(){
137    this.$element('dialogId').show()
138     prompt.showToast({
139      message: 'dialogShow'
140    })
141  },
142  confirmClick(e) {
143    this.$element('dialogId').close()
144    prompt.showToast({
145      message: 'dialogClose'
146    })
147  },
148}
149```
150
151
152![en-us_image_0000001223287720](figures/en-us_image_0000001223287720.gif)
153
154
155> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/>
156> - This component supports only one child component.
157>
158> - Attributes and styles of a **&lt;dialog&gt;** component cannot be dynamically updated.
159>
160> - The **&lt;dialog&gt;** component does not support the **focusable** and **click-effect** attributes.
161
162
163## Example Scenario
164
165
166Use the **&lt;dialog&gt;** component to implement a schedule. When the dialog box is open, use the [**&lt;textarea&gt;**](../reference/arkui-js/js-components-basic-textarea.md) component to add an event and touch the OK button to obtain the current time and save the input text. The events in the calendar are displayed in a list.
167
168
169
170```
171<!-- xxx.hml -->
172<div class="doc-page">
173  <text style="margin-top: 60px;margin-left: 30px;">
174    <span>{{date}} events</span>
175  </text>
176  <div class="btndiv">
177    <button type="circle" class="btn" onclick="addschedule">+</button>
178  </div>
179<!--  for Render events data  -->
180  <list style="width: 100%;">
181    <list-item type="item" for="schedulelist" style="width:100%;height: 200px;">
182      <div class="schedulediv">
183        <text class="text1">{{date}}  event</text>
184        <text class="text2">{{$item.schedule}}</text>
185      </div>
186    </list-item>
187  </list>
188  <dialog id="datedialog" oncancel="canceldialog" >
189    <div class="dialogdiv">
190      <div class="innertxt">
191        <text class="text3">{{date}}</text>
192        <text class="text4">New event</text>
193      </div>
194      <textarea placeholder="Event information" onchange="getschedule" class="area" extend="true"></textarea>
195      <div class="innerbtn">
196        <button type="text" value="Cancel" onclick="cancelschedule" class="btntxt"></button>
197        <button type="text" value="OK" onclick="setschedule" class="btntxt"></button>
198      </div>
199    </div>
200  </dialog>
201</div>
202```
203
204
205
206```
207/* xxx.css */
208.doc-page {
209  flex-direction: column;
210  background-color: #F1F3F5;
211}
212.btndiv {
213  width: 100%;
214  height: 200px;
215  flex-direction: column;
216  align-items: center;
217  justify-content: center;
218}
219.btn {
220  radius:60px;
221  font-size: 100px;
222  background-color: #1E90FF;
223}
224.schedulediv {
225  width: 100%;
226  height: 200px;
227  flex-direction: column;
228  justify-content: space-around;
229  padding-left: 55px;
230}
231.text1 {
232  color: #000000;
233  font-weight: bold;
234  font-size: 39px;
235}
236.text2 {
237  color: #a9a9a9;
238  font-size: 30px;
239}
240.dialogdiv {
241  flex-direction: column;
242  align-items: center;
243}
244.innertxt {
245  width: 320px;
246  height: 160px;
247  flex-direction: column;
248  align-items: center;
249  justify-content: space-around;
250}
251.text3 {
252  font-family: serif;
253  color: #1E90FF;
254  font-size: 38px;
255}
256.text4 {
257  color: #a9a9a9;
258  font-size: 33px;
259}
260.area {
261  width: 320px;
262  border-bottom: 1px solid #1E90FF;
263}
264.innerbtn {
265  width: 320px;
266  height: 120px;
267  justify-content: space-around;
268}
269.btntxt {
270  text-color: #1E90FF;
271}
272```
273
274
275
276```
277/* xxx.js */
278var info = null;
279import prompt from '@system.prompt';
280import router from '@system.router';
281export default {
282  data: {
283    curYear:'',
284    curMonth:'',
285    curDay:'',
286    date:'',
287    schedule:'',
288    schedulelist:[]
289  },
290  onInit() {
291    // Obtain the current date.
292    var date = new Date();
293    this.curYear = date.getFullYear();
294    this.curMonth = date.getMonth() + 1;
295    this.curDay = date.getDate();
296    this.date = this.curYear + '-' + this.curMonth + '-' + this.curDay;
297    this.schedulelist = []
298  },
299  addschedule(e) {
300    this.$element('datedialog').show()
301  },
302  canceldialog(e) {
303    prompt.showToast({
304      message: 'Event setting canceled.'
305    })
306  },
307  getschedule(e) {
308    info = e.value
309  },
310  cancelschedule(e) {
311    this.$element('datedialog').close()
312    prompt.showToast({
313      message: 'Event setting canceled.'
314    })
315  },
316//    Touch OK to save the data.
317  setschedule(e) {
318    if (e.text === '') {
319      this.schedule = info
320    } else {
321      this.schedule = info
322      var addItem =  {schedule: this.schedule,}
323      this.schedulelist.push(addItem)
324    }
325    this.$element('datedialog').close()
326  }
327}
328```
329
330
331![en-us_image_0000001223127756](figures/en-us_image_0000001223127756.gif)