All files / vue-cascader/src/components Cascader.vue

89.66% Statements 26/29
58.33% Branches 7/12
60% Functions 3/5
89.66% Lines 26/29
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128                  1x 1x               6x                         3x       3x           4x                   4x         3x 3x 3x   3x 7x 5x 2x     2x       3x 2x 2x   2x       1x 1x 1x 1x   1x     1x     1x                                                                            
<template>
 <div class="cascader" v-click-outside="close">
  <div class="trigger" @click="toggle">{{result}}</div>
  <div v-if="isVisible">
   <CascaderItem :options="options" :value="value" :level="level" @change="change"></CascaderItem>
  </div>
 </div>
</template>
<script>
import cloneDeep from "lodash/cloneDeep";
import CascaderItem from "./CascaderItem";
 
export default {
 components: {
  CascaderItem
 },
 computed: {
  result() {
   return this.value.map(item => item.label).join("/");
  }
 },
 props: {
  lazyload: {
   type: Function
  },
  level: {
   type: Number,
   default: 0
  },
  value: {
   type: Array,
   default: () => []
  },
  options: {
   type: Array,
   default: () => []
  }
 },
 directives: {
  clickOutside: {
   inserted(el, bindings) {
    document.addEventListener("click", e => {
     if (el === e.target || el.contains(e.target)) {
      return;
     }
     bindings.value();
    });
   }
  }
 },
 data() {
  return { isVisible: false };
 },
 methods: {
  handle(id, children) {
   // id当前选择的 children 需要增加的孩子
   const cloneValue = cloneDeep(this.options);
   let stack = [...cloneValue];
   let index = 0;
   let current;
   while ((current = stack[index++])) {
    if (current.id !== id) {
     if (current.children) {
      stack = stack.concat(current.children);
     }
    } else {
     break;
    }
   }
   // current就是当前我要找的
   if (current) {
    current.children = children;
    console.log(current, "----");
    //    this.$set(current,'children',children);
    this.$emit("update:options", cloneValue);
   }
  },
  change(value) {
   const lastItem = value[value.length - 1];
   const { id } = lastItem;
E   if (this.lazyload) {
    this.lazyload(id, data => this.handle(id, data));
   }
   this.$emit("input", value);
  },
  toggle() {
   this.isVisible = !this.isVisible;
  },
  close() {
   this.isVisible = false;
  }
 }
};
</script>
<style lang="stylus">
.trigger {
 width: 150px;
 height: 30px;
 line-height: 30px;
 border: 1px solid #ccc;
}
 
.content {
 display: flex;
}
 
.content-left {
 border: 1px solid #ccc;
 max-height: 200px;
 overflow-y: scroll;
}
 
.content-right {
 position: relative;
 margin-left: -1px;
}
 
.label {
 font-size: 14px;
 width: 80px;
 line-height: 20px;
}
 
.label:hover {
 background: #ccc;
}
</style>