list.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <template>
  2. <div class="com-list-box" :class="{'fill' : height == 'fill', 'auto' : height == 'auto'}">
  3. <div class="com-list-title" v-if="title != ''">{{title}}</div>
  4. <div class="com-list-body">
  5. <div class="con-list-item" v-for="(data, index) of list" :key="index" @click="select(index)"
  6. :class="data.color + ' ' + (activeIndex == index ? 'active' : '')">{{data.text}}
  7. </div>
  8. </div>
  9. </div>
  10. </template>
  11. <script>
  12. export default {
  13. // 名称
  14. name: "List",
  15. // 使用组件
  16. components: {},
  17. // 传入参数
  18. props: {
  19. // 标题 ""不显示
  20. title: {
  21. type: String,
  22. default: ""
  23. },
  24. // 列表面板高度 fill auto 300px
  25. height: {
  26. type: String,
  27. default: "fill"
  28. },
  29. // 默认选中第几个
  30. index: {
  31. type: Number,
  32. default: 0
  33. },
  34. /**
  35. * {
  36. text: "1#逆变室",
  37. color: "blue"
  38. }
  39. */
  40. list: Array
  41. },
  42. // 自定义事件
  43. emits: {
  44. // 选中事件
  45. select: null
  46. },
  47. // 数据
  48. data() {
  49. return {
  50. activeIndex: 0
  51. }
  52. },
  53. // 函数
  54. methods: {
  55. select: function (index) {
  56. this.activeIndex = index;
  57. this.$emit('select', this.list[index]);
  58. }
  59. },
  60. // 生命周期钩子
  61. beforeCreate() {
  62. // 创建前
  63. },
  64. created() {
  65. // 创建后
  66. this.activeIndex = this.index;
  67. },
  68. beforeMount() {
  69. // 渲染前
  70. },
  71. mounted() {
  72. // 渲染后
  73. if (this.height != 'fill' && this.height != 'auto') {
  74. this.$el.style.height = this.height;
  75. }
  76. },
  77. beforeUpdate() {
  78. // 数据更新前
  79. },
  80. updated() {
  81. // 数据更新后
  82. },
  83. }
  84. </script>
  85. <style lang="less">
  86. .com-list-box {
  87. display: flex;
  88. flex-direction: column;
  89. &.auto {
  90. height: auto;
  91. }
  92. &.fill {
  93. height: 100%;
  94. }
  95. .com-list-title {
  96. font-size: @fontsize-l;
  97. color: @write;
  98. background-color: fade(@gray, 50%);
  99. padding: 16px 20px;
  100. }
  101. .com-list-body {
  102. background-color: fade(@gray, 20%);
  103. flex-grow: 1;
  104. overflow: auto;
  105. display: flex;
  106. flex-direction: column;
  107. .con-list-item {
  108. color: @gray;
  109. position: relative;
  110. font-size: @fontsize;
  111. text-align: center;
  112. padding: 4px 0;
  113. border-bottom: 1px solid @gray;
  114. cursor: pointer;
  115. &:hover {
  116. background-color: fade(@gray, 30%);
  117. }
  118. &.active {
  119. color: @write;
  120. background-color: fade(@blue, 60%);
  121. }
  122. &::after {
  123. content: '';
  124. position: absolute;
  125. width: 8px;
  126. height: 8px;
  127. left: 12px;
  128. top: calc(50% - 4px);
  129. }
  130. &.write {
  131. &::after {
  132. background-color: @write;
  133. }
  134. }
  135. &.green {
  136. &::after {
  137. background-color: @green;
  138. }
  139. }
  140. &.blue {
  141. &::after {
  142. background-color: @blue;
  143. }
  144. }
  145. &.purple {
  146. &::after {
  147. background-color: @purple;
  148. }
  149. }
  150. &.red {
  151. &::after {
  152. background-color: @red;
  153. }
  154. }
  155. &.orange {
  156. &::after {
  157. background-color: @orange;
  158. }
  159. }
  160. &.gray {
  161. &::after {
  162. background-color: @gray;
  163. }
  164. }
  165. &.yellow {
  166. &::after {
  167. background-color: @yellow;
  168. }
  169. }
  170. }
  171. }
  172. }
  173. </style>