u-dropdown-item.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <template>
  2. <view class="u-dropdown-item" v-if="active" @touchmove.stop.prevent="() => {}" @tap.stop.prevent="() => {}">
  3. <block v-if="!$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="value == 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. export default {
  24. name: 'u-dropdown-item',
  25. props: {
  26. // 当前选中项的value值
  27. value: {
  28. type: [Number, String, Array],
  29. default: ''
  30. },
  31. // 菜单项标题
  32. title: {
  33. type: [String, Number],
  34. default: ''
  35. },
  36. // 选项数据,如果传入了默认slot,此参数无效
  37. options: {
  38. type: Array,
  39. default () {
  40. return []
  41. }
  42. },
  43. // 是否禁用此菜单项
  44. disabled: {
  45. type: Boolean,
  46. default: false
  47. },
  48. // 下拉弹窗的高度
  49. height: {
  50. type: [Number, String],
  51. default: 'auto'
  52. }
  53. },
  54. data() {
  55. return {
  56. active: false, // 当前项是否处于展开状态
  57. activeColor: '#2979ff', // 激活时左边文字和右边对勾图标的颜色
  58. inactiveColor: '#606266', // 未激活时左边文字和右边对勾图标的颜色
  59. }
  60. },
  61. computed: {
  62. // 监听props是否发生了变化,有些值需要传递给父组件u-dropdown,无法双向绑定
  63. propsChange() {
  64. return `${this.title}-${this.disabled}`;
  65. }
  66. },
  67. watch: {
  68. propsChange(n) {
  69. // 当值变化时,通知父组件重新初始化,让父组件执行每个子组件的init()方法
  70. // 将所有子组件数据重新整理一遍
  71. if (this.parent) this.parent.init();
  72. }
  73. },
  74. created() {
  75. // 父组件的实例
  76. this.parent = false;
  77. },
  78. methods: {
  79. init() {
  80. // 获取父组件u-dropdown
  81. let parent = this.$u.$parent.call(this, 'u-dropdown');
  82. if (parent) {
  83. this.parent = parent;
  84. // 将子组件的激活颜色配置为父组件设置的激活和未激活时的颜色
  85. this.activeColor = parent.activeColor;
  86. this.inactiveColor = parent.inactiveColor;
  87. // 将本组件的this,放入到父组件的children数组中,让父组件可以操作本(子)组件的方法和属性
  88. // push进去前,显判断是否已经存在了本实例,因为在子组件内部数据变化时,会通过父组件重新初始化子组件
  89. let exist = parent.children.find(val => {
  90. return this === val;
  91. })
  92. if (!exist) parent.children.push(this);
  93. if (parent.children.length == 1) this.active = true;
  94. // 父组件无法监听children的变化,故将子组件的title,传入父组件的menuList数组中
  95. parent.menuList.push({
  96. title: this.title,
  97. disabled: this.disabled
  98. });
  99. }
  100. },
  101. // cell被点击
  102. cellClick(value) {
  103. // 修改通过v-model绑定的值
  104. this.$emit('input', value);
  105. // 通知父组件(u-dropdown)收起菜单
  106. this.parent.close();
  107. // 发出事件,抛出当前勾选项的value
  108. this.$emit('change', value);
  109. }
  110. },
  111. mounted() {
  112. this.init();
  113. }
  114. }
  115. </script>
  116. <style scoped lang="scss">
  117. @import "../../libs/css/style.components.scss";
  118. </style>