ui-js-components-stepper.md 9.1 KB
Newer Older
Z
zengyawen 已提交
1 2
# Stepper

Z
zengyawen 已提交
3
当一个任务需要多个步骤时,可以使用stepper组件展示当前进展。具体用法请参考[Stepper API](../reference/arkui-js/js-components-container-stepper.md)
Z
zengyawen 已提交
4 5 6 7 8 9 10 11 12 13 14


> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 从API Version 5 开始支持。


## 创建Stepper组件

在pages/index目录下的hml文件中创建一个Stepper组件。

```
Z
zengyawen 已提交
15 16
<!-- index.hml -->
<div class="container"> 
W
wangshuainan1 已提交
17 18
 <stepper>    
   <stepper-item>     
Z
zengyawen 已提交
19 20 21 22 23 24 25
     <text>Step 1</text>
   </stepper-item> 
   <stepper-item>     
     <text>Step 2</text>
   </stepper-item> 
 </stepper> 
</div>
Z
zengyawen 已提交
26 27 28 29 30
```

```
/* xxx.css */
.container {
W
wangshuainan1 已提交
31 32
  width:100%;
  height:100%;
Z
zengyawen 已提交
33 34 35 36 37 38 39 40 41 42 43 44
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #F1F3F5;
}
text{
  width: 100%;
  height: 100%;
  text-align: center;
}
```

Z
zengyawen 已提交
45
![zh-cn_image_0000001234289455](figures/zh-cn_image_0000001234289455.gif)
Z
zengyawen 已提交
46 47 48 49 50 51 52


## 设置index属性

页面默认显示索引值为index的步骤。

```
Z
zengyawen 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66
<!-- index.hml -->
<div class="container"> 
 <stepper index="2">    
   <stepper-item>     
     <text>stepper-item1</text>
   </stepper-item> 
   <stepper-item>     
     <text>stepper-item2</text>
   </stepper-item> 
   <stepper-item>     
     <text>stepper-item3</text>
   </stepper-item> 
  </stepper> 
</div>
Z
zengyawen 已提交
67 68 69 70 71
```

```
/* index.css */
.container {
W
wangshuainan1 已提交
72 73
  width:100%;
  height:100%;
Z
zengyawen 已提交
74 75 76 77 78 79 80 81 82 83
  flex-direction: column;
  background-color:#F1F3F5;
}
text{
  width: 100%;
  height: 100%;
  text-align: center;
}
```

Z
zengyawen 已提交
84
![zh-cn_image_0000001234011019](figures/zh-cn_image_0000001234011019.gif)
Z
zengyawen 已提交
85 86 87 88

通过设置label属性,自定义stepper-item的提示按钮。

```
Z
zengyawen 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
<!-- index.hml -->
<div class="container" style="background-color:#F1F3F5;"> 
 <stepper index="1">    
   <stepper-item label="{{label_1}}">     
     <text>stepper-item1</text>
   </stepper-item> 
   <stepper-item label="{{label_2}}">     
     <text>stepper-item2</text>
   </stepper-item> 
   <stepper-item label="{{label_3}}">     
     <text>stepper-item3</text>
   </stepper-item>
   <stepper-item>     
     <text>stepper-item4</text>
   </stepper-item> 
 </stepper> 
</div>
Z
zengyawen 已提交
106 107 108 109
```

```
/* index.css */
W
wangshuainan1 已提交
110 111 112
.container {  
  width:100%;
  height:100%;
Z
zengyawen 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  flex-direction: column;
  background-color:#F1F3F5;
}
text{
  width: 100%;
  height: 100%;
  text-align: center;
}
```

```
/* index.js */
export default { 
  data: {
    label_1:{      nextLabel: 'NEXT',      status: 'normal'    },
    label_2:{
      prevLabel: 'BACK',
      nextLabel: 'NEXT',
      status: 'normal'
    },
    label_3:{
      prevLabel: 'BACK',
      nextLabel: 'END',
      status: 'disabled'
    },
  },
}
```

![zh-cn_image_0000001163531210](figures/zh-cn_image_0000001163531210.gif)


## 设置样式

Stepper组件默认填充父容器,通过border和background-color设置边框、背景色。
```
Z
zengyawen 已提交
149 150
<!-- index.hml -->
<div class="container" > 
Z
zengyawen 已提交
151
  <div class="stepperContent">
Z
zengyawen 已提交
152 153 154 155 156
    <stepper class="stepperClass">    
      <stepper-item>     
        <text>stepper-item1</text>
      </stepper-item> 
    </stepper> 
Z
zengyawen 已提交
157 158 159 160 161 162 163
  </div>
</div>
```

```
/* index.css */
.container {
W
wangshuainan1 已提交
164 165
  width:100%;
  height:100%;
Z
zengyawen 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color:#F1F3F5;
}
.stepperContent{
  width: 300px;
  height: 300px;
}
.stepperClass{
  border:1px solid silver ;  background-color: white;
}
text{
  width: 100%;
  height: 100%;
  text-align: center;
}
```

Z
zengyawen 已提交
185
![zh-cn_image_0000001234130975](figures/zh-cn_image_0000001234130975.png)
Z
zengyawen 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219


## 添加事件

Stepper分别添加finish,change,next,back,skip事件。

- 当change与next或back同时存在时,会先执行next或back事件再去执行change事件。

- 重新设置index属性值时要先清除index的值再重新设置,否则检测不到值的改变。

```
<!-- index.hml -->
<div class="container"  style="background-color:#F1F3F5;">
  <div >
    <stepper onfinish="stepperFinish" onchange="stepperChange" onnext="stepperNext" onback="stepperBack" onskip="stepperSkip" id="stepperId" index="{{index}}">
      <stepper-item>
        <text>stepper-item1</text>
        <button value="skip" onclick="skipClick"></button>
      </stepper-item>
      <stepper-item>
         <text>stepper-item2</text>
         <button value="skip" onclick="skipClick"></button>
      </stepper-item>
      <stepper-item>
        <text>stepper-item3</text>
      </stepper-item>
    </stepper>
  </div>
</div>
```

```
/* xxx.css */
.doc-page {
W
wangshuainan1 已提交
220 221
  width:100%;
  height:100%;
Z
zengyawen 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
stepper-item{
  width: 100%;
  flex-direction: column;
  align-self: center;
  justify-content: center;
}
text{
  margin-top: 45%;
  justify-content: center;
  align-self: center;
  margin-bottom: 50px;
}
button{
  width: 80%;
  height: 60px;
  margin-top: 20px;
}
```

```
/* index.js */
import prompt from '@system.prompt';
export default {
  data: {
    index:0,
  },
   stepperSkip(){
    this.index = null;
    this.index=2;
  },
   skipClick(){
    this.$element('stepperId').setNextButtonStatus({status: 'skip', label: 'SKIP'});
  },
  stepperFinish(){
    prompt.showToast({
      message: 'All Finished'
    })
  },
  stepperChange(e){
    console.log("stepperChange"+e.index)
    prompt.showToast({
      message: 'Previous step: '+e.prevIndex+"-------Current step:"+e.index
    })
  },
  stepperNext(e){
    console.log("stepperNext"+e.index)
    prompt.showToast({
      message: 'Current step:'+e.index+"-------Next step:"+e.pendingIndex
    })
    var index = {pendingIndex:e.pendingIndex }
    return index;
  },
  stepperBack(e){
    console.log("stepperBack"+e.index)
    var index = {pendingIndex: e.pendingIndex }
    return index;
  }
}
```

Z
zengyawen 已提交
286
![zh-cn_image_0000001189089950](figures/zh-cn_image_0000001189089950.gif)
Z
zengyawen 已提交
287 288 289 290 291 292


## 场景示例

在本场景中,开发者可以在界面上点击选择并实时显示选择结果,点击下一步按钮后可动态修改页面的字体颜色和字体大小。

Z
zengyawen 已提交
293
用Stepper组件实现分步,再创建[Toggle](../reference/arkui-js/js-components-basic-toggle.md)组件实现选择显示功能,再使用[Select](../reference/arkui-js/js-components-basic-select.md)组件实现改变选中值动态修改字体颜色或大小。
Z
zengyawen 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338

```
<div class="container">
  <stepper id="mystep" index="0" onfinish="back" style="text-color: indigo;">
    <stepper-item label="{{label1}}">
      <div style="flex-direction: column;padding: 0px 10px;">
        <text class="text" style="margin-top: 10%;text-align: center;width: 100%;">Select error types:</text>
        <text style="margin-top: 20px;padding: 10px">
          <span>{{error}}</span>
        </text>
        <div style="justify-content: space-around;flex-wrap: wrap;">
          <toggle for="{{togglelist1}}" value="{{$item}}" class="tog" onchange="multiTog({{$item}})"></toggle>
        </div>
      </div>
    </stepper-item>
    <stepper-item label="{{label2}}">
      <div style="flex-direction: column;align-items: center;">
        <text class="txt" style="margin-top: 10%;">Toggle</text>
        <div style="justify-content: space-around;flex-wrap: wrap;;margin-top:10%">
          <toggle class="tog" for="{{togglelist1}}" value="{{$item}}" style="text-color: {{tcolor}};font-size: {{tsize}}; font-style: {{tstyle}};font-weight: {{tweight}};font-family: {{tfamily}};">
          </toggle>
        </div>
        <div style="flex-wrap: wrap;width: 700px;margin-top:10%">
          <div style="flex-direction: column;width: 350px;height: 185px;align-items: center;">
            <text class="txt">text-color</text>
            <select onchange="settcolor">
              <option for="{{color_list}}" value="{{$item}}">{{$item}}</option>
            </select>
          </div>
          <div style="flex-direction: column;width: 350px;height: 185px;align-items: center;">
            <text class="txt">font-size</text>
            <select onchange="settsize">
              <option for="{{size_list}}" value="{{$item}}">{{$item}}</option>
            </select>
          </div>
        </div>
      </div>
    </stepper-item>
  </stepper>
</div>
```

```
/* xxx.css */
.container {
W
wangshuainan1 已提交
339 340
  width:100%;
  height:100%;
Z
zengyawen 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color:#F1F3F5;
}
.dvd {
  stroke-width: 8px;
  color: orangered;
  margin: 65px;
}
.tog{
  margin-right: 20px;
  margin-top: 30px;
}
```

```
/* index.js */
import prompt from '@system.prompt';
import router from '@system.router';
let myset = new Set();
export default {
  data: {
    error: '',
    tcolor:'#FF4500',
    color_list:['#FF4500','#5F9EA0','#0000FF'],
    tsize: '12px',
    size_list: ['12px', '30px', '8px', '50px'],
    label1: {
      prevLabel: 'The text on the left of the starting step is invalid.',
      nextLabel: 'Toggle'
    },
    label2: {
      prevLabel: 'toggle',
      nextLabel: 'END'
    },
    togglelist1:['Program error', 'Software', 'System', 'Application'],
  },
  multiTog(arg, e) {
    this.error = ' '
    if (e.checked) {
      myset.add(arg)
    } else {
      myset.delete(arg)
    }
    for (let item of myset) {
      this.error += item + ' '
    }
  },
  settcolor(e) {
    this.tcolor = e.newValue
  },
  settsize(e) {
    this.tsize = e.newValue
  }
}
```

Z
zengyawen 已提交
399
![zh-cn_image_0000001189249862](figures/zh-cn_image_0000001189249862.gif)