u-dropdown-item.vue 4.4 KB

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