ui-js-components-stepper.md 9.8 KB
Newer Older
E
ester.zhou 已提交
1
# \<stepper> Development
Z
zengyawen 已提交
2

3
When multiple steps are required to complete a task, you can use the **\<stepper>** component to navigate your users through the whole process. For details, see [stepper](../reference/arkui-js/js-components-container-stepper.md).
Z
zengyawen 已提交
4 5


E
ester.zhou 已提交
6
> **NOTE**
E
ester.zhou 已提交
7
>
G
ge-yafang 已提交
8 9 10
> This component is supported since API version 5.


E
ester.zhou 已提交
11
## Creating a \<stepper> Component
G
ge-yafang 已提交
12

E
ester.zhou 已提交
13
Create a **\<stepper>** component in the .hml file under **pages/index**.
G
ge-yafang 已提交
14

E
ester.zhou 已提交
15 16
```html
<!-- xxx.hml -->
Z
zengyawen 已提交
17
<div class="container"> 
E
ester.zhou 已提交
18 19
 <stepper>    
   <stepper-item>     
Z
zengyawen 已提交
20 21 22 23 24
     <text>Step 1</text>
   </stepper-item> 
   <stepper-item>     
     <text>Step 2</text>
   </stepper-item> 
E
ester.zhou 已提交
25
 </stepper> 
Z
zengyawen 已提交
26 27 28
</div>
```

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

G
ge-yafang 已提交
46 47
![en-us_image_0000001223287656](figures/en-us_image_0000001223287656.gif)

Z
zengyawen 已提交
48

G
ge-yafang 已提交
49 50
## Setting the Index

H
HelloCrease 已提交
51
Set **index** to the index value of the step that you want to display by default.
Z
zengyawen 已提交
52

E
ester.zhou 已提交
53 54
```html
<!-- xxx.hml -->
Z
zengyawen 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
<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>
```

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

G
ge-yafang 已提交
85 86
![en-us_image_0000001267767837](figures/en-us_image_0000001267767837.gif)

E
ester.zhou 已提交
87
Set the **label** attribute to customize the label for the **\<stepper-item>**.
Z
zengyawen 已提交
88

E
ester.zhou 已提交
89 90 91
```html
<!-- xxx.hml -->
<div class="container"> 
Z
zengyawen 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
 <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>
```

E
ester.zhou 已提交
109 110
```css
/* xxx.css */
E
ester.zhou 已提交
111 112 113
.container {  
  width:100%;
  height:100%;
Z
zengyawen 已提交
114
  flex-direction: column;
E
ester.zhou 已提交
115
  background-color: #F1F3F5;
Z
zengyawen 已提交
116 117 118 119 120 121 122 123
}
text{
  width: 100%;
  height: 100%;
  text-align: center;
}
```

E
ester.zhou 已提交
124 125
```js
// xxx.js
Z
zengyawen 已提交
126 127
export default { 
  data: {
E
ester.zhou 已提交
128 129 130
    label_1:{
      nextLabel: 'NEXT',      
      status: 'normal'    
H
HelloCrease 已提交
131
    },
Z
zengyawen 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145
    label_2:{
      prevLabel: 'BACK',
      nextLabel: 'NEXT',
      status: 'normal'
    },
    label_3:{
      prevLabel: 'BACK',
      nextLabel: 'END',
      status: 'disabled'
    },
  },
}
```

G
ge-yafang 已提交
146
![en-us_image_0000001267767841](figures/en-us_image_0000001267767841.gif)
Z
zengyawen 已提交
147 148


H
HelloCrease 已提交
149
## Setting Styles
G
ge-yafang 已提交
150

E
ester.zhou 已提交
151
By default, the **\<stepper>** component fills entire space of its container. The sample code below shows how to set the border and background color using the **border** and **background-color** attributes.
E
ester.zhou 已提交
152 153
```html
<!-- xxx.hml -->
Z
zengyawen 已提交
154 155 156 157 158 159 160 161 162 163 164
<div class="container" > 
  <div class="stepperContent">
    <stepper class="stepperClass">    
      <stepper-item>     
        <text>stepper-item1</text>
      </stepper-item> 
    </stepper> 
  </div>
</div>
```

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

G
ge-yafang 已提交
190
![en-us_image_0000001223287668](figures/en-us_image_0000001223287668.png)
Z
zengyawen 已提交
191 192


G
ge-yafang 已提交
193
## Adding Events
Z
zengyawen 已提交
194

E
ester.zhou 已提交
195
The **\<stepper>** component supports the **finish**, **change**, **next**, **back**, and **skip** events.
G
ge-yafang 已提交
196

H
HelloCrease 已提交
197
- When the **change** and **next** or **back** events exist at the same time, the **next** or **back** event is executed before the **change** event.
G
ge-yafang 已提交
198

H
HelloCrease 已提交
199
- Before resetting the **index** attribute, you must remove the current value. Otherwise, the value change cannot be detected.
Z
zengyawen 已提交
200

E
ester.zhou 已提交
201 202
```html
<!-- xxx.hml -->
Z
zengyawen 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
<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>
```

E
ester.zhou 已提交
222
```css
Z
zengyawen 已提交
223 224
/* xxx.css */
.doc-page {
E
ester.zhou 已提交
225 226
  width:100%;
  height:100%;
Z
zengyawen 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
  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;
}
```

E
ester.zhou 已提交
250 251
```js
// xxx.js
E
esterzhou 已提交
252
import promptAction from '@ohos.promptAction';
Z
zengyawen 已提交
253 254 255 256 257 258 259 260 261 262 263 264
export default {
  data: {
    index:0,
  },
   stepperSkip(){
    this.index = null;
    this.index=2;
  },
   skipClick(){
    this.$element('stepperId').setNextButtonStatus({status: 'skip', label: 'SKIP'});
  },
  stepperFinish(){
E
esterzhou 已提交
265
    promptAction.showToast({
Z
zengyawen 已提交
266 267 268 269 270
      message: 'All Finished'
    })
  },
  stepperChange(e){
    console.log("stepperChange"+e.index)
E
esterzhou 已提交
271
    promptAction.showToast({
E
ester.zhou 已提交
272
      // index indicates the sequence number of the current step.
Z
zengyawen 已提交
273 274 275 276 277
      message: 'Previous step: '+e.prevIndex+"-------Current step:"+e.index
    })
  },
  stepperNext(e){
    console.log("stepperNext"+e.index)
E
esterzhou 已提交
278
    promptAction.showToast({
E
ester.zhou 已提交
279
      // pendingIndex indicates the sequence number of the step to be redirected to.
Z
zengyawen 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292
      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;
  }
}
```

G
ge-yafang 已提交
293 294
![en-us_image_0000001267607869](figures/en-us_image_0000001267607869.gif)

Z
zengyawen 已提交
295

G
ge-yafang 已提交
296
## Example Scenario
Z
zengyawen 已提交
297

E
ester.zhou 已提交
298
In this example, you can select the options displayed on the page and see how your selection takes effect in real time. Clicking the next button will dynamically change the font color and font size of the selected option.
Z
zengyawen 已提交
299

E
ester.zhou 已提交
300
Use a **\<stepper>** component to navigate through the steps. Create a **\<Toggle>**(../reference/arkui-js/js-components-basic-toggle.md) component to implement the functions of selecting an option and displaying the selection result. Then use the **\<Select>**(../reference/arkui-js/js-components-basic-select.md) component to dynamically change the font color or size of the selected option.
G
ge-yafang 已提交
301

E
ester.zhou 已提交
302 303
```html
<!-- xxx.hml -->
Z
zengyawen 已提交
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 343
<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>
```

E
ester.zhou 已提交
344
```css
Z
zengyawen 已提交
345 346
/* xxx.css */
.container {
E
ester.zhou 已提交
347 348
  width:100%;
  height:100%;
Z
zengyawen 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
  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;
}
```

E
ester.zhou 已提交
365 366
```js
// xxx.js
E
esterzhou 已提交
367 368
import promptAction from '@ohos.promptAction';
import router from '@ohos.router';
Z
zengyawen 已提交
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 403 404 405 406
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
  }
}
```

E
ester.zhou 已提交
407
![en-us_image_0000001267887817](figures/en-us_image_0000001267887817.gif)