u-dropdown-item.vue 3.2 KB

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