Commit a1553a9cc4e61004fc6335fb96934f98a0443dab

Authored by Aresn
Committed by GitHub
2 parents 5064c7b3 fd79f102

Merge pull request #2124 from daiyanze/feature/checkable_tag

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