all files / src/chip/ chip.vue

0% Statements 0/16
0% Branches 0/12
100% Functions 0/0
0% Lines 0/12
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122                                                                                                                                                                                                                                                   
<template>
  <div @mouseenter="onMouseenter" @mouseup="onMouseup" @mousedown="onMousedown"
      @mouseleave="onMouseleave" @touchstart="onTouchstart" @click= "handleClick"
      @touchend="onTouchend" @touchcancel="onTouchend" :class="classNames" class="mu-chip" :style="style">
    <slot></slot>
    <svg class="mu-chip-delete-icon" viewBox="0 0 24 24"
      v-if="showDelete && !disabled" @click.stop="handleDelete" :class="deleteIconClass">
      <path d="M12 2C6.47 2 2 6.47 2 12s4.47 10 10 10 10-4.47 10-10S17.53 2 12 2zm5 13.59L15.59 17 12 13.41 8.41 17 7 15.59 10.59 12 7 8.41 8.41 7 12 10.59 15.59 7 17 8.41 13.41 12 17 15.59z"/>
    </svg>
  </div>
</template>
 
<script>
import {isPc, getColor} from '../utils'
export default {
  name: 'mu-chip',
  props: {
    showDelete: {
      type: Boolean,
      default: false
    },
    disabled: {
      type: Boolean,
      default: false
    },
    deleteIconClass: {
      type: [Array, String, Object]
    },
    backgroundColor: {
      type: String
    },
    color: {
      type: String
    }
  },
  data () {
    return {
      focus: false,
      hover: false
    }
  },
  computed: {
    classNames () {
      if (this.disabled) return null
      return this.focus ? ['hover', 'active'] : this.hover ? ['hover'] : null
    },
    style () {
      return {
        'background-color': getColor(this.backgroundColor),
        'color': getColor(this.color)
      }
    }
  },
  methods: {
    onMouseenter () {
      if (isPc()) this.hover = true
    },
    onMouseleave () {
      if (isPc()) this.hover = false
    },
    onMousedown () {
      this.focus = true
    },
    onMouseup () {
      this.focus = false
    },
    onTouchstart () {
      this.focus = true
    },
    onTouchend () {
      this.focus = false
    },
    handleDelete () {
      this.$emit('delete')
    },
    handleClick () {
      if (!this.disabled) this.$emit('click')
    }
  }
}
</script>
 
<style lang="less">
@import "../styles/import.less";
.mu-chip {
  border-radius: 16px;
  line-height: 32px;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  background-color: darken(@alternateTextColor, 12%);
  color: fade(@textColor, 87%);
  padding: 0 12px;
  cursor: default;
  .mu-avatar:first-child{
    margin-left: -12px;
    margin-right: 4px;
  }
  &.active {
    box-shadow: 0 1px 6px fade(@fullBlack, 12%), 0 1px 4px fade(@fullBlack, 12%);
  }
  &.hover{
    .mu-chip-delete-icon{
      color: fade(fade(@textColor, 26%), 40%);
    }
    background-color: rgb(206, 206, 206);
    cursor: pointer;
  }
}
.mu-chip-delete-icon{
  display: inline-block;
  margin-right: -8px;
  margin-left: 4px;
  color: fade(@textColor, 26%);
  fill: currentColor;
  height: 24px;
  width: 24px;
  user-select: none;
  transition: all 450ms @easeOutFunction;
}
</style>