Fix reference to full formatted address
This commit is contained in:
parent
99dc9a08c0
commit
3c43f72028
1 changed files with 49 additions and 47 deletions
|
|
@ -5,7 +5,7 @@ class AddressInput extends HTMLElement {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.attachShadow({mode: "open" });
|
this.attachShadow({ mode: "open" });
|
||||||
this.internals = this.attachInternals();
|
this.internals = this.attachInternals();
|
||||||
this.render();
|
this.render();
|
||||||
|
|
||||||
|
|
@ -30,28 +30,28 @@ class AddressInput extends HTMLElement {
|
||||||
|
|
||||||
// Lifecycle: when element is removed from the DOM
|
// Lifecycle: when element is removed from the DOM
|
||||||
disconnectedCallback() {
|
disconnectedCallback() {
|
||||||
this._input.removeEventListener('input', this._handleInput);
|
this._input.removeEventListener("input", this._handleInput);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lifecycle: watch these attributes for changes
|
// Lifecycle: watch these attributes for changes
|
||||||
static get observedAttributes() {
|
static get observedAttributes() {
|
||||||
return ['placeholder', 'api-key'];
|
return ["placeholder", "api-key"];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lifecycle: respond to attribute changes
|
// Lifecycle: respond to attribute changes
|
||||||
attributeChangedCallback(name, oldValue, newValue) {
|
attributeChangedCallback(name, oldValue, newValue) {
|
||||||
if (name === 'placeholder' && this._input) {
|
if (name === "placeholder" && this._input) {
|
||||||
this._input.placeholder = newValue;
|
this._input.placeholder = newValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (name === 'api-key') {
|
if (name === "api-key") {
|
||||||
this._apiKey = newValue;
|
this._apiKey = newValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Properties API
|
// Properties API
|
||||||
get value() {
|
get value() {
|
||||||
return this._input ? this._input.value : '';
|
return this._input ? this._input.value : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
set value(val) {
|
set value(val) {
|
||||||
|
|
@ -63,7 +63,7 @@ class AddressInput extends HTMLElement {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Private methods
|
// Private methods
|
||||||
_handleInput(event) {
|
_handleInput(event) {
|
||||||
const searchText = event.target.value.trim();
|
const searchText = event.target.value.trim();
|
||||||
|
|
||||||
|
|
@ -72,18 +72,16 @@ class AddressInput extends HTMLElement {
|
||||||
|
|
||||||
// Clear suggestions if input is less than 3 characters
|
// Clear suggestions if input is less than 3 characters
|
||||||
if (searchText.length < 3) {
|
if (searchText.length < 3) {
|
||||||
this._suggestions.innerHTML = '';
|
this._suggestions.innerHTML = "";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debounce API calls (wait 300ms after typing stops)
|
// Debounce API calls (wait 300ms after typing stops)
|
||||||
this._debounceTimer = setTimeout(() => {
|
this._debounceTimer = setTimeout(() => {
|
||||||
this._fetchAddressSuggestions(searchText)
|
this._fetchAddressSuggestions(searchText).then((response) => {
|
||||||
.then(response => {
|
this._renderSuggestions(response.features);
|
||||||
this._renderSuggestions(response.features);
|
});
|
||||||
});
|
|
||||||
}, 300);
|
}, 300);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async _fetchAddressSuggestions(text) {
|
async _fetchAddressSuggestions(text) {
|
||||||
|
|
@ -94,55 +92,59 @@ class AddressInput extends HTMLElement {
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
return data;
|
return data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching geocoding suggestions:', error);
|
console.error("Error fetching geocoding suggestions:", error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_renderSuggestions(suggestions) {
|
_renderSuggestions(suggestions) {
|
||||||
console.log("Rendering suggestions", suggestions);
|
console.log("Rendering suggestions", suggestions);
|
||||||
this._suggestions.innerHTML = suggestions.map((item, index) => {
|
this._suggestions.innerHTML = suggestions
|
||||||
if (item.properties.place_formatted != "") {
|
.map((item, index) => {
|
||||||
return `
|
if (item.properties.place_formatted != "") {
|
||||||
|
return `
|
||||||
<div class="suggestion-item list-group-item"
|
<div class="suggestion-item list-group-item"
|
||||||
data-index="${index}"
|
data-index="${index}"
|
||||||
data-lat="${item.geometry.coordinates[1]}"
|
data-lat="${item.geometry.coordinates[1]}"
|
||||||
data-lng="${item.geometry.coordinates[0]}">
|
data-lng="${item.geometry.coordinates[0]}">
|
||||||
<div class="main-address">${item.properties.name || item.properties.full_address}</div>
|
<div class="main-address">${item.properties.name || item.properties.full_address}</div>
|
||||||
<div class="place-info">${item.properties.place_formatted}</div>
|
<div class="place-info">${item.properties.place_formatted}</div>
|
||||||
</div>`
|
</div>`;
|
||||||
} else {
|
} else {
|
||||||
return `
|
return `
|
||||||
<div class="suggestion-item list-group-item"
|
<div class="suggestion-item list-group-item"
|
||||||
data-index="${index}"
|
data-index="${index}"
|
||||||
data-lat="${item.coordinates.lat}"
|
data-lat="${item.coordinates.lat}"
|
||||||
data-lng="${item.coordinates.lng}">
|
data-lng="${item.coordinates.lng}">
|
||||||
<div class="main-address">${item.properties.name || item.properties.full_address}</div>
|
<div class="main-address">${item.properties.name || item.properties.full_address}</div>
|
||||||
<div class="place-info">${item.properties.place_formatted}</div>
|
<div class="place-info">${item.properties.place_formatted}</div>
|
||||||
</div>`
|
</div>`;
|
||||||
}
|
}
|
||||||
}).join('');
|
})
|
||||||
|
.join("");
|
||||||
|
|
||||||
// Add click listeners to suggestions
|
// Add click listeners to suggestions
|
||||||
this.shadowRoot.querySelectorAll('.suggestion-item').forEach(el => {
|
this.shadowRoot.querySelectorAll(".suggestion-item").forEach((el) => {
|
||||||
el.addEventListener('click', e => {
|
el.addEventListener("click", (e) => {
|
||||||
const index = parseInt(el.dataset.index);
|
const index = parseInt(el.dataset.index);
|
||||||
const suggestion = suggestions[index];
|
const suggestion = suggestions[index];
|
||||||
this.SetValue(suggestion);
|
this.SetValue(suggestion);
|
||||||
// Dispatch custom event
|
// Dispatch custom event
|
||||||
this.dispatchEvent(new CustomEvent('address-selected', {
|
this.dispatchEvent(
|
||||||
bubbles: true,
|
new CustomEvent("address-selected", {
|
||||||
composed: true, // Allows event to cross shadow DOM boundary
|
bubbles: true,
|
||||||
detail: {
|
composed: true, // Allows event to cross shadow DOM boundary
|
||||||
location: suggestion
|
detail: {
|
||||||
}
|
location: suggestion,
|
||||||
}));
|
},
|
||||||
|
}),
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initial render of component
|
// Initial render of component
|
||||||
render() {
|
render() {
|
||||||
const placeholder = this.getAttribute('placeholder') || 'Enter address';
|
const placeholder = this.getAttribute("placeholder") || "Enter address";
|
||||||
|
|
||||||
this.shadowRoot.innerHTML = `
|
this.shadowRoot.innerHTML = `
|
||||||
<style>
|
<style>
|
||||||
|
|
@ -192,15 +194,15 @@ class AddressInput extends HTMLElement {
|
||||||
// Public methods
|
// Public methods
|
||||||
clear() {
|
clear() {
|
||||||
if (this._input) {
|
if (this._input) {
|
||||||
this._input.value = '';
|
this._input.value = "";
|
||||||
this._suggestions.innerHTML = '';
|
this._suggestions.innerHTML = "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SetValue(suggestion) {
|
SetValue(suggestion) {
|
||||||
this.value = suggestion.properties.full_address;
|
this.value = suggestion.properties.formatted_address_line;
|
||||||
this._suggestions.innerHTML = '';
|
this._suggestions.innerHTML = "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
customElements.define('address-input', AddressInput);
|
customElements.define("address-input", AddressInput);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue