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