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