123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <template>
- <view>
- <view
- v-if="opened"
- class="wk-dropdown-item"
- @touchmove.stop.prevent
- @tap.stop.prevent>
- <template v-if="!$slots.default && !$slots.$default">
- <view
- v-for="(item, index) in options"
- :key="index"
- :class="{active: activeIndex === index}"
- class="wk-dropdown-item_options__item"
- @click="handleChoose(item, index)">
- <view class="text">
- {{ item.label }}
- </view>
- <text v-if="activeIndex === index" class="wk wk-check icon" />
- </view>
- </template>
-
- <slot v-else />
- </view>
- </view>
- </template>
- <script>
- /**
- * WkDropdownItem 下拉菜单
- * @author yxk
- * @property {Array<Object>} options 下拉选项数组
- * @property {String} name tab标题
- * @property {String} leftIcon tab左侧图标
- * @property {Number} defaultIndex 默认勾选中第几项
- * @event {Function} choose 点击选项时触发 参数 opt 被点击的项 index 第几个被点击
- */
- export default {
- name: 'WkDropdownItem',
- inject: ['dropdown'],
- props: {
- options: {
- type: Array,
- default: () => []
- },
- name: {
- type: String,
- required: true
- },
- leftIcon: {
- type: String,
- default: null
- },
- defaultIndex: {
- type: Number,
- default: -1
- }
- },
- data() {
- return {
- opened: false,
- activeIndex: -1
- }
- },
- computed: {
- propsObj() {
- return {
- name: this.name,
- leftIcon: this.leftIcon
- }
- }
- },
- watch: {
- propsObj: {
- handler() {
- this.initFunc()
- },
- deep: true
- },
- defaultIndex: {
- handler(val) {
- this.activeIndex = val
- },
- immediate: true
- }
- },
- created() {
- this.initFunc()
- },
- methods: {
- initFunc() {
- const findIndex = this.dropdown.menuList.findIndex(o => this.name === o.name)
- if (findIndex === -1) {
- // this.dropdown.children.push(this)
- this.dropdown.menuList.push({
- name: this.name,
- leftIcon: this.leftIcon
- })
- } else {
- // this.dropdown.children.splice(findIndex, 1, this)
- this.dropdown.menuList.splice(findIndex, 1, {
- name: this.name,
- leftIcon: this.leftIcon
- })
- }
- },
-
- getParentNode(name = undefined) {
- let parent = this.$parent;
- // 通过while历遍,这里主要是为了H5需要多层解析的问题
- while (parent) {
- // 父组件
- if (parent.$options && parent.$options.name !== name) {
- // 如果组件的name不相等,继续上一级寻找
- parent = parent.$parent;
- } else {
- return parent;
- }
- }
- return false;
- },
-
- handleChoose(opt, index) {
- this.activeIndex = index
- this.$emit('choose', opt, index)
- if (this.dropdown.autoClose) {
- this.dropdown.close()
- }
- }
- }
- }
- </script>
- <style scoped lang="scss">
- .wk-dropdown-item {
- width: 100%;
- flex: 1;
- overflow: auto;
-
- .wk-dropdown-item_options__item {
- height: 75rpx;
- line-height: 75rpx;
- padding: 0 30rpx;
- @include left;
- .text {
- flex: 1;
- }
-
- &.active {
- color: $theme-color;
- .icon {
- color: $theme-color;
- text-align: right;
- }
- }
- }
- }
- </style>
|