|
1 | 1 | <template> |
2 | 2 | <md-field class="md-chips" :class="[$mdActiveTheme, chipsClasses]"> |
3 | | - <slot /> |
| 3 | + <slot/> |
4 | 4 |
|
5 | 5 | <md-chip |
6 | 6 | v-for="(chip, key) in value" |
|
24 | 24 | :placeholder="mdPlaceholder" |
25 | 25 | @input="handleInput" |
26 | 26 | @keydown.enter="insertChip" |
27 | | - @keydown.8="handleBackRemove"> |
| 27 | + @keydown.8="handleBackRemove" |
| 28 | + @focusout="handleFocusOut" |
| 29 | + > |
28 | 30 | </md-input> |
29 | 31 | </md-field> |
30 | 32 | </template> |
|
54 | 56 | }, |
55 | 57 | mdPlaceholder: [String, Number], |
56 | 58 | mdStatic: Boolean, |
| 59 | + mdAutoInsert: { |
| 60 | + type: Boolean, |
| 61 | + default: false |
| 62 | + }, |
57 | 63 | mdLimit: Number, |
58 | 64 | mdCheckDuplicated: { |
59 | 65 | type: Boolean, |
|
68 | 74 | duplicatedChip: null |
69 | 75 | }), |
70 | 76 | computed: { |
71 | | - chipsClasses () { |
| 77 | + chipsClasses() { |
72 | 78 | return { |
73 | 79 | 'md-has-value': this.value && this.value.length |
74 | 80 | } |
75 | 81 | }, |
76 | 82 |
|
77 | | - modelRespectLimit () { |
| 83 | + modelRespectLimit() { |
78 | 84 | return !this.mdLimit || this.value.length < this.mdLimit |
79 | 85 | }, |
80 | 86 |
|
81 | | - formattedInputValue () { |
| 87 | + formattedInputValue() { |
82 | 88 | if (!this.mdFormat) { |
83 | 89 | return this.inputValue |
84 | 90 | } |
85 | 91 | return this.mdFormat(this.inputValue) |
86 | 92 | } |
87 | 93 | }, |
88 | 94 | methods: { |
89 | | - insertChip ({ target }) { |
| 95 | + insertChip({target}) { |
90 | 96 | let inputValue = this.formattedInputValue |
91 | 97 |
|
92 | 98 | if (!inputValue || !this.modelRespectLimit) { |
|
107 | 113 | this.$emit('md-insert', inputValue) |
108 | 114 | this.inputValue = '' |
109 | 115 | }, |
110 | | - removeChip (chip) { |
| 116 | + removeChip(chip) { |
111 | 117 | const index = this.value.indexOf(chip) |
112 | 118 |
|
113 | 119 | this.value.splice(index, 1) |
114 | 120 | this.$emit('input', this.value) |
115 | 121 | this.$emit('md-delete', chip, index) |
116 | 122 | this.$nextTick(() => this.$refs.input.$el.focus()) |
117 | 123 | }, |
118 | | - handleBackRemove () { |
| 124 | + handleBackRemove() { |
119 | 125 | if (!this.inputValue) { |
120 | 126 | this.removeChip(this.value[this.value.length - 1]) |
121 | 127 | } |
122 | 128 | }, |
123 | | - handleInput () { |
| 129 | + handleInput() { |
124 | 130 | if (this.mdCheckDuplicated) { |
125 | 131 | this.checkDuplicated() |
126 | 132 | } else { |
127 | 133 | this.duplicatedChip = null |
128 | 134 | } |
129 | 135 | }, |
130 | | - checkDuplicated () { |
| 136 | + handleFocusOut({target}) { |
| 137 | + if (this.mdAutoInsert) { |
| 138 | + this.insertChip(target) |
| 139 | + } |
| 140 | + }, |
| 141 | + checkDuplicated() { |
131 | 142 | if (!this.value.includes(this.formattedInputValue)) { |
132 | 143 | this.duplicatedChip = null |
133 | 144 | return false |
|
141 | 152 | } |
142 | 153 | }, |
143 | 154 | watch: { |
144 | | - value () { |
| 155 | + value() { |
145 | 156 | this.checkDuplicated() |
146 | 157 | } |
147 | 158 | } |
|
152 | 163 | @import "~components/MdAnimation/variables"; |
153 | 164 |
|
154 | 165 | .md-chips.md-field { |
155 | | - padding-top: 12px; |
156 | | - flex-wrap: wrap; |
| 166 | + padding-top : 12px; |
| 167 | + flex-wrap : wrap; |
157 | 168 |
|
158 | 169 | &.md-has-value { |
159 | 170 | label { |
160 | | - top: -6px; |
| 171 | + top : -6px; |
| 172 | + } |
161 | 173 | } |
162 | | - } |
163 | 174 |
|
164 | 175 | .md-chip { |
165 | | - margin-bottom: 4px; |
| 176 | + margin-bottom : 4px; |
166 | 177 |
|
167 | 178 | &:last-of-type { |
168 | | - margin-right: 8px; |
| 179 | + margin-right : 8px; |
| 180 | + } |
169 | 181 | } |
170 | | - } |
171 | 182 |
|
172 | 183 | .md-input { |
173 | | - min-width: 128px; |
| 184 | + min-width : 128px; |
| 185 | + } |
174 | 186 | } |
175 | | - } |
176 | 187 | </style> |
0 commit comments