u-dropdown-item.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 class="u-dropdown-item__scroll" scroll-y="true" :style="{
  5. height: addUnit(height)
  6. }">
  7. <view class="u-dropdown-item__options">
  8. <up-cell-group>
  9. <up-cell @click="cellClick(item.value)" :arrow="false" :title="item.label" v-for="(item, index) in options"
  10. :key="index" :title-style="{
  11. color: modelValue == item.value ? activeColor : inactiveColor
  12. }">
  13. <up-icon v-if="modelValue == item.value" name="checkbox-mark" :color="activeColor" size="32"></up-icon>
  14. </up-cell>
  15. </up-cell-group>
  16. </view>
  17. </scroll-view>
  18. </block>
  19. <slot v-else />
  20. </view>
  21. </template>
  22. <script>
  23. import { props } from './props';
  24. import { mpMixin } from '../../libs/mixin/mpMixin';
  25. import { mixin } from '../../libs/mixin/mixin';
  26. import { addUnit, $parent } from '../../libs/function/index';
  27. /**
  28. * dropdown-item 下拉菜单
  29. * @description 该组件一般用于向下展开菜单,同时可切换多个选项卡的场景
  30. * @tutorial https://ijry.github.io/uview-plus/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: [mpMixin, mixin, props],
  42. options: {
  43. styleIsolation: 'shared',
  44. },
  45. data() {
  46. return {
  47. active: false, // 当前项是否处于展开状态
  48. activeColor: '#2979ff', // 激活时左边文字和右边对勾图标的颜色
  49. inactiveColor: '#606266', // 未激活时左边文字和右边对勾图标的颜色
  50. }
  51. },
  52. computed: {
  53. // 监听props是否发生了变化,有些值需要传递给父组件u-dropdown,无法双向绑定
  54. propsChange() {
  55. return `${this.title}-${this.disabled}`;
  56. }
  57. },
  58. watch: {
  59. propsChange(n) {
  60. // 当值变化时,通知父组件重新初始化,让父组件执行每个子组件的init()方法
  61. // 将所有子组件数据重新整理一遍
  62. if (this.parent) this.parent.init();
  63. }
  64. },
  65. created() {
  66. // 父组件的实例
  67. this.parent = false;
  68. },
  69. emits: ['update:modelValue', 'change'],
  70. methods: {
  71. addUnit,
  72. init() {
  73. // 获取父组件u-dropdown
  74. let parent = $parent.call(this, 'u-dropdown');
  75. if (parent) {
  76. this.parent = parent;
  77. // 将子组件的激活颜色配置为父组件设置的激活和未激活时的颜色
  78. this.activeColor = parent.activeColor;
  79. this.inactiveColor = parent.inactiveColor;
  80. // 将本组件的this,放入到父组件的children数组中,让父组件可以操作本(子)组件的方法和属性
  81. // push进去前,显判断是否已经存在了本实例,因为在子组件内部数据变化时,会通过父组件重新初始化子组件
  82. let exist = parent.children.find(val => {
  83. return this === val;
  84. })
  85. if (!exist) parent.children.push(this);
  86. if (parent.children.length == 1) this.active = true;
  87. // 父组件无法监听children的变化,故将子组件的title,传入父组件的menuList数组中
  88. parent.menuList.push({
  89. title: this.title,
  90. disabled: this.disabled
  91. });
  92. }
  93. },
  94. // cell被点击
  95. cellClick(value) {
  96. // 修改通过v-model绑定的值
  97. // #ifdef VUE2
  98. this.$emit('input', value);
  99. // #endif
  100. // #ifdef VUE3
  101. this.$emit('update:modelValue', value);
  102. // #endif
  103. // 通知父组件(u-dropdown)收起菜单
  104. this.parent.close();
  105. // 发出事件,抛出当前勾选项的value
  106. this.$emit('change', value);
  107. }
  108. },
  109. mounted() {
  110. this.init();
  111. }
  112. }
  113. </script>
  114. <style scoped lang="scss">
  115. @import "../../libs/css/components.scss";
  116. .u-dropdown-item__scroll {
  117. background: #ffffff;
  118. }
  119. </style>