element-ui Steps步驟條組件源碼分析整理筆記(九)

来源:https://www.cnblogs.com/fangnianqin/archive/2018/12/12/10107581.html
-Advertisement-
Play Games

Steps步驟條組件源碼: steps.vue step.vue ...


Steps步驟條組件源碼:

steps.vue

<template>
    <!--設置 simple 可應用簡潔風格,該條件下 align-center / description / direction / space 都將失效。-->
  <div
    class="el-steps"
    :class="[
       !simple && 'el-steps--' + direction,
       simple && 'el-steps--simple'
     ]">
      <slot></slot>
  </div>
</template>

<script>
import Migrating from 'element-ui/src/mixins/migrating';

export default {
  name: 'ElSteps',

  mixins: [Migrating],

  props: {
    space: [Number, String], //每個 step 的間距,不填寫將自適應間距。支持百分比。
    active: Number, //設置當前激活步驟
    direction: {  //顯示方向
      type: String,
      default: 'horizontal'
    },
    alignCenter: Boolean, //進行居中對齊
    simple: Boolean, // 是否應用簡潔風格
    finishStatus: { //設置結束步驟的狀態
      type: String,
      default: 'finish'
    },
    processStatus: { //設置當前步驟的狀態
      type: String,
      default: 'process'
    }
  },

  data() {
    return {
      steps: [], //記錄步驟數數組
      stepOffset: 0
    };
  },

  methods: {
    //  屬性遷移
    getMigratingConfig() {
      return {
        props: {
          'center': 'center is removed.'
        }
      };
    }
  },

  watch: {
    active(newVal, oldVal) {
      // 當前激活步驟改變時,觸發父組件的change方法,將改變前和改變後的步驟作為參數傳遞出去
      this.$emit('change', newVal, oldVal);
    },

    steps(steps) {
      steps.forEach((child, index) => {
        child.index = index;
      });
    }
  }
};
</script>

step.vue

<template>
    <!--每一步驟的最外層包裹div-->
  <div
    class="el-step"
    :style="style"
    :class="[
      !isSimple && `is-${$parent.direction}`,
      isSimple && 'is-simple',
      isLast && !space && !isCenter && 'is-flex',
      isCenter && !isVertical && !isSimple && 'is-center'
     ]">
    <!-- 步驟的數字圖標和步驟條直線 -->
    <div class="el-step__head" :class="`is-${currentStatus}`">
        <!--步驟條直線-->
        <!--如果是最後一步,margin-right不存在;如果不是,則為0-->
      <div class="el-step__line" :style="isLast ? '' : { marginRight: $parent.stepOffset + 'px' }">
        <i class="el-step__line-inner" :style="lineStyle"></i>
      </div>
        <!--步驟條的數字圖標-->
      <div class="el-step__icon" :class="`is-${icon ? 'icon' : 'text'}`">
          <!--如果當前狀態為:wait、process、finish-->
        <slot v-if="currentStatus !== 'success' && currentStatus !== 'error'" name="icon">
            <!--如果是圖標則顯示對應的圖標-->
          <i v-if="icon" class="el-step__icon-inner" :class="[icon]"></i>
            <!--如果圖標和未設置isSimple簡潔風格時,則顯示步驟文字-->
          <div class="el-step__icon-inner" v-if="!icon && !isSimple">{{ index + 1 }}</div>
        </slot>
        <!--如果當前狀態為:success、error-->
        <i v-else :class="['el-icon-' + (currentStatus === 'success' ? 'check' : 'close')]" class="el-step__icon-inner is-status"></i>
      </div>
    </div>
    <!-- 步驟條下麵每一步的標題和描述 -->
    <div class="el-step__main">
        <!--每一步的標題-->
      <div class="el-step__title" ref="title" :class="['is-' + currentStatus]">
        <slot name="title">{{ title }}</slot>
      </div>
        <!--簡潔模式下會有>圖標-->
      <div v-if="isSimple" class="el-step__arrow"></div>
        <!--每一步的描述-->
      <div v-else class="el-step__description" :class="['is-' + currentStatus]">
        <slot name="description">{{ description }}</slot>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ElStep',

  props: {
    title: String, //標題
    icon: String, //圖標
    description: String, //描述性文字
    status: String //設置當前步驟的狀態,不設置則根據 steps 確定狀態。 wait(灰色)/ process(黑色)/ finish(藍色)/ error / success(綠色)
  },

  data() {
    return {
      index: -1,
      lineStyle: {}, //步驟條直線的樣式
      internalStatus: ''
    };
  },

  beforeCreate() {
    this.$parent.steps.push(this);
  },
  mounted() {
     const unwatch = this.$watch('index', val => {
        this.$watch('$parent.active', this.updateStatus, { immediate: true });
        unwatch();
     });
  },
  beforeDestroy() {
    const steps = this.$parent.steps;
    const index = steps.indexOf(this);
    if (index >= 0) {
      steps.splice(index, 1);
    }
  },

  computed: {
    // 返回當前步驟的狀態
    currentStatus() {
      return this.status || this.internalStatus;
    },
    prevStatus() {
      const prevStep = this.$parent.steps[this.index - 1];
      return prevStep ? prevStep.currentStatus : 'wait';
    },
    // 返回是否是居中對齊
    isCenter() {
      return this.$parent.alignCenter;
    },
    // 返回顯示的方向:豎直(false)或者水平(true)
    isVertical() {
      return this.$parent.direction === 'vertical';
    },
    // 返回是否應用簡潔風格
    isSimple() {
      return this.$parent.simple;
    },
    // 判斷當前是不是最後步驟
    isLast() {
      const parent = this.$parent;
      return parent.steps[parent.steps.length - 1] === this;
    },
    //  返回總步驟數
    stepsCount() {
      return this.$parent.steps.length;
    },
    // 返回每個step的間距。
    space() {
      const { isSimple, $parent: { space } } = this;
      // isSimple為true時,space將失效
      return isSimple ? '' : space ;
    },
    style: function() {
      const style = {};
      const parent = this.$parent;
      const len = parent.steps.length; //總步驟
      // 每個step的間距
      const space = (typeof this.space === 'number'  //如果設置的space是number
        ? this.space + 'px'   //space等於設置的space
        : this.space ? this.space : 100 / (len - (this.isCenter ? 0 : 1)) + '%'); //如果未設置space,未設置居中,則等於100除以(總步驟數-1);設置居中顯示,則等於00除以總步驟數。
      // flex-basis 屬性用於設置或檢索彈性盒伸縮基準值。
      style.flexBasis = space;
      //如果是水平方向則直接返回設置的樣式
      if (this.isVertical) return style;
      //如果是最後的步驟,設置最大寬度等於(100/總步驟數)%
      if (this.isLast) {
        style.maxWidth = 100 / this.stepsCount + '%';
      } else {
          //如果不是最後的步驟,marginRight為0
        style.marginRight = -this.$parent.stepOffset + 'px';
      }
      return style;
    }
  },

  methods: {
    updateStatus(val) {
      const prevChild = this.$parent.$children[this.index - 1];
      if (val > this.index) { //如果是下一步
        //  internalStatus 等於用戶設置的結束步驟狀態
        this.internalStatus = this.$parent.finishStatus;
      } else if (val === this.index && this.prevStatus !== 'error') {
          //  internalStatus 等於用戶設置的當前步驟狀態
        this.internalStatus = this.$parent.processStatus;
      } else {
        this.internalStatus = 'wait';
      }
      if (prevChild) prevChild.calcProgress(this.internalStatus);
    },
    //設置步驟間直線的樣式
    calcProgress(status) {
      let step = 100;
      const style = {};
      // transitionDelay在過渡效果開始前等待的秒數:
      style.transitionDelay = 150 * this.index + 'ms';
      if (status === this.$parent.processStatus) {
        step = this.currentStatus !== 'error' ? 0 : 0;
      } else if (status === 'wait') {
        step = 0;
        //為負數的時候過渡的動作會從該時間點開始顯示,之前的動作被截斷;為正數的時候過渡的動作會延遲觸發。
        style.transitionDelay = (-150 * this.index) + 'ms';
      }

      style.borderWidth = step ? '1px' : 0;
      this.$parent.direction === 'vertical'
        ? style.height = step + '%'
        : style.width = step + '%';

      this.lineStyle = style;

    }
  }


};
</script>

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • JavaScript是運行在客戶端的腳本,因此一般是不能夠設置Session的,因為Session是運行在伺服器端的。 而cookie是運行在客戶端的,所以可以用JS來設置cookie. 假設有這樣一種情況,在某個用例流程中,由A頁面跳至B頁面,若在A頁面中採用JS用變數temp保存了某一變數的值, ...
  • 本文由雲+社區發表 本文主要講述瞭如何一步步在生產環境上部署django和vue,操作系統預設為centos 說明 :後文中出現的以下字元串均表示具體的路徑或者名稱,含義如下: DJANGO_DIR 表示django的工程根目錄 DJANGO_NAME 表示django的工程名稱 VUE_HTML_ ...
  • Collapse 摺疊面板源碼: collapse.vue collapse item.vue ...
  • 嗷嗷方便的文字轉語音,不過用的時候記得到百度語音上申請key,免費的.之前在網路上看到有人寫了一部分,自己豐富下,以後用也方便 ...
  • 來自:https://blog.csdn.net/m0_37068028/article/details/72898154 侵刪 來自:https://segmentfault.com/a/1190000010378259 侵刪 第一種: //date.jsexport function forma ...
  • tofixed方法 四捨五入 toFixed() 方法可把 Number 四捨五入為指定小數位數的數字。例如將數據Num保留2位小數,則表示為:toFixed(Num);但是其四捨五入的規則與數學中的規則不同,使用的是銀行家舍入規則,銀行家舍入:所謂銀行家舍入法,其實質是一種四舍六入五取偶(又稱四舍 ...
  • 示例html代碼: 示例html代碼: 示例html代碼: 示例html代碼: 示例html代碼: <div id="test"> <span style="color:red">test1</span> test2 </div> 獲得id為test的DOM對象,下麵就不一一獲取了。 var tes ...
  • 面向對象的語言有一個標誌,那就是它們都有類的概念,而通過類可以創建任意多個具有相同屬性和方法的對象。 理解對象 創建自定義對象的最簡單的方法就是創建一個Object的實例,然後再為它添加屬性和方法。例如: 同樣上面的例子可以通過對象字面量語法寫成如下: 屬性類型 ECMAScript中有兩種屬性:數 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...