ui-js-components-stepper.md 9.3 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
  flex-direction: column;
W
wangshuainan 已提交
75
  background-color: #F1F3F5;
Z
zengyawen 已提交
76 77 78 79 80 81 82 83
}
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
<!-- index.hml -->
W
wangshuainan 已提交
90
<div class="container"> 
Z
zengyawen 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
 <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
  flex-direction: column;
W
wangshuainan 已提交
114
  background-color: #F1F3F5;
Z
zengyawen 已提交
115 116 117 118 119 120 121 122 123 124 125 126
}
text{
  width: 100%;
  height: 100%;
  text-align: center;
}
```

```
/* index.js */
export default { 
  data: {
W
wangshuainan 已提交
127 128 129 130
    label_1:{
      nextLabel: 'NEXT',      
      status: 'normal'    
    },
Z
zengyawen 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    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 已提交
152 153
<!-- index.hml -->
<div class="container" > 
Z
zengyawen 已提交
154
  <div class="stepperContent">
Z
zengyawen 已提交
155 156 157 158 159
    <stepper class="stepperClass">    
      <stepper-item>     
        <text>stepper-item1</text>
      </stepper-item> 
    </stepper> 
Z
zengyawen 已提交
160 161 162 163 164 165 166
  </div>
</div>
```

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

Z
zengyawen 已提交
189
![zh-cn_image_0000001234130975](figures/zh-cn_image_0000001234130975.png)
Z
zengyawen 已提交
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 220 221 222 223


## 添加事件

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 已提交
224 225
  width:100%;
  height:100%;
Z
zengyawen 已提交
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 286 287 288 289
  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 已提交
290
![zh-cn_image_0000001189089950](figures/zh-cn_image_0000001189089950.gif)
Z
zengyawen 已提交
291 292 293 294 295 296


## 场景示例

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

Z
zengyawen 已提交
297
用Stepper组件实现分步,再创建[Toggle](../reference/arkui-js/js-components-basic-toggle.md)组件实现选择显示功能,再使用[Select](../reference/arkui-js/js-components-basic-select.md)组件实现改变选中值动态修改字体颜色或大小。
Z
zengyawen 已提交
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 339 340 341 342

```
<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 已提交
343 344
  width:100%;
  height:100%;
Z
zengyawen 已提交
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 399 400 401 402
  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 已提交
403
![zh-cn_image_0000001189249862](figures/zh-cn_image_0000001189249862.gif)
404 405 406 407 408 409


## 相关实例

针对Stepper开发,有以下相关实例可供参考:

410
- [`StepNavigator`:步骤导航器(JS)(API7)](https://gitee.com/openharmony/app_samples/tree/master/UI/StepNavigator)