Problem:
When pressing the Enter key in the v2-text-field ("Wprowadź kod ICD-10"), the request to /api/services/app/IcdCodes/SearchIcd is not being sent. This likely stems from how Vue handles event listeners on custom components.
Root Cause:
In Vue 2, custom components do not automatically emit native DOM events. Instead, they emit their own component-specific events. To listen to native DOM events on a custom component, you need to use the .native modifier. Without this modifier, the event listener (@keyup.enter) is looking for an event emitted by the V2TextField component itself, not the native DOM event from the internal input element.
Solution:
Add the .native modifier to the @keyup.enter event listener.
This ensures that the handleIcdCodeInput method is triggered when the Enter key is pressed within the input field of the V2TextField component.
Updated Code Snippet:
<v2-text-field
v-model="icdCodeInput"
label="Wprowadź kod ICD-10"
placeholder="np. A00.0"
:filled="true"
class="mt-2"
@keyup.enter.native="handleIcdCodeInput"
/>
Full Component with the Fix Applied:
<template>
<div>
<!-- Other components and layout code -->
<v-col cols="12" md="4">
<icd-code-list
v-model="model.diagnoses"
title="Rozpoznania"
:readonly="readonly"
use-remarks
type="Icd10"
class="mt-2">
<template #actions="{ item }">
<slot name="diagnose-actions" :item="item"></slot>
</template>
</icd-code-list>
<v2-text-field
v-model="icdCodeInput"
label="Wprowadź kod ICD-10"
placeholder="np. A00.0"
:filled="true"
class="mt-2"
@keyup.enter.native="handleIcdCodeInput"/> <!-- Added .native here -->
</v-col>
<!-- Rest of the template -->
</div>
</template>
<script lang="ts">
import { defineComponent, ref, computed, watch, inject, onMounted, onBeforeUpdate } from "@vue/composition-api";
import axios from "@/lib/axios";
// ... other imports
const ProcessVisitCoreCard = defineComponent({
components: {
// ... component registrations
},
props: {
// ... props definition
},
emits: [
// ... emits definition
],
setup(props, context) {
// ... other setup code
const icdCodeInput = ref("");
const handleIcdCodeInput = async () => {
const code = icdCodeInput.value.trim().toUpperCase();
if (!code) return;
try {
const response = await axios.post("/api/services/app/IcdCodes/SearchIcd", {
searchPhrase: code
});
if (response.status !== 200) {
throw new Error("Błąd sieciowy.");
}
const data = response.data.result.items as any[];
const foundItemDto = data.find(item => item.code.toUpperCase() === code);
if (foundItemDto) {
const newIcdCode = IcdCode.fromDto(foundItemDto);
model.value.diagnoses.push(newIcdCode);
icdCodeInput.value = "";
} else {
alert("Nie znaleziono rozpoznania dla podanego kodu ICD-10.");
}
} catch (error) {
console.error(error);
alert("Wystąpił błąd podczas wyszukiwania kodu ICD-10.");
}
};
return {
// ... other returned properties
icdCodeInput,
handleIcdCodeInput
};
}
});
export default ProcessVisitCoreCard;
</script>
Alternative Considerations:
-
Component Emitting Custom Events:
If V2TextField is designed to emit its own event when the Enter key is pressed (e.g., @enter), you should listen to that specific event instead. Check the V2TextField documentation or implementation to verify available events.
-
Using a Wrapper or Ref:
If adding .native doesn't resolve the issue, another approach is to directly access the input element using a ref and attach the event listener manually.
-
Ensure handleIcdCodeInput is Correctly Defined:
Double-check that the handleIcdCodeInput method is correctly defined and accessible within the component's scope. In your provided code, it appears correctly defined and returned from the setup function.
Conclusion:
The primary issue is related to how Vue 2 handles events on custom components. By adding the .native modifier to the event listener, you bridge the gap between the native DOM events and Vue's event system, ensuring that your handler is invoked as expected when the Enter key is pressed.