diff --git a/src/components/forms/DynamicForm.vue b/src/components/forms/DynamicForm.vue index 97bb2b0..0cd9cd5 100644 --- a/src/components/forms/DynamicForm.vue +++ b/src/components/forms/DynamicForm.vue @@ -55,7 +55,7 @@ const fetchConfig = async () => { formData[field.key] = []; } else if (field.type === "phone_country") { formData[field.key] = ""; - phoneParts[field.key] = { code: "", number: "" }; + phoneParts[field.key] = { code: "", number: "", flag: "" }; } else { formData[field.key] = ""; } @@ -305,13 +305,6 @@ const onPhoneInput = (key, event) => { formatErrors[key] = false; }; -const onPhoneCountryCodeChange = (key, code) => { - if (!phoneParts[key]) return; - phoneParts[key].code = code; - formData[key] = code + phoneParts[key].number; - stepErrors[key] = false; -}; - const onPhoneCountryNumberInput = (key, event) => { if (!phoneParts[key]) return; const cleaned = event.target.value.replace(/\D/g, ""); @@ -321,8 +314,56 @@ const onPhoneCountryNumberInput = (key, event) => { stepErrors[key] = false; }; +const getPhoneCountryDisplay = (key) => { + if (phoneCountrySearchText[key] !== undefined && phoneCountrySearchText[key] !== null) { + return phoneCountrySearchText[key]; + } + if (!phoneParts[key] || !phoneParts[key].code) return ""; + return phoneParts[key].flag + ? `${phoneParts[key].code}` + : phoneParts[key].code; +}; + +const filteredPhoneCountries = (field, key) => { + const text = (phoneCountrySearchText[key] || "").toLowerCase().trim(); + const list = suggestionsCache.value[field.source] || []; + if (!text) return list; + return list.filter( + item => item.label.toLowerCase().includes(text) || item.dial_code.includes(text) + ); +}; + +const onPhoneCountrySearchFocus = (field, key) => { + loadSuggestions(field.source); + showCountrySuggestions[key] = true; +}; + +const onPhoneCountrySearchBlur = (key) => { + setTimeout(() => { + showCountrySuggestions[key] = false; + phoneCountrySearchText[key] = undefined; + }, 200); +}; + +const onPhoneCountrySearchInput = (key, event) => { + phoneCountrySearchText[key] = event.target.value; + showCountrySuggestions[key] = true; +}; + +const onPhoneCountrySelect = (key, item) => { + phoneParts[key].code = item.dial_code; + phoneParts[key].flag = item.flag; + phoneParts[key].number = phoneParts[key].number || ""; + formData[key] = item.dial_code + phoneParts[key].number; + phoneCountrySearchText[key] = undefined; + showCountrySuggestions[key] = false; + stepErrors[key] = false; +}; + const suggestionsCache = ref({}); const showSuggestions = reactive({}); +const phoneCountrySearchText = reactive({}); +const showCountrySuggestions = reactive({}); const loadSuggestions = async (source) => { if (suggestionsCache.value[source]) return; @@ -583,18 +624,39 @@ const getFieldInputType = (field) => {