Eerste goed werkende versie van een formulier in de chat input.
This commit is contained in:
@@ -1,9 +1,21 @@
|
||||
// static/js/components/ChatInput.js
|
||||
|
||||
export const ChatInput = {
|
||||
// Importeer de IconManager (als module systeem wordt gebruikt)
|
||||
// Anders moet je ervoor zorgen dat MaterialIconManager.js eerder wordt geladen
|
||||
// en iconManager beschikbaar is via window.iconManager
|
||||
|
||||
export const ChatInput = {
|
||||
name: 'ChatInput',
|
||||
components: {
|
||||
'dynamic-form': window.__vueApp ? DynamicForm : null
|
||||
'dynamic-form': window.DynamicForm
|
||||
},
|
||||
created() {
|
||||
// Als module systeem wordt gebruikt:
|
||||
// import { iconManager } from './MaterialIconManager.js';
|
||||
// Anders gebruiken we window.iconManager als het beschikbaar is:
|
||||
if (window.iconManager && this.formData && this.formData.icon) {
|
||||
window.iconManager.ensureIconsLoaded({}, [this.formData.icon]);
|
||||
}
|
||||
},
|
||||
props: {
|
||||
currentMessage: {
|
||||
@@ -29,18 +41,44 @@ export const ChatInput = {
|
||||
},
|
||||
emits: ['send-message', 'update-message', 'submit-form'],
|
||||
watch: {
|
||||
formData: {
|
||||
handler(newFormData) {
|
||||
console.log('ChatInput received formData:', newFormData);
|
||||
if (newFormData) {
|
||||
this.formValues = {}; // Reset formulierwaarden
|
||||
this.showForm = true;
|
||||
} else {
|
||||
this.showForm = false;
|
||||
'formData.icon': {
|
||||
handler(newIcon) {
|
||||
if (newIcon && window.iconManager) {
|
||||
window.iconManager.ensureIconsLoaded({}, [newIcon]);
|
||||
}
|
||||
},
|
||||
immediate: true
|
||||
},
|
||||
formData: {
|
||||
handler(newFormData, oldFormData) {
|
||||
console.log('ChatInput formData changed:', newFormData);
|
||||
|
||||
if (!newFormData) {
|
||||
console.log('FormData is null of undefined');
|
||||
this.formValues = {};
|
||||
return;
|
||||
}
|
||||
|
||||
// Controleer of velden aanwezig zijn
|
||||
if (!newFormData.fields) {
|
||||
console.error('FormData bevat geen velden!', newFormData);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Velden in formData:', newFormData.fields);
|
||||
console.log('Aantal velden:', Array.isArray(newFormData.fields)
|
||||
? newFormData.fields.length
|
||||
: Object.keys(newFormData.fields).length);
|
||||
|
||||
// Initialiseer formulierwaarden
|
||||
this.initFormValues();
|
||||
|
||||
// Log de geïnitialiseerde waarden
|
||||
console.log('Formulierwaarden geïnitialiseerd:', this.formValues);
|
||||
},
|
||||
immediate: true,
|
||||
deep: true
|
||||
},
|
||||
currentMessage(newVal) {
|
||||
this.localMessage = newVal;
|
||||
},
|
||||
@@ -52,8 +90,7 @@ export const ChatInput = {
|
||||
data() {
|
||||
return {
|
||||
localMessage: this.currentMessage,
|
||||
formValues: {},
|
||||
showForm: false
|
||||
formValues: {}
|
||||
};
|
||||
},
|
||||
computed: {
|
||||
@@ -72,20 +109,53 @@ export const ChatInput = {
|
||||
},
|
||||
|
||||
canSend() {
|
||||
const hasValidForm = this.showForm && this.formData && this.validateForm();
|
||||
const hasValidForm = this.formData && this.validateForm();
|
||||
const hasValidMessage = this.localMessage.trim() && !this.isOverLimit;
|
||||
|
||||
return (!this.isLoading) && (hasValidForm || hasValidMessage);
|
||||
},
|
||||
|
||||
sendButtonText() {
|
||||
return this.showForm ? 'Verstuur formulier' : 'Verstuur bericht';
|
||||
if (this.isLoading) {
|
||||
return 'Verzenden...';
|
||||
}
|
||||
return this.formData ? 'Verstuur formulier' : 'Verstuur bericht';
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
this.autoResize();
|
||||
// Debug informatie over formData bij initialisatie
|
||||
console.log('ChatInput mounted, formData:', this.formData);
|
||||
if (this.formData) {
|
||||
console.log('FormData bij mount:', JSON.stringify(this.formData));
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
initFormValues() {
|
||||
if (this.formData && this.formData.fields) {
|
||||
console.log('Initializing form values for fields:', this.formData.fields);
|
||||
this.formValues = {};
|
||||
|
||||
// Verwerk array van velden
|
||||
if (Array.isArray(this.formData.fields)) {
|
||||
this.formData.fields.forEach(field => {
|
||||
const fieldId = field.id || field.name;
|
||||
if (fieldId) {
|
||||
this.formValues[fieldId] = field.default !== undefined ? field.default : '';
|
||||
}
|
||||
});
|
||||
}
|
||||
// Verwerk object van velden
|
||||
else if (typeof this.formData.fields === 'object') {
|
||||
Object.entries(this.formData.fields).forEach(([fieldId, field]) => {
|
||||
this.formValues[fieldId] = field.default !== undefined ? field.default : '';
|
||||
});
|
||||
}
|
||||
|
||||
console.log('Initialized form values:', this.formValues);
|
||||
}
|
||||
},
|
||||
|
||||
handleKeydown(event) {
|
||||
if (event.key === 'Enter' && !event.shiftKey) {
|
||||
event.preventDefault();
|
||||
@@ -98,14 +168,12 @@ export const ChatInput = {
|
||||
sendMessage() {
|
||||
if (!this.canSend) return;
|
||||
|
||||
if (this.showForm && this.formData) {
|
||||
if (this.formData) {
|
||||
// Valideer het formulier
|
||||
if (this.validateForm()) {
|
||||
// Verstuur het formulier
|
||||
this.$emit('submit-form', this.formValues);
|
||||
this.formValues = {};
|
||||
// Reset het formulier na verzenden
|
||||
this.showForm = false;
|
||||
}
|
||||
} else if (this.localMessage.trim()) {
|
||||
// Verstuur normaal bericht
|
||||
@@ -113,6 +181,12 @@ export const ChatInput = {
|
||||
}
|
||||
},
|
||||
|
||||
// Deze methode houden we voor de volledigheid, maar zal niet meer direct worden aangeroepen
|
||||
cancelForm() {
|
||||
this.formValues = {};
|
||||
// We sturen geen emit meer, maar het kan nuttig zijn om in de toekomst te hebben
|
||||
},
|
||||
|
||||
validateForm() {
|
||||
if (!this.formData || !this.formData.fields) return false;
|
||||
|
||||
@@ -160,42 +234,39 @@ export const ChatInput = {
|
||||
this.focusInput();
|
||||
},
|
||||
|
||||
toggleForm() {
|
||||
this.showForm = !this.showForm;
|
||||
if (!this.showForm) {
|
||||
this.focusInput();
|
||||
}
|
||||
},
|
||||
|
||||
submitForm() {
|
||||
if (this.canSubmitForm) {
|
||||
this.$emit('submit-form', { ...this.formValues });
|
||||
this.showForm = false;
|
||||
this.focusInput();
|
||||
}
|
||||
},
|
||||
|
||||
cancelForm() {
|
||||
this.showForm = false;
|
||||
this.focusInput();
|
||||
},
|
||||
|
||||
updateFormValues(newValues) {
|
||||
this.formValues = { ...newValues };
|
||||
}
|
||||
},
|
||||
template: `
|
||||
<div class="chat-input-container">
|
||||
<div v-if="formData && showForm" class="dynamic-form-container">
|
||||
<!-- Dynamisch toevoegen van Material Symbols Outlined voor iconen -->
|
||||
<div v-if="formData && formData.icon" class="material-icons-container">
|
||||
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0,0" />
|
||||
</div>
|
||||
<!-- Dynamisch formulier container -->
|
||||
<div v-if="formData" class="dynamic-form-container" style="margin-bottom: 10px; border: 1px solid #ddd; border-radius: 8px; padding: 15px 15px 5px 15px; position: relative; background-color: #f9f9f9; box-shadow: 0 2px 4px rgba(0,0,0,0.05);">
|
||||
<!-- De titel wordt in DynamicForm weergegeven en niet hier om dubbele titels te voorkomen -->
|
||||
<div v-if="!formData.fields" style="color: red; padding: 10px;">Fout: Geen velden gevonden in formulier</div>
|
||||
<dynamic-form
|
||||
v-if="formData && formData.fields"
|
||||
:form-data="formData"
|
||||
:form-values="formValues"
|
||||
:is-submitting="isLoading"
|
||||
:hide-actions="true"
|
||||
@update:form-values="updateFormValues"
|
||||
></dynamic-form>
|
||||
|
||||
<!-- Geen extra knoppen meer onder het formulier, alles gaat via de hoofdverzendknop -->
|
||||
</div>
|
||||
|
||||
<style>
|
||||
@keyframes spin {
|
||||
0% { transform: rotate(0deg); }
|
||||
100% { transform: rotate(360deg); }
|
||||
}
|
||||
</style>
|
||||
|
||||
<div class="chat-input">
|
||||
<!-- Main input area -->
|
||||
<div class="input-main">
|
||||
@@ -218,26 +289,15 @@ export const ChatInput = {
|
||||
</div>
|
||||
|
||||
<!-- Input actions -->
|
||||
<div class="input-actions">
|
||||
<!-- Formulier toggle knop -->
|
||||
<button
|
||||
v-if="hasFormData"
|
||||
@click="toggleForm"
|
||||
class="form-toggle-btn"
|
||||
:disabled="isLoading"
|
||||
:class="{ 'active': showForm }"
|
||||
:title="showForm ? 'Verberg formulier' : 'Toon formulier'"
|
||||
>
|
||||
<i class="material-icons">description</i>
|
||||
</button>
|
||||
<div class="input-actions">
|
||||
|
||||
<!-- Send button -->
|
||||
<!-- Universele verzendknop (voor zowel berichten als formulieren) -->
|
||||
<button
|
||||
@click="sendMessage"
|
||||
class="send-btn"
|
||||
:class="{ 'form-mode': showForm && formData }"
|
||||
:class="{ 'form-mode': formData }"
|
||||
:disabled="!canSend"
|
||||
:title="showForm ? 'Verstuur formulier' : 'Verstuur bericht'"
|
||||
:title="formData ? 'Verstuur formulier' : 'Verstuur bericht'"
|
||||
>
|
||||
<span v-if="isLoading" class="loading-spinner">⏳</span>
|
||||
<svg v-else width="20" height="20" viewBox="0 0 24 24" fill="currentColor">
|
||||
|
||||
Reference in New Issue
Block a user