All files / src/components/m-multiplex.vue item.vue

73.68% Statements 14/19
37.5% Branches 3/8
50% Functions 2/4
72.22% Lines 13/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99      7x     7x                                                                                                                                                                                   7x 7x  
<template>
<div :class="$style.root"
    :selected="currentSelected" :readonly="parentVM.readonly" :disabled="disabled || parentVM.disabled"
    @click="select($event)" v-ellipsis-title
    vusion-slot-name="text">
    <slot>{{ text }}</slot>
</div>
</template>
 
<script>
import { MChild } from '../m-parent.vue';
import { ellipsisTitle } from '../../directives';
 
export default {
    name: 'm-multiplex-item',
    parentName: 'm-multiplex',
    groupName: 'm-multiplex-group',
    directives: { ellipsisTitle },
    mixins: [MChild],
    props: {
        text: String,
        value: null,
        selected: { type: Boolean, default: false },
        disabled: { type: Boolean, default: false },
        item: Object,
    },
    data() {
        return {
            currentSelected: this.selected, // @inherit: parentVM: undefined,
        };
    },
    watch: {
        selected(selected) {
            this.currentSelected = selected;
            this.parentVM && this.parentVM.watchSelectedChange(this);
        },
    },
    methods: {
        select(e) {
            // readme:点击事件可能在组件destoryed后在调用,多发于导航相关,使用isCreated来判断而不是使用parentVM来判断是因为防止有人单独mixin当前组件。
            if (!this.isCreated) {
                return;
            }
            if (this.disabled || this.parentVM.readonly || this.parentVM.disabled)
                return;
            this.$emit('click', e, this);
            this.parentVM.click(this);
 
            let cancel = false;
            this.$emit('before-select', {
                value: this.value,
                item: this.item,
                itemVM: this,
                preventDefault: () => (cancel = true),
            }, this);
            if (cancel)
                return;
            this.parentVM.select(this);
        },
    },
};
</script>
 
<style module>
.root {
    /* @Private */
    display: inline-block;
    cursor: var(--cursor-pointer);
 
    /* @Public */
    padding: 4px 12px;
}
 
.root:hover {
    background: none;
}
 
.root[readonly] {
    cursor: default;
    background: none;
}
 
.root[selected] {
    background: var(--brand-primary);
    color: var(--color-white);
}
 
.root[disabled] {
    /* @Private */
    cursor: var(--cursor-not-allowed);
    background: var(--background-color-base);
    color: var(--brand-disabled);
}
 
.root[selected][disabled] {
    background: var(--brand-disabled-light);
}
</style>