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