11<template >
2- <div v-clickaway =" handleClickAway " class =" relative" >
2+ <div v-clickaway =" closePopUp " class =" relative" >
33 <BaseButton
4- ref =" filterButton"
54 variant =" text"
65 icon =" filter"
76 aria-label =" filter button"
1514 role =" dialog"
1615 @click.stop
1716 >
18- <fieldset id =" radio-group-action" class =" space-y-3" >
17+ <fieldset id =" radio-group-action" class =" space-y-3" role = " radiogroup " >
1918 <BaseRadioInput
2019v-for =" option in filterOptions"
2120 :id =" `${name}-${option}`"
2221 :key =" option"
2322 :name =" name"
24- :label =" option.charAt(0).toUpperCase() + option.slice(1 )"
23+ :label =" capitalizeOption( option)"
2524 :value =" option"
2625 :checked =" filterOption === option"
2726 @select-value =" handleSelectFilter"
28- @escape =" handleClickAway"
2927 />
3028 </fieldset >
3129 </div >
@@ -34,11 +32,10 @@ v-for="option in filterOptions"
3432<script setup lang="ts">
3533import BaseButton from ' @/components/base/BaseButton.vue' ;
3634import BaseRadioInput from ' @/components/base/BaseRadioInput.vue' ;
37- import { ref , useTemplateRef } from ' vue' ;
35+ import { ref , onMounted , onUnmounted } from ' vue' ;
3836import { onClickaway } from ' @/directives/clickAway' ;
3937
4038const vClickaway = onClickaway ;
41- const filterButton = useTemplateRef (' filterButton' );
4239
4340const props = defineProps <{
4441 filterOptions: string [];
@@ -51,14 +48,31 @@ const emits = defineEmits(['update-filter-option']);
5148const showAlertFilterPopup = ref (false );
5249const filterOption = ref (props .defaultFilterOption );
5350
54- function handleClickAway() {
51+ onMounted (() => {
52+ document .addEventListener (' keydown' , handleEscapeKey );
53+ });
54+
55+ onUnmounted (() => {
56+ document .removeEventListener (' keydown' , handleEscapeKey );
57+ });
58+
59+ function capitalizeOption(option : string ) {
60+ return ` ${option .charAt (0 ).toUpperCase ()}${option .slice (1 )} ` ;
61+ }
62+
63+ function closePopUp() {
5564 showAlertFilterPopup .value = false ;
5665}
5766
67+
68+ function handleEscapeKey(event : KeyboardEvent ) {
69+ if (event .key === ' Escape' && showAlertFilterPopup .value ) {
70+ closePopUp ();
71+ }
72+ }
73+
5874function handleSelectFilter(option : string ) {
5975 filterOption .value = option ;
60- showAlertFilterPopup .value = false ;
6176 emits (' update-filter-option' , option );
62- filterButton .value ?.$el ?.focus ();
6377}
6478 </script >
0 commit comments