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