u-dropdown-item.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <template>
  2. <view class="u-dropdown-item" v-if="active" @touchmove.stop.prevent="() => {}" @tap.stop.prevent="() => {}">
  3. <block v-if="!$slots.default && !$slots.$default">
  4. <scroll-view scroll-y="true" :style="{
  5. height: $u.addUnit(height)
  6. }">
  7. <view class="u-dropdown-item__options">
  8. <u-cell-group>
  9. <u-cell-item @click="cellClick(item.value)" :arrow="false" :title="item.label" v-for="(item, index) in options"
  10. :key="index" :title-style="{
  11. color: value === item.value ? activeColor : inactiveColor
  12. }">
  13. <u-icon v-if="getValue() === item.value" name="checkbox-mark" :color="activeColor" size="32"></u-icon>
  14. </u-cell-item>
  15. </u-cell-group>
  16. </view>
  17. </scroll-view>
  18. </block>
  19. <slot v-else />
  20. </view>
  21. </template>
  22. <script>
  23. /**
  24. * dropdown-item 下拉菜单
  25. * @description 该组件一般用于向下展开菜单,同时可切换多个选项卡的场景
  26. * @tutorial http://uviewui.com/components/dropdown.html
  27. * @property {String | Number} v-model 双向绑定选项卡选择值
  28. * @property {String} title 菜单项标题
  29. * @property {Array[Object]} options 选项数据,如果传入了默认slot,此参数无效
  30. * @property {Boolean} disabled 是否禁用此选项卡(默认false)
  31. * @property {String | Number} duration 选项卡展开和收起的过渡时间,单位ms(默认300)
  32. * @property {String | Number} height 弹窗下拉内容的高度(内容超出将会滚动)(默认auto)
  33. * @example <u-dropdown-item title="标题"></u-dropdown-item>
  34. */
  35. export default {
  36. name: 'u-dropdown-item',
  37. emits: ["update:modelValue", "input", "change"],
  38. props: {
  39. // 当前选中项的value值
  40. value: {
  41. type: [Number, String, Array],
  42. default: ''
  43. },
  44. modelValue: {
  45. type: [Number, String, Array],
  46. default: ''
  47. },
  48. // 菜单项标题
  49. title: {
  50. type: [String, Number],
  51. default: ''
  52. },
  53. // 选项数据,如果传入了默认slot,此参数无效
  54. options: {
  55. type: Array,
  56. default () {
  57. return []
  58. }
  59. },
  60. // 是否禁用此菜单项
  61. disabled: {
  62. type: Boolean,
  63. default: false
  64. },
  65. // 下拉弹窗的高度
  66. height: {
  67. type: [Number, String],
  68. default: 'auto'
  69. },
  70. },
  71. data() {
  72. return {
  73. active: false, // 当前项是否处于展开状态
  74. activeColor: '#2979ff', // 激活时左边文字和右边对勾图标的颜色
  75. inactiveColor: '#606266', // 未激活时左边文字和右边对勾图标的颜色
  76. }
  77. },
  78. computed: {
  79. // 监听props是否发生了变化,有些值需要传递给父组件u-dropdown,无法双向绑定
  80. propsChange() {
  81. return `${this.title}-${this.disabled}`;
  82. }
  83. },
  84. watch: {
  85. propsChange(n) {
  86. // 当值变化时,通知父组件重新初始化,让父组件执行每个子组件的init()方法
  87. // 将所有子组件数据重新整理一遍
  88. if (this.parent) this.parent.init();
  89. }
  90. },
  91. created() {
  92. // 父组件的实例
  93. this.parent = false;
  94. },
  95. methods: {
  96. getValue(){
  97. // #ifndef VUE3
  98. return this.value;
  99. // #endif
  100. // #ifdef VUE3
  101. return this.modelValue;
  102. // #endif
  103. },
  104. init() {
  105. // 获取父组件u-dropdown
  106. let parent = this.$u.$parent.call(this, 'u-dropdown');
  107. if (parent) {
  108. this.parent = parent;
  109. // 将子组件的激活颜色配置为父组件设置的激活和未激活时的颜色
  110. this.activeColor = parent.activeColor;
  111. this.inactiveColor = parent.inactiveColor;
  112. // 将本组件的this,放入到父组件的children数组中,让父组件可以操作本(子)组件的方法和属性
  113. // push进去前,显判断是否已经存在了本实例,因为在子组件内部数据变化时,会通过父组件重新初始化子组件
  114. let exist = parent.children.find(val => {
  115. return this === val;
  116. })
  117. if (!exist) parent.children.push(this);
  118. if (parent.children.length == 1) this.active = true;
  119. // 父组件无法监听children的变化,故将子组件的title,传入父组件的menuList数组中
  120. parent.menuList.push({
  121. title: this.title,
  122. disabled: this.disabled
  123. });
  124. }
  125. },
  126. // cell被点击
  127. cellClick(value) {
  128. // 修改通过v-model绑定的值
  129. this.$emit('input', value);
  130. this.$emit("update:modelValue", value);
  131. // 通知父组件(u-dropdown)收起菜单
  132. this.parent.close();
  133. // 发出事件,抛出当前勾选项的value
  134. this.$emit('change', value);
  135. }
  136. },
  137. mounted() {
  138. this.init();
  139. }
  140. }
  141. </script>
  142. <style scoped lang="scss">
  143. @import "../../libs/css/style.components.scss";
  144. </style>