updown.vue 1.21 KB
<template>
    <li class="order-item" :class="active" @click="clickHandler">
        <span class="order-name">{{txt}}</span>
        <span class="order-icon">
            <i class="icon icon-sort-asc" :class="{active: $parent.val === vals[0]}"></i>
            <i class="icon icon-sort-desc" :class="{active: $parent.val === vals[1]}"></i>
        </span>
    </li>
</template>
<style>
.order-item .icon {
    color: #b0b0b0;
    &.active {
        color: #000;
    }
}
</style>
<script>
module.exports = {
    props: {
        txt: String,
        vals: Array // [asc, desc]
    },
    data: function() {
        return {};
    },
    computed: {
        active: function() {
            const index = this.vals.indexOf(this.$parent.val);

            if (index >= 0) {
                return {
                    active: true,
                };
            }
        }
    },
    methods: {
        clickHandler: function() {
            let index = this.vals.indexOf(this.$parent.val);

            if (index === -1) {
                this.$parent.val = this.vals[0];
            } else {
                index = index === 0 ? 1 : 0;
                this.$parent.val = this.vals[index];
            }
        }
    }
};
</script>