Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(game-lobby): auto-focus on player name input even after changing page #873

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<PrimeVueFloatLabel>
<PrimeVueInputText
id="player-name-input"
ref="playerNameInput"
v-model="inputValue"
aria-labelledby="player-name-input-help"
:autofocus="!isOnTouchDevice"
Expand Down Expand Up @@ -52,7 +53,7 @@
<script lang="ts" setup>
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
import { storeToRefs } from "pinia";
import { computed } from "vue";
import { type ComponentPublicInstance, computed } from "vue";

import { MAX_PLAYERS_IN_GAME } from "~/composables/api/game/constants/game.constants";
import { MAX_PLAYER_NAME_LENGTH } from "~/composables/api/game/constants/player/player.constants";
Expand All @@ -69,6 +70,8 @@ const { createGameDto } = storeToRefs(createGameDtoStore);

const inputValue = defineModel<string>({ required: true });

const playerNameInput = ref<ComponentPublicInstance | null>(null);

const doesPlayerNameExistInGame = computed<boolean>(() => createGameDto.value.players.some(({ name }) => name === inputValue.value.trim()));

const hasPlayerNameReachedMaxLength = computed<boolean>(() => inputValue.value.trim().length >= MAX_PLAYER_NAME_LENGTH);
Expand All @@ -92,5 +95,16 @@ const playerNameInputHelpText = computed<string>(() => {
return t("components.GameLobbyPlayerInput.pleaseEnterPlayerName");
});

function focusOnPlayerNameInput(): void {
if (!playerNameInput.value) {
throw createError("Player name input is not defined");
}
if (!isOnTouchDevice.value) {
(playerNameInput.value.$el as HTMLElement).focus();
}
}

onMounted(focusOnPlayerNameInput);

defineExpose({ isAddButtonDisabled });
</script>
Loading
Loading