|
4 | 4 |
|
5 | 5 | <md-chip
|
6 | 6 | v-for="(chip, key) in value"
|
7 |
| - :key="key" |
| 7 | + :key="chip" |
8 | 8 | :md-deletable="!mdStatic"
|
9 | 9 | :md-clickable="!mdStatic"
|
| 10 | + :md-duplicated="duplicatedChip === chip" |
10 | 11 | @keydown.enter="$emit('md-click', chip, key)"
|
11 | 12 | @click.native="$emit('md-click', chip, key)"
|
12 | 13 | @md-delete.stop="removeChip(chip)">
|
|
21 | 22 | :type="mdInputType"
|
22 | 23 | :id="id"
|
23 | 24 | :placeholder="mdPlaceholder"
|
| 25 | + @input="handleInput" |
24 | 26 | @keydown.enter="insertChip"
|
25 | 27 | @keydown.8="handleBackRemove">
|
26 | 28 | </md-input>
|
|
52 | 54 | },
|
53 | 55 | mdPlaceholder: [String, Number],
|
54 | 56 | mdStatic: Boolean,
|
55 |
| - mdLimit: Number |
| 57 | + mdLimit: Number, |
| 58 | + mdCheckDuplicated: { |
| 59 | + type: Boolean, |
| 60 | + default: false |
| 61 | + } |
56 | 62 | },
|
57 | 63 | data: () => ({
|
58 |
| - inputValue: '' |
| 64 | + inputValue: '', |
| 65 | + duplicatedChip: null |
59 | 66 | }),
|
60 | 67 | computed: {
|
61 | 68 | chipsClasses () {
|
|
70 | 77 | },
|
71 | 78 | methods: {
|
72 | 79 | insertChip ({ target }) {
|
73 |
| - if ( |
74 |
| - !this.inputValue || |
75 |
| - this.value.includes(this.inputValue) || |
76 |
| - !this.modelRespectLimit |
77 |
| - ) { |
| 80 | + if (!this.inputValue || !this.modelRespectLimit) { |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + if (this.value.includes(this.inputValue)) { |
| 85 | + this.duplicatedChip = null |
| 86 | + // to trigger animate |
| 87 | + this.$nextTick(() => { |
| 88 | + this.duplicatedChip = this.inputValue |
| 89 | + }) |
78 | 90 | return
|
79 | 91 | }
|
| 92 | + |
80 | 93 | this.value.push(this.inputValue)
|
81 | 94 | this.$emit('input', this.value)
|
82 | 95 | this.$emit('md-insert', this.inputValue)
|
|
94 | 107 | if (!this.inputValue) {
|
95 | 108 | this.removeChip(this.value[this.value.length - 1])
|
96 | 109 | }
|
| 110 | + }, |
| 111 | + handleInput () { |
| 112 | + if (this.mdCheckDuplicated) { |
| 113 | + this.checkDuplicated() |
| 114 | + } else { |
| 115 | + this.duplicatedChip = null |
| 116 | + } |
| 117 | + }, |
| 118 | + checkDuplicated () { |
| 119 | + if (!this.value.includes(this.inputValue)) { |
| 120 | + this.duplicatedChip = null |
| 121 | + return |
| 122 | + } |
| 123 | + if (!this.mdCheckDuplicated) return |
| 124 | + this.duplicatedChip = this.inputValue |
| 125 | + } |
| 126 | + }, |
| 127 | + watch: { |
| 128 | + value () { |
| 129 | + this.checkDuplicated() |
97 | 130 | }
|
98 | 131 | }
|
99 | 132 | })
|
|
0 commit comments