Move player search into PlayerList

This commit is contained in:
James Lyne 2022-01-13 11:51:06 +00:00
parent 763b4d907e
commit 9bed746ccc
2 changed files with 71 additions and 52 deletions

View File

@ -15,16 +15,22 @@
--> -->
<template> <template>
<RadioList v-if="players.length"> <input v-if="filteredPlayers && search" id="players__search" type="text" name="search"
<PlayerListItem v-for="player in players" :key="player.name" :player="player"></PlayerListItem> v-model="searchQuery" :placeholder="messagePlayersSearchPlaceholder" @keydown="onKeydown">
<RadioList v-if="filteredPlayers.length" :aria-labelledby="ariaLabelledby">
<PlayerListItem v-for="player in filteredPlayers" :key="player.name" :player="player"></PlayerListItem>
</RadioList> </RadioList>
<div v-else-if="searchQuery" class="section__skeleton">{{ messageSkeletonPlayersSearch }}</div>
<div v-else class="section__skeleton">{{ messageSkeletonPlayers }}</div>
</template> </template>
<script lang="ts"> <script lang="ts">
import PlayerListItem from "./PlayerListItem.vue"; import PlayerListItem from "./PlayerListItem.vue";
import {defineComponent} from "@vue/runtime-core"; import {computed, defineComponent} from "@vue/runtime-core";
import RadioList from "@/components/util/RadioList.vue"; import RadioList from "@/components/util/RadioList.vue";
import {LiveAtlasPlayer} from "@/index"; import {LiveAtlasPlayer} from "@/index";
import {useStore} from "@/store";
import {ref} from "vue";
export default defineComponent({ export default defineComponent({
components: { components: {
@ -32,10 +38,68 @@ export default defineComponent({
PlayerListItem PlayerListItem
}, },
props: { props: {
search: {
type: Boolean,
default: true,
},
players: { players: {
type: Object as () => LiveAtlasPlayer[], type: Object as () => LiveAtlasPlayer[],
required: true, required: true,
},
ariaLabelledby: {
type: String,
default: '',
}
},
setup(props) {
const store = useStore(),
messageSkeletonPlayers = computed(() => store.state.messages.playersSkeleton),
messageSkeletonPlayersSearch = computed(() => store.state.messages.playersSearchSkeleton),
messagePlayersSearchPlaceholder = computed(() => store.state.messages.playersSearchPlaceholder),
searchQuery = ref(""),
filteredPlayers = computed(() => {
const query = searchQuery.value.toLowerCase();
return query ? props.players.filter(p => {
return p.name.toLowerCase().indexOf(query) > -1;
}) : props.players;
}),
onKeydown = (e: KeyboardEvent) => {
e.stopImmediatePropagation();
};
return {
messageSkeletonPlayers,
messageSkeletonPlayersSearch,
messagePlayersSearchPlaceholder,
searchQuery,
filteredPlayers,
onKeydown
} }
} }
}); });
</script> </script>
<style lang="scss" scoped>
.players {
#players__search {
margin-bottom: 1.5rem;
padding: 0.5rem 1rem;
box-sizing: border-box;
width: 100%;
position: sticky;
top: 4.8rem;
z-index: 3;
& + .section__skeleton {
margin-top: 0;
}
}
}
</style>

View File

@ -18,11 +18,8 @@
<SidebarSection name="players" class="players"> <SidebarSection name="players" class="players">
<template v-slot:heading>{{ messageHeading }}</template> <template v-slot:heading>{{ messageHeading }}</template>
<template v-slot:default> <template v-slot:default>
<input v-if="players && searchEnabled" id="players__search" type="text" name="search" <PlayerList :players="players" :search="searchEnabled" aria-labelledby="players-heading">
v-model="searchQuery" :placeholder="messagePlayersSearchPlaceholder" @keydown="onKeydown"> </PlayerList>
<PlayerList v-if="filteredPlayers.length" :players="filteredPlayers" aria-labelledby="players-heading"></PlayerList>
<div v-else-if="searchQuery" class="section__skeleton">{{ messageSkeletonPlayersSearch }}</div>
<div v-else class="section__skeleton">{{ messageSkeletonPlayers }}</div>
</template> </template>
</SidebarSection> </SidebarSection>
</template> </template>
@ -30,7 +27,6 @@
<script lang="ts"> <script lang="ts">
import {computed, defineComponent} from "@vue/runtime-core"; import {computed, defineComponent} from "@vue/runtime-core";
import {useStore} from "@/store"; import {useStore} from "@/store";
import {ref} from "vue";
import SidebarSection from "@/components/sidebar/SidebarSection.vue"; import SidebarSection from "@/components/sidebar/SidebarSection.vue";
import PlayerList from "@/components/list/PlayerList.vue"; import PlayerList from "@/components/list/PlayerList.vue";
@ -43,59 +39,18 @@ export default defineComponent({
setup() { setup() {
const store = useStore(), const store = useStore(),
messageHeading = computed(() => store.getters.playersHeading), messageHeading = computed(() => store.getters.playersHeading),
messageSkeletonPlayers = computed(() => store.state.messages.playersSkeleton),
messageSkeletonPlayersSearch = computed(() => store.state.messages.playersSearchSkeleton),
messagePlayersSearchPlaceholder = computed(() => store.state.messages.playersSearchPlaceholder),
searchEnabled = computed(() => store.state.ui.playersSearch), searchEnabled = computed(() => store.state.ui.playersSearch),
searchQuery = ref(""),
players = computed(() => store.state.sortedPlayers), players = computed(() => store.state.sortedPlayers),
filteredPlayers = computed(() => { maxPlayers = computed(() => store.state.maxPlayers || 0);
const query = searchQuery.value.toLowerCase();
return query ? store.state.sortedPlayers.filter(p => {
return p.name.toLowerCase().indexOf(query) > -1;
}) : store.state.sortedPlayers;
}),
maxPlayers = computed(() => store.state.maxPlayers || 0),
onKeydown = (e: KeyboardEvent) => {
e.stopImmediatePropagation();
};
return { return {
messageHeading, messageHeading,
messageSkeletonPlayers,
messageSkeletonPlayersSearch,
messagePlayersSearchPlaceholder,
searchEnabled, searchEnabled,
searchQuery,
players, players,
filteredPlayers, maxPlayers
maxPlayers,
onKeydown
} }
} }
}); });
</script> </script>
<style lang="scss" scoped>
.players {
#players__search {
margin-bottom: 1.5rem;
padding: 0.5rem 1rem;
box-sizing: border-box;
width: 100%;
position: sticky;
top: 4.8rem;
z-index: 3;
& + .section__skeleton {
margin-top: 0;
}
}
}
</style>