Implement user search in vue

This commit is contained in:
Pavel Djundik 2018-07-08 22:02:36 +03:00 committed by Pavel Djundik
parent c20af6329b
commit 1e09ccdb90
2 changed files with 68 additions and 11 deletions

View file

@ -6,24 +6,37 @@
type="search"
class="search"
aria-label="Search among the user list"
tabindex="-1">
tabindex="-1"
v-model="userSearchInput"
>
</div>
<div class="names">
<div
v-for="(users, mode) in groupedUsers"
:key="mode"
:class="['user-mode', getModeClass(mode)]">
<Username
v-for="user in users"
:key="user.nick"
:user="user"/>
:class="['user-mode', getModeClass(mode)]"
>
<template v-if="userSearchInput.length > 0">
<UsernameFiltered
v-for="user in users"
:key="user.original.nick"
:user="user"/>
</template>
<template v-else>
<Username
v-for="user in users"
:key="user.nick"
:user="user"/>
</template>
</div>
</div>
</aside>
</template>
<script>
const fuzzy = require("fuzzy");
import Username from "./Username.vue";
import UsernameFiltered from "./UsernameFiltered.vue";
const modes = {
"~": "owner",
@ -39,19 +52,45 @@ export default {
name: "ChatUserList",
components: {
Username,
UsernameFiltered,
},
props: {
channel: Object,
},
data: function() {
return {
userSearchInput: "",
};
},
computed: {
groupedUsers() {
const groups = {};
for (const user of this.channel.users) {
if (!groups[user.mode]) {
groups[user.mode] = [user];
} else {
groups[user.mode].push(user);
if (this.userSearchInput) {
const result = fuzzy.filter(
this.userSearchInput,
this.channel.users,
{
pre: "<b>",
post: "</b>",
extract: (u) => u.nick,
}
);
for (const user of result) {
if (!groups[user.original.mode]) {
groups[user.original.mode] = [];
}
groups[user.original.mode].push(user);
}
} else {
for (const user of this.channel.users) {
if (!groups[user.mode]) {
groups[user.mode] = [user];
} else {
groups[user.mode].push(user);
}
}
}
@ -62,6 +101,9 @@ export default {
getModeClass(mode) {
return modes[mode];
},
onInput(e) {
console.log(this.channel.userSearchInput);
},
},
};
</script>

View file

@ -0,0 +1,15 @@
<template>
<span
:class="['user', $options.filters.colorClass(user.original.nick)]"
:data-name="user.original.nick"
role="button" v-html="user.original.mode + user.string"/>
</template>
<script>
export default {
name: "UsernameFiltered",
props: {
user: Object,
},
};
</script>