Commit dc39cc3163382062511f05f015d90b748df33758

Authored by daiyanze
1 parent 2e8add2f

Feature: Checkable tag

Showing 2 changed files with 36 additions and 9 deletions   Show diff stats
examples/routers/tag.vue
1 1 <template>
2 2 <div>
3   - <Tag v-for="item in count" :key="item" :name="item" @on-close="close" closable>标签{{ item + 1 }}</Tag>
  3 + <Tag v-for="item in count" :key="item" :name="item" type="border" color="yellow" @on-close="close" @on-check="check" closable>标签{{ item + 1 }}</Tag>
  4 + <Tag v-for="item in count" :key="item" :name="item" type="border" color="yellow" @on-close="close" @on-check="check" closable checkable>标签{{ 1 - item }}</Tag>
4 5 <Button icon="ios-plus-empty" type="dashed" size="small" @click="count += 1">添加标签</Button>
5 6 </div>
6 7 </template>
... ... @@ -9,13 +10,17 @@
9 10 data () {
10 11 return {
11 12 count: 3
12   - }
  13 + };
13 14 },
14 15 methods: {
15 16 close (e, name) {
16 17 console.log(e);
17 18 console.log(name);
  19 + },
  20 + check (e, name) {
  21 + console.log(e);
  22 + console.log(name);
18 23 }
19 24 }
20   - }
  25 + };
21 26 </script>
... ...
src/components/tag/tag.vue
1 1 <template>
2 2 <transition name="fade">
3   - <div :class="classes">
4   - <span :class="dotClasses" v-if="showDot"></span><span :class="textClasses"><slot></slot></span><Icon v-if="closable" type="ios-close-empty" @click.native.stop="close"></Icon>
  3 + <div :class="classes" @click.stop="check">
  4 + <span :class="dotClasses" v-if="showDot"></span><span :class="textClasses"><slot></slot></span>
  5 + <Icon v-if="closable" type="ios-close-empty" @click.native.stop="close"></Icon>
5 6 </div>
6 7 </transition>
7 8 </template>
... ... @@ -19,6 +20,14 @@
19 20 type: Boolean,
20 21 default: false
21 22 },
  23 + checkable: {
  24 + type: Boolean,
  25 + default: false
  26 + },
  27 + checked: {
  28 + type: Boolean,
  29 + default: true
  30 + },
22 31 color: {
23 32 validator (value) {
24 33 return oneOf(value, ['blue', 'green', 'red', 'yellow', 'default']);
... ... @@ -33,14 +42,20 @@
33 42 type: [String, Number]
34 43 }
35 44 },
  45 + data () {
  46 + return {
  47 + isChecked: this.checked
  48 + };
  49 + },
36 50 computed: {
37 51 classes () {
38 52 return [
39 53 `${prefixCls}`,
40 54 {
41   - [`${prefixCls}-${this.color}`]: !!this.color,
  55 + [`${prefixCls}-${this.color}`]: !!this.color && (this.checkable && this.isChecked),
42 56 [`${prefixCls}-${this.type}`]: !!this.type,
43   - [`${prefixCls}-closable`]: this.closable
  57 + [`${prefixCls}-closable`]: this.closable,
  58 + [`${prefixCls}-checkable`]: this.checkable
44 59 }
45 60 ];
46 61 },
... ... @@ -56,10 +71,17 @@
56 71 },
57 72 methods: {
58 73 close (event) {
  74 + this._emitAction(event, 'on-close');
  75 + },
  76 + check (event) {
  77 + this.isChecked = !this.isChecked;
  78 + this._emitAction(event, 'on-check');
  79 + },
  80 + _emitAction (event, action) {
59 81 if (this.name === undefined) {
60   - this.$emit('on-close', event);
  82 + this.$emit(action, event);
61 83 } else {
62   - this.$emit('on-close', event, this.name);
  84 + this.$emit(action, event, this.name);
63 85 }
64 86 }
65 87 }
... ...