wk-dropdown-item.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <template>
  2. <view>
  3. <view
  4. v-if="opened"
  5. class="wk-dropdown-item"
  6. @touchmove.stop.prevent
  7. @tap.stop.prevent>
  8. <template v-if="!$slots.default && !$slots.$default">
  9. <view
  10. v-for="(item, index) in options"
  11. :key="index"
  12. :class="{active: activeIndex === index}"
  13. class="wk-dropdown-item_options__item"
  14. @click="handleChoose(item, index)">
  15. <view class="text">
  16. {{ item.label }}
  17. </view>
  18. <text v-if="activeIndex === index" class="wk wk-check icon" />
  19. </view>
  20. </template>
  21. <slot v-else />
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. /**
  27. * WkDropdownItem 下拉菜单
  28. * @author yxk
  29. * @property {Array<Object>} options 下拉选项数组
  30. * @property {String} name tab标题
  31. * @property {String} leftIcon tab左侧图标
  32. * @property {Number} defaultIndex 默认勾选中第几项
  33. * @event {Function} choose 点击选项时触发 参数 opt 被点击的项 index 第几个被点击
  34. */
  35. export default {
  36. name: 'WkDropdownItem',
  37. inject: ['dropdown'],
  38. props: {
  39. options: {
  40. type: Array,
  41. default: () => []
  42. },
  43. name: {
  44. type: String,
  45. required: true
  46. },
  47. leftIcon: {
  48. type: String,
  49. default: null
  50. },
  51. defaultIndex: {
  52. type: Number,
  53. default: -1
  54. }
  55. },
  56. data() {
  57. return {
  58. opened: false,
  59. activeIndex: -1
  60. }
  61. },
  62. computed: {
  63. propsObj() {
  64. return {
  65. name: this.name,
  66. leftIcon: this.leftIcon
  67. }
  68. }
  69. },
  70. watch: {
  71. propsObj: {
  72. handler() {
  73. this.initFunc()
  74. },
  75. deep: true
  76. },
  77. defaultIndex: {
  78. handler(val) {
  79. this.activeIndex = val
  80. },
  81. immediate: true
  82. }
  83. },
  84. created() {
  85. this.initFunc()
  86. },
  87. methods: {
  88. initFunc() {
  89. const findIndex = this.dropdown.menuList.findIndex(o => this.name === o.name)
  90. if (findIndex === -1) {
  91. // this.dropdown.children.push(this)
  92. this.dropdown.menuList.push({
  93. name: this.name,
  94. leftIcon: this.leftIcon
  95. })
  96. } else {
  97. // this.dropdown.children.splice(findIndex, 1, this)
  98. this.dropdown.menuList.splice(findIndex, 1, {
  99. name: this.name,
  100. leftIcon: this.leftIcon
  101. })
  102. }
  103. },
  104. getParentNode(name = undefined) {
  105. let parent = this.$parent;
  106. // 通过while历遍,这里主要是为了H5需要多层解析的问题
  107. while (parent) {
  108. // 父组件
  109. if (parent.$options && parent.$options.name !== name) {
  110. // 如果组件的name不相等,继续上一级寻找
  111. parent = parent.$parent;
  112. } else {
  113. return parent;
  114. }
  115. }
  116. return false;
  117. },
  118. handleChoose(opt, index) {
  119. this.activeIndex = index
  120. this.$emit('choose', opt, index)
  121. if (this.dropdown.autoClose) {
  122. this.dropdown.close()
  123. }
  124. }
  125. }
  126. }
  127. </script>
  128. <style scoped lang="scss">
  129. .wk-dropdown-item {
  130. width: 100%;
  131. flex: 1;
  132. overflow: auto;
  133. .wk-dropdown-item_options__item {
  134. height: 75rpx;
  135. line-height: 75rpx;
  136. padding: 0 30rpx;
  137. @include left;
  138. .text {
  139. flex: 1;
  140. }
  141. &.active {
  142. color: $theme-color;
  143. .icon {
  144. color: $theme-color;
  145. text-align: right;
  146. }
  147. }
  148. }
  149. }
  150. </style>