classic-tabs.vue 2.55 KB
<template>
  <div :class="classes">
    <div class="yoho-coupon-filter-bar">
      <div
        :class="tabCls(item)"
        v-for="(item, index) in navList"
        @click="handleChange(index)"
        :key="index"
      >{{item.label}}</div>
    </div>
    <div :class="contentClasses" :style="contentStyle" ref="panes">
      <slot name="panes"></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: "ClassicTabs",
  props: {
    value: {
      type: [String, Number]
    }
  },
  data() {
    return {
      prefixCls: "yoho-tabs",
      navList: [],
      activeKey: this.value
    };
  },
  computed: {
    classes() {
      return [`${this.prefixCls}`];
    },
    contentClasses() {
      return [`yoho-tabs-pane`];
    },
    contentStyle() {
      return [];
    }
  },
  methods: {
    updateNav() {
      this.navList = [];
      this.getTabs().forEach((pane, index) => {
        this.navList.push({
          label: pane.label,
          name: pane.currentName || index,
          disabled: pane.disabled
        });

        if (!pane.currentName) pane.currentName = index;

        if (index === 0) {
          if (!this.activeKey) {
            this.activeKey = pane.currentName || index;
          }
        }
      });

      this.updateStatus();
    },
    getTabs() {
      return this.$children.filter(item => item.$options.name === "TabPane");
    },
    updateStatus() {
      const tabs = this.getTabs();

      tabs.forEach(tab => (tab.show = tab.currentName === this.activeKey));
    },
    handleChange(index) {
      const nav = this.navList[index];

      this.activeKey = nav.name;
      this.$emit("input", nav.name);
    },
    tabCls(item) {
      return [
        "filter-btn-box",
        {
          active: item.name === this.activeKey
        }
      ];
    }
  },
  watch: {
    activeKey(val) {
      this.updateStatus();
    },
    value(val) {
      this.activeKey = val;
    }
  },
  created() {
    // this.updateNav();
  }
};
</script>

<style lang="scss" scoped>
$yoho-tab: yoho-tab;

.#{$yoho-tab}s {
  width: 100%;
  height: 100%;
}

.yoho-coupon-filter-bar {
  width: 100%;
  height: 90px;
  display: flex;
  padding: 14px 0;
  box-shadow: 0 0 16px 0 #eee;
  background-color: #fff;
  position: fixed;
  left: 0;
  z-index: 3;

  .filter-btn-box {
    flex: 1;
    line-height: 60px;
    text-align: center;
    font-size: 28px;
    color: #b0b0b0;
    border-right: 1px solid #e0e0e0;
  }

  .filter-btn-box:last-child {
    border-right: none;
  }

  .show-filter-btn {
    color: #b0b0b0;
  }

  .active {
    color: #444;
  }
}
</style>