Skip to content

Commit aaf494d

Browse files
author
Mirko
committed
Refactor pull request fritx#108
1 parent 4a9473f commit aaf494d

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.Idea
12
.DS_Store
23
node_modules*/
34
dist/

src/At.vue

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,17 @@ export default {
5454
type: Array,
5555
default: () => []
5656
},
57+
emptyMembersMessage: {
58+
type: String,
59+
default: 'There is no one to tag'
60+
},
5761
nameKey: {
5862
type: String,
5963
default: ''
6064
},
6165
filterMatch: {
6266
type: Function,
63-
default: (name, chunk, at) => {
67+
default: (name = this.value, chunk, at) => {
6468
// match at lower-case
6569
return name.toLowerCase()
6670
.indexOf(chunk.toLowerCase()) > -1
@@ -83,6 +87,7 @@ export default {
8387
// at[v-model] mode should be on only when
8488
// initial :value/v-model is present (not nil)
8589
bindsValue: this.value != null,
90+
allMembers: this.members,
8691
customsEmbedded: false,
8792
hasComposition: false,
8893
atwho: null
@@ -124,7 +129,7 @@ export default {
124129
})
125130
}
126131
},
127-
members () {
132+
allMembers () {
128133
this.handleInput(true)
129134
},
130135
value (value, oldValue) {
@@ -210,13 +215,13 @@ export default {
210215
return
211216
}
212217
213-
const { atItems, members, suffix, deleteMatch, itemName } = this
218+
const { atItems, allMembers, suffix, deleteMatch, itemName } = this
214219
const text = range.toString()
215220
const { at, index } = getAtAndIndex(text, atItems)
216221
217222
if (index > -1) {
218223
const chunk = text.slice(index + at.length)
219-
const has = members.some(v => {
224+
const has = allMembers.some(v => {
220225
const name = itemName(v)
221226
return deleteMatch(name, chunk, suffix)
222227
})
@@ -314,11 +319,11 @@ export default {
314319
if (!show) {
315320
this.closePanel()
316321
} else {
317-
const { members, filterMatch, itemName } = this
322+
const { allMembers, filterMatch, itemName } = this
318323
if (!keep && chunk) { // fixme: should be consistent with AtTextarea.vue
319324
this.$emit('at', chunk)
320325
}
321-
const matched = members.filter(v => {
326+
const matched = allMembers.filter(v => {
322327
const name = itemName(v)
323328
return filterMatch(name, chunk, at)
324329
})
@@ -357,7 +362,7 @@ export default {
357362
this.atwho = {
358363
range,
359364
offset,
360-
list,
365+
list: list.length > 0 ? list : [],
361366
x: rect.left,
362367
y: rect.top - 4,
363368
cur: 0 // todo: 尽可能记录
@@ -371,9 +376,17 @@ export default {
371376
},
372377
373378
scrollToCur () {
374-
const curEl = this.$refs.cur[0]
375-
const scrollParent = curEl.parentElement.parentElement // .atwho-view
376-
scrollIntoView(curEl, scrollParent)
379+
if(this.$refs.cur) {
380+
/**
381+
* checks that the fist element of the list is defined
382+
* and avoid making this function error out
383+
*/
384+
if (this.$refs.cur[0]) {
385+
const curEl = this.$refs.cur[0]
386+
const scrollParent = curEl.parentElement.parentElement // .atwho-view
387+
scrollIntoView(curEl, scrollParent)
388+
}
389+
}
377390
},
378391
selectByMouse (e) {
379392
const el = closest(e.target, d => {
@@ -386,7 +399,7 @@ export default {
386399
}
387400
},
388401
selectByKeyboard (e) {
389-
const offset = e.keyCode === 38 ? -1 : 1
402+
const offset = e.keyCode === 38 || e.keyCode === 13 ? -1 : 1
390403
const { cur, list } = this.atwho
391404
const nextCur = this.loop
392405
? (cur + offset + list.length) % list.length

src/AtTemplate.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
>
1515
<div class="atwho-inner">
1616
<div class="atwho-view">
17-
<ul class="atwho-ul">
17+
<ul class="atwho-ul" v-if="atwho.list.length > 0">
1818
<li v-for="(item, index) in atwho.list"
1919
class="atwho-li"
2020
:key="index"
@@ -29,6 +29,13 @@
2929
</slot>
3030
</li>
3131
</ul>
32+
<ul class="atwho-ul" v-else>
33+
<li class="atwho-li">
34+
<slot name="emptyList">
35+
<span v-text="emptyMembersMessage"></span>
36+
</slot>
37+
</li>
38+
</ul>
3239
</div>
3340
</div>
3441
</div>

0 commit comments

Comments
 (0)