u-dropdown-item.vue 4.3 KB

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