Skip to content

Commit a5399e1

Browse files
committed
Implemented the function of automatically adding a value in the MdChips component, in case of loss of focus
1 parent 0f96730 commit a5399e1

File tree

3 files changed

+57
-20
lines changed

3 files changed

+57
-20
lines changed

docs/app/pages/Components/Chips/Chips.vue

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<example src="./examples/DuplicatedFeedback.vue" />
66
<example src="./examples/Format.vue" />
77
<example src="./examples/Themed.vue" />
8+
<example src="./examples/AutoInsert.vue" />
89

910
<template>
1011
<page-container centered :title="$t('pages.chips.title')">
@@ -61,6 +62,13 @@
6162
<code-example title="Formatted chips" :component="examples['format']" />
6263
</div>
6364

65+
<div class="page-container-section">
66+
<h2 id="auto-insert">Auto insert</h2>
67+
68+
<p>Automatic value entry when focus is lost:</p>
69+
<code-example title="Auto insert" :component="examples['auto-insert']" />
70+
</div>
71+
6472
<div class="page-container-section">
6573
<h2 id="hue-colors">Hue Colors</h2>
6674

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<template>
2+
<div>
3+
<md-chips class="md-primary" v-model="emails" md-placeholder="Enter a email" :md-auto-insert="true">
4+
<label>Recipients</label>
5+
</md-chips>
6+
</div>
7+
</template>
8+
9+
<script>
10+
export default {
11+
name: 'AutoInsert',
12+
data: () => ({
13+
emails: [
14+
'John.Smith@example.com',
15+
]
16+
})
17+
}
18+
</script>

src/components/MdChips/MdChips.vue

+31-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<md-field class="md-chips" :class="[$mdActiveTheme, chipsClasses]">
3-
<slot />
3+
<slot/>
44

55
<md-chip
66
v-for="(chip, key) in value"
@@ -24,7 +24,9 @@
2424
:placeholder="mdPlaceholder"
2525
@input="handleInput"
2626
@keydown.enter="insertChip"
27-
@keydown.8="handleBackRemove">
27+
@keydown.8="handleBackRemove"
28+
@focusout="handleFocusOut"
29+
>
2830
</md-input>
2931
</md-field>
3032
</template>
@@ -54,6 +56,10 @@
5456
},
5557
mdPlaceholder: [String, Number],
5658
mdStatic: Boolean,
59+
mdAutoInsert: {
60+
type: Boolean,
61+
default: false
62+
},
5763
mdLimit: Number,
5864
mdCheckDuplicated: {
5965
type: Boolean,
@@ -68,25 +74,25 @@
6874
duplicatedChip: null
6975
}),
7076
computed: {
71-
chipsClasses () {
77+
chipsClasses() {
7278
return {
7379
'md-has-value': this.value && this.value.length
7480
}
7581
},
7682
77-
modelRespectLimit () {
83+
modelRespectLimit() {
7884
return !this.mdLimit || this.value.length < this.mdLimit
7985
},
8086
81-
formattedInputValue () {
87+
formattedInputValue() {
8288
if (!this.mdFormat) {
8389
return this.inputValue
8490
}
8591
return this.mdFormat(this.inputValue)
8692
}
8793
},
8894
methods: {
89-
insertChip ({ target }) {
95+
insertChip({target}) {
9096
let inputValue = this.formattedInputValue
9197
9298
if (!inputValue || !this.modelRespectLimit) {
@@ -107,27 +113,32 @@
107113
this.$emit('md-insert', inputValue)
108114
this.inputValue = ''
109115
},
110-
removeChip (chip) {
116+
removeChip(chip) {
111117
const index = this.value.indexOf(chip)
112118
113119
this.value.splice(index, 1)
114120
this.$emit('input', this.value)
115121
this.$emit('md-delete', chip, index)
116122
this.$nextTick(() => this.$refs.input.$el.focus())
117123
},
118-
handleBackRemove () {
124+
handleBackRemove() {
119125
if (!this.inputValue) {
120126
this.removeChip(this.value[this.value.length - 1])
121127
}
122128
},
123-
handleInput () {
129+
handleInput() {
124130
if (this.mdCheckDuplicated) {
125131
this.checkDuplicated()
126132
} else {
127133
this.duplicatedChip = null
128134
}
129135
},
130-
checkDuplicated () {
136+
handleFocusOut({target}) {
137+
if (this.mdAutoInsert) {
138+
this.insertChip(target)
139+
}
140+
},
141+
checkDuplicated() {
131142
if (!this.value.includes(this.formattedInputValue)) {
132143
this.duplicatedChip = null
133144
return false
@@ -141,7 +152,7 @@
141152
}
142153
},
143154
watch: {
144-
value () {
155+
value() {
145156
this.checkDuplicated()
146157
}
147158
}
@@ -152,25 +163,25 @@
152163
@import "~components/MdAnimation/variables";
153164
154165
.md-chips.md-field {
155-
padding-top: 12px;
156-
flex-wrap: wrap;
166+
padding-top : 12px;
167+
flex-wrap : wrap;
157168
158169
&.md-has-value {
159170
label {
160-
top: -6px;
171+
top : -6px;
172+
}
161173
}
162-
}
163174
164175
.md-chip {
165-
margin-bottom: 4px;
176+
margin-bottom : 4px;
166177
167178
&:last-of-type {
168-
margin-right: 8px;
179+
margin-right : 8px;
180+
}
169181
}
170-
}
171182
172183
.md-input {
173-
min-width: 128px;
184+
min-width : 128px;
185+
}
174186
}
175-
}
176187
</style>

0 commit comments

Comments
 (0)