Skip to content

Commit

Permalink
feat: SelectService avec une largeur variable (#883)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matteo-OCTO authored Jan 27, 2025
1 parent 74cc03f commit b762341
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 14 deletions.
70 changes: 57 additions & 13 deletions src/components/custom/Service/ServiceSelect.vue
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
<template>
<span ref="mesureurElement" v-text="mesureurTexte" class="hidden-text"></span>

<label :for="id" class="fr-sr-only">{{ label }}</label>
<select class="service-select" :id="id" :name="id" @input="updateMois">
<option
v-for="option in options"
:key="option.code"
:value="option.code"
:selected="option.estLaCategorieParDefaut"
>
<select
ref="selectElement"
v-model="optionSelectionnee"
class="service-select"
:id="id"
:name="id"
@change="updateWidth"
>
<option v-for="option in options" :key="option.code" :value="option.code">
{{ option.label }}
</option>
</select>
</template>

<script setup lang="ts">
defineProps<{
import { computed, nextTick, onMounted, ref, watch } from 'vue';
const SELECT_RIGHT_PADDING = 40;
const props = defineProps<{
id: string;
label: string;
options: {
Expand All @@ -22,13 +30,41 @@
estLaCategorieParDefaut: boolean;
}[];
}>();
const emit = defineEmits<{ (e: 'update', value: string): void }>();
const updateMois = (event: Event) => {
const inputElement = event.target as HTMLInputElement;
emit('update', inputElement.value);
};
const mesureurTexte = ref<string>('');
const mesureurElement = ref<HTMLElement | null>(null);
const selectElement = ref<HTMLElement | null>(null);
const optionSelectionneParDefaut = props.options.find(option => option.estLaCategorieParDefaut);
const optionSelectionnee = ref(optionSelectionneParDefaut?.code);
const texteDeLOptionSelectionnee = computed(() => {
return props.options.find(option => option.code === optionSelectionnee.value)?.label ?? '';
});
watch(texteDeLOptionSelectionnee, () => {
mesureurTexte.value = texteDeLOptionSelectionnee.value;
});
watch(optionSelectionnee, value => {
emit('update', value ?? '');
});
async function updateWidth() {
if (mesureurElement.value) {
await nextTick();
const width = mesureurElement.value.offsetWidth + SELECT_RIGHT_PADDING;
if (selectElement.value) {
selectElement.value.style.width = width + 'px';
}
}
}
onMounted(() => {
mesureurTexte.value = texteDeLOptionSelectionnee.value;
updateWidth();
});
</script>

<style>
Expand All @@ -46,4 +82,12 @@
.service-select option {
font-size: 1.25rem;
}
.hidden-text {
position: absolute;
visibility: hidden;
white-space: nowrap;
pointer-events: none;
overflow: hidden;
}
</style>
1 change: 0 additions & 1 deletion src/components/pages/PagesService/PageServiceRecettes.vue
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
});
const lancerRecherche = async () => {
isLoading.value = true;
await usecase.execute(
utilisateurStore().utilisateur.id,
typeDeRecettes.value,
Expand Down

0 comments on commit b762341

Please sign in to comment.