目录

steps

Overview

The steps element displays progress through a sequence of logical steps and it is often used for multiple scenarios such as online shopping and logistics. This element has the following features:

The following figure shows the effect.

The basic layout code is as follows:

<template>
  <div class="container">
    <text class="section">Horizontal arrangement</text>
    <steps class="stepcss" options="{{list2}}" active-color="#007AFF" active="{{active}}" direction="row"></steps>
    <text class="section">Vertical arrangement</text>
    <steps if="{{isShow}}}" class="stepcss" options="{{list2}}" active-color="#007AFF" active="{{active}}" direction="column"></steps>
  </div>
</template>

Development Procedure

[h2]Customizing the Subelements

  1. Design the layout.

    The element UI is universal, but the content layout is not. The content layout cannot be fixed. Otherwise, once the UI of the content part changes, the subelements have to be modified, which violates the open-closed principle in programming.

    Therefore, multiple style attributes are supported in steps.ux for developers to customize the style.

    <template>
      <div class="rootcontainer">
        <block if="{{direction=='column'}}">
          <div style="width: 100%;">
            <!-- Vertical split lines. -->
            <div style="flex-direction: column; align-items: center; padding-top: 16px; width: 60px;">
              <div if="{{computed}}" class="steps_column-line-item" for="{{listoptions}}">
                <image if="$idx==active" class="steps_column-imgcheck"></image>
                <image else class="steps_column-imgcircle"></image>
                <div if="$idx!=options.length-1 && computed" class="steps_column-line" style="height: {{$item.lineHeight}}px;"></div>
                 <!-- <div if="$idx!=options.length-1 && computed" class="steps_column-line" style="height:100%;"></div> -->
              </div>
            </div>
    
            <!-- Steps arranged vertically. -->
            <div style="flex-direction: column; width: 100%;">
              <block for="{{listoptions}}">
                <div if="$idx==active" class="steps_column_text_div" id="{{$item.itemId}}">
                  <text class="steps_txt_state" style="color:{{activeColor}};">{{ $item.title }}</text>
                  <text class="steps_txt_des" style="color:{{activeColor}};">{{ $item.desc }}</text>
                  <text class="steps_txt_time" style="color:{{activeColor}};">{{ $item.time }}</text>
                  <text class="steps_row-line"></text>
                </div>
                <div else class="steps_column_text_div" id="{{$item.itemId}}">
                  <text class="steps_txt_state" style="color:{{deactiveColor}};">{{ $item.title }}</text>
                  <text class="steps_txt_des" style="color:{{deactiveColor}};">{{ $item.desc }}</text>
                  <text class="steps_txt_time" style="color:{{deactiveColor}};">{{ $item.time }}</text>
                  <text class="steps_row-line"></text>
                </div>
              </block>
            </div>
          </div>
        </block>
    
        <block else>
          <!-- Horizontal arrangement. -->
          <div id="rowroot" style="flex-direction: column;width: 100%;">
            <!-- Steps arranged horizontally. -->
            <div class="steps_row">
              <div class="steps_row_text_div" style="width: {{row_list_item_width}}px;" id="{{$item.itemId}}" for="{{listoptions}}">
                <block if="$idx==active">
                  <text class="steps_txt_state" style="color:{{activeColor}};">{{ $item.title }}</text>
                  <text class="steps_txt_des" style="color:{{activeColor}};">{{ $item.desc }}</text>
                  <text class="steps_txt_time" style="color:{{activeColor}};">{{ $item.time }}</text>
                </block>
    
                <block else>
                  <text class="steps_txt_state" style="color:{{deactiveColor}};">{{ $item.title }}</text>
                  <text class="steps_txt_des" style="color:{{deactiveColor}};">{{ $item.desc }}</text>
                  <text class="steps_txt_time" style="color:{{deactiveColor}};">{{ $item.time }}</text>
                </block>
              </div>
            </div>
    
            <!-- Layout of the split lines.-->
            <div class="steps_row_line_root" style=" margin-left: {{lineleft}}px; margin-right:{{lineleft}}px;" >
              <div if="{{computed}}" class="steps_row-line-item" for="{{listoptions}}">
                <image if="$idx==active" class="steps_column-imgcheck"></image>
                <image else class="steps_column-imgcircle"></image>
                <block if="$idx!=options.length-1">
                  <div class="steps_column-line" style="width:{{row_list_item_width+5}}px; height: 1px;"></div>
                </block>
              </div>
            </div>
          </div>
        </block>
      </div>
    </template>
  2. Customize the attributes and supported events.
    Supported attributes

    Attribute

    Type

    Default Value

    Description

    direction

    String

    column

    Arrangement direction of steps.

    options

    Array

    -

    Step list.

    active

    Number

    0

    Current step.

    activeColor

    Color

    #1aad19

    Color of the current step.

    deactiveColor

    Color

    #999999

    Color of the steps excluding the current step.

    Supported event

    None

[h2]Horizontal Arrangement

  1. The horizontal progress bar is placed under steps.
  2. Arrange the steps and progress bar horizontally.
  3. Use the for loop to form a list layout.
  4. Center align the gray dot that indicates the start point of the event and then calculate the distance between the progress bar and two edges of the root node.
  5. The distance between two dots should be the same as each step in length. So you need to calculate the total width of all steps first and then divide it by the step quantity.
    calRowItemWidth(len) {
      var that = this;
      this.$element('rowroot').getBoundingClientRect({
        success: function (data) {
          console.log('calRowItemWidth  getBoundingClientRect succeeded, result data =' 
          + JSON.stringify(data));
          that.row_list_item_width = data.width / len ;
          that.lineleft = that.row_list_item_width / 2 ;
          console.info("calRowItemWidth= " + that.row_list_item_width);
          that.computed = true;
        },
        fail() {
          console.log('getBoundingClientRect failed');
        },
        complete: function () {
          console.log('getBoundingClientRect completed');
        }
      });
    }

[h2]Vertical Arrangement

  1. The vertical progress bar is placed on one side of steps.
  2. Arrange the steps and progress bar vertically.
  3. Use the for loop to form a list layout.
  4. The height of the progress bar is determined by the step description size on the right. To ensure that each dot on the progress bar is placed at the first line of a step, you can use the getBoundingClientRect() method of element to obtain the width of each step and assign the value to the corresponding variables.
    calLineHeight(optionItem) {
      var that = this;
      this.$element(optionItem.itemId).getBoundingClientRect({
        success: function (data) {
          console.log('getBoundingClientRect succeeded, result data =' 
          + JSON.stringify(data));
          let lineHeight = data.height - 10;
          optionItem.lineHeight = lineHeight;
          that.computed = true;
        },
        fail() {
          console.log('getBoundingClientRect failed');
        },
        complete: function () {
          console.log('getBoundingClientRect completed');
        }
      });
    },

    The getBoundingClientRect() method can calculate the length and width of an element only after the page is rendered. When it is called during page lifecycle, the delay is about 200 ms.

The width of each step and progress line is not fixed, but dynamically adapted based on the event content. When the widths are calculated, the page will be refreshed. This is because the length calculation of the progress line starts after the page is rendered.

If the description of each step is not long, you can fix the width so the calculation can be skipped.

Sample Code

step.ux file:

<template>
  <div class="rootcontainer">
    <block if="{{direction=='column'}}">
      <div style="width: 100%;">
        <!-- Vertical split lines. -->
        <div style="flex-direction: column; align-items: center; padding-top: 16px; width: 60px;">
          <div if="{{computed}}" class="steps_column-line-item" for="{{listoptions}}">
            <image if="$idx==active" class="steps_column-imgcheck"></image>
            <image else class="steps_column-imgcircle"></image>
            <div if="$idx!=options.length-1 && computed" class="steps_column-line" style="height: {{$item.lineHeight}}px;"></div>
             <!-- <div if="$idx!=options.length-1 && computed" class="steps_column-line" style="height:100%;"></div> -->
          </div>
        </div>

        <!-- Steps arranged vertically. -->
        <div style="flex-direction: column; width: 100%;">
          <block for="{{listoptions}}">
            <div if="$idx==active" class="steps_column_text_div" id="{{$item.itemId}}">
              <text class="steps_txt_state" style="color:{{activeColor}};">{{ $item.title }}</text>
              <text class="steps_txt_des" style="color:{{activeColor}};">{{ $item.desc }}</text>
              <text class="steps_txt_time" style="color:{{activeColor}};">{{ $item.time }}</text>
              <text class="steps_row-line"></text>
            </div>
            <div else class="steps_column_text_div" id="{{$item.itemId}}">
              <text class="steps_txt_state" style="color:{{deactiveColor}};">{{ $item.title }}</text>
              <text class="steps_txt_des" style="color:{{deactiveColor}};">{{ $item.desc }}</text>
              <text class="steps_txt_time" style="color:{{deactiveColor}};">{{ $item.time }}</text>
              <text class="steps_row-line"></text>
            </div>
          </block>
        </div>
      </div>
    </block>	

    <block else>
      <!-- Horizontal arrangement. -->
      <div id="rowroot" style="flex-direction: column;width: 100%;">
        <!-- Steps arranged horizontally. -->
        <div class="steps_row">
          <div class="steps_row_text_div" style="width: {{row_list_item_width}}px;" id="{{$item.itemId}}" for="{{listoptions}}">
            <block if="$idx==active">
              <text class="steps_txt_state" style="color:{{activeColor}};">{{ $item.title }}</text>
              <text class="steps_txt_des" style="color:{{activeColor}};">{{ $item.desc }}</text>
              <text class="steps_txt_time" style="color:{{activeColor}};">{{ $item.time }}</text>
            </block>

            <block else>
              <text class="steps_txt_state" style="color:{{deactiveColor}};">{{ $item.title }}</text>
              <text class="steps_txt_des" style="color:{{deactiveColor}};">{{ $item.desc }}</text>
              <text class="steps_txt_time" style="color:{{deactiveColor}};">{{ $item.time }}</text>
            </block>
          </div>
        </div>

        <!-- Layout of the split lines.-->
        <div class="steps_row_line_root" style=" margin-left: {{lineleft}}px; margin-right:{{lineleft}}px;" >
          <div if="{{computed}}" class="steps_row-line-item" for="{{listoptions}}">
            <image if="$idx==active" class="steps_column-imgcheck"></image>
            <image else class="steps_column-imgcircle"></image>
            <block if="$idx!=options.length-1">
              <div class="steps_column-line" style="width:{{row_list_item_width+5}}px; height: 1px;"></div>
            </block>
          </div>
        </div>
      </div>
    </block>
  </div>
</template>

<style>
  .rootcontainer {
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 100%;
  }
  .steps_column {
    display: flex;
    flex-direction: row;
    justify-content: center;
    padding-top: 16px;
  }
  .steps_column-line-item {
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }

  .steps_column_text_div {
    flex-direction: column;
    align-items: flex-start;
    width: 100%;
    padding-top: 16px;
    padding-left: 30px;
  }

  .steps_row-line {
    height: 1px;
    width: 100%;
    margin-top: 20px;
    background-color: #999;
  }

  .steps_column-line {
    width: 1px;
    background-color: #999;
  }
  .steps_row-circle {
    width: 10px;
    height: 10px;
    border-radius: 1px;
    background-color: #999;
  }

  .steps_column-imgcircle {
    width: 10px;
    height: 10px;
    border-radius: 5px;
    background-color: #999;
  }

  .steps_column-imgcheck {
    width: 30px;
    height: 30px;
    border-radius: 15px;
    background-color: #00bfff;
  }

  .steps_txt_state {
    font-size: 30px;
    font-weight: bold;
  }

  .steps_txt_des {
    font-size: 25px;
    margin-top: 6px;
  }

  .steps_txt_time {
    font-size: 25px;
    margin-top: 6px;
  }

  .steps_row {
    display: flex;
    flex-direction: row;
    justify-content: center;
  }

  .steps_row_text_div {
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    width: 100%;
  }
  .steps_row-line-item {
    flex-direction: row;
    align-items: center;
    justify-content: center;
  }

  .steps_row_line_root {
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
  }
</style>

<script>

  module.exports = {
    props: {
      direction: {
        // Arrangement direction of steps. The value can be row or column.
        type: String,
        default: 'column'
      },
      activeColor: {
        // Color of the step when it is activated.
        type: String,
        default: '#1aad19'
      },
      deactiveColor: {
        // Color of the steps excluding the current step.
        type: String,
        default: '#999999'
      },
      active: {
        // Current step.
        type: Number,
        default: 0
      },
      options: {
        type: Array,
        default() {
          return []
        }
      }
    },
    data() {
      return {
        txtColor: '#1aad19',
        listoptions: [],
        lineoptions: [],
        row_list_item_width: 200,
        lineleft: 0,
        computed: false
      }
    },
    onInit() {
      this.initListOptions();
      setTimeout(() => {
        this.cal();
      }, 200);

    },

    initListOptions() {
      let len = this.options.length;
      for (var i = 0; i < len; i++) {
        let item = {};
        item.desc = this.options[i].desc;
        item.title = this.options[i].title;
        item.time = this.options[i].time;
        item.itemId = "item" + i;
        item.lineHeight = 0;
        this.listoptions.push(item);
      }
    },
    cal() {
      let len = this.listoptions.length;
      if (this.direction == "column") {
        for (var i = 0; i < len; i++) {
          let optionItem = this.listoptions[i];
          this.calLineHeight(optionItem);
        }
      } else {
        this.calRowItemWidth(len);
      }
    },

    calLineHeight(optionItem) {
      var that = this;
      this.$element(optionItem.itemId).getBoundingClientRect({
        success: function (data) {
          console.log('getBoundingClientRect succeeded, result data =' 
          + JSON.stringify(data));
          let lineHeight = data.height - 10;
          optionItem.lineHeight = lineHeight;
          that.computed = true;
        },
        fail() {
          console.log('getBoundingClientRect failed');
        },
        complete: function () {
          console.log('getBoundingClientRect completed');
        }
      });
    },
    calRowItemWidth(len) {
      var that = this;
      this.$element('rowroot').getBoundingClientRect({
        success: function (data) {
          console.log('calRowItemWidth  getBoundingClientRect succeeded, result data =' 
          + JSON.stringify(data));
          that.row_list_item_width = data.width / len;
          that.lineleft = that.row_list_item_width / 2;
          console.info("calRowItemWidth= " + that.row_list_item_width);
          that.computed = true;
        },
        fail() {
          console.log('getBoundingClientRect failed');
        },
        complete: function () {
          console.log('getBoundingClientRect completed');
        }
      });
    }
  }
</script>

hello.ux file:

<import name="steps" src="../Steps/steps.ux"></import>
<template>
  <div class="container">
    <text class="section">Horizontal arrangement</text>
    <steps class="stepcss" options="{{list2}}" active-color="#007AFF" active="{{active}}" direction="row"></steps>
    <text class="section">Vertical arrangement</text>
    <steps if="{{isShow}}}" class="stepcss" options="{{list2}}" active-color="#007AFF" active="{{active}}" direction="column"></steps>
  </div>
</template>

<style>
  .container {
    flex-direction: column;
    align-items: center;
    padding-left: 20px;
    padding-right: 20px;
    padding-top: 16px;
  }

  .section {
    background-color: #00bfff;
    width: 100%;
    height: 80px;
    margin-top: 25px;
  }

  .stepcss {
    padding-left: 30px;
    padding-right: 30px;
    width: 100%;
  }
</style>

<script>
  module.exports = {
    data() {
      return {
        isShow: true,
        active: 1,
        list1: [{
          title: 'Step 1',
          desc: '2020-11-11'
        }, {
          title: 'Step 2',
          desc: '2020-11-12'
        }, {
          title: 'Step 2',
          desc: '2020-11-12'
        },
        {
          title: 'Step 3',
          desc: '2020-11-12'
        },
        {
          title: 'Step 4',
          desc: '2020-11-12'
        },
        {
          title: 'Step 5',
          desc: '2020-11-12'
        }, {
          title: 'Step 6',
          desc: '2020-11-12'
        }],
        list11: [{
          title: 'Step 1',
          desc: '2020-11-11'
        }, {
          title: 'Step 2',
          desc: '2020-11-12'
        }, {
          title: 'Step 2',
          desc: '2020-11-12'
        },
        {
          title: 'Step 3',
          desc: '2020-11-12'
        },
        {
          title: 'Step 4',
          desc: '2020-11-12'
        }],
        list2: [{
          title: 'Finished',
          desc: 'This is a description.',
          time: '2020-11-11'
        }, {
          title: 'In progress',
          desc: 'This is a description.',
          time: '2020-11-12'
        }, {
          title: 'Waiting',
          desc: 'This is a description.',
          time: '2020-11-13'
         }, 
        {
          title: 'Waiting',
          desc: 'This is a description.',
          time: '2020-11-14'
        }, {
          title: 'Waiting',
          desc: 'This is a description.',
          time: '2020-11-14 17:00'
        }
        ]
      }
    },
    onInit() {
      console.info("hello onInit ");
      this.$page.setTitleBar({
        text: 'steps',
        textColor: '#ffffff',
        backgroundColor: '#007DFF',
        backgroundOpacity: 0.5,
        menu: true
      });
    },

    onShow(options) {
      console.info("hello onShow ");
    },
    deal: function () {
      this.isShow = !this.isShow;
      console.info(" this.isShow=" + this.isShow);
    }
  }
</script>