Fixed defect CSR-934 as well as fixed other issues made apparent by the fix

This commit is contained in:
Leah Schumann 2022-12-12 17:14:38 -05:00
parent 46decae726
commit 0402d8469e

View file

@ -12,7 +12,8 @@
aria-haspopup="" aria-haspopup=""
hasIcon hasIcon
disableAutoFill disableAutoFill
validationRules="street-address-required" /> validationRules="street-address-required"
@keydown.enter.prevent />
</div> </div>
</div> </div>
<transition name="fade" mode="out-in"> <transition name="fade" mode="out-in">
@ -109,6 +110,9 @@ export default {
alertCopyVerificationWarning: "", alertCopyVerificationWarning: "",
alertHeadlineNoMatchWarning: "", alertHeadlineNoMatchWarning: "",
alertCopyNoMatchWarning: "", alertCopyNoMatchWarning: "",
matchingIndirectly: false,
matchFound: false,
enterPressed: false
}; };
}, },
computed: { computed: {
@ -213,11 +217,11 @@ export default {
fillInAddress fillInAddress
); );
addressField1.addEventListener("focus", () => {
// Wrapping the addressField1 element in the Google Address Autocomplete object // Wrapping the addressField1 element in the Google Address Autocomplete object
// will cause "autocomplete='off'" which Chrome completely ignores. This event // will cause "autocomplete='off'" which Chrome completely ignores. This event
// handler will set the value to something arbitrary so autofill doesn't work. // handler will set the value to something arbitrary so autofill doesn't work.
// https://stackoverflow.com/a/30976223 // https://stackoverflow.com/a/30976223
addressField1.addEventListener("focus", () => {
addressField1.setAttribute("autocomplete", "do-not-autofill"); addressField1.setAttribute("autocomplete", "do-not-autofill");
// Make place results box stick to the input on scroll // Make place results box stick to the input on scroll
@ -229,27 +233,56 @@ export default {
} }
}); });
addressField1.addEventListener("keydown", (e) => {
if (e.code === "Enter" || e.code === "NumpadEnter" || e.code === "Tab") {
if (e.code === "Tab") {
self.matchingIndirectly = true;
} else {
self.enterPressed = true;
}
addressField1.blur();
} else {
return;
}
});
addressField1.addEventListener("change", () => { addressField1.addEventListener("change", () => {
const hover = document.querySelector(".pac-container .pac-item:hover"); // NOTE: The standard "place_changed" handler of the autocomplete will use the address the user had chosen
// if an item has been clicked, do nothing, otherwise get first solution and use Geocoder to get the place // using either the down / up arrows or the address the user was hovering over when they pressed "Enter."
if (hover === null) {
// If there has already been a match found then do nothing
// OR
// If the user pressed "Enter" then do nothing
if (self.matchFound || self.enterPressed) {
return;
}
// Get the address that the user clicked on (if any)
const clickedAddress = document.querySelector(".pac-container .pac-item:hover");
// If the Street Address field changed without clicking (by pressing Tab, or clicking outside the field)
if (clickedAddress === null) {
// Select for the user, fill-in the address using first item in the list.
const item = document.querySelector(".pac-container .pac-item"); const item = document.querySelector(".pac-container .pac-item");
if (item != null) { if (item != null) {
self.matchingIndirectly = true;
const firstResult = item.textContent; const firstResult = item.textContent;
const geocoder = new window.google.maps.Geocoder(); const geocoder = new window.google.maps.Geocoder();
geocoder.geocode( geocoder.geocode({
{
address: firstResult, address: firstResult,
}, },
function (results, status) { function (results, status) {
if (status === window.google.maps.GeocoderStatus.OK) { if (status === window.google.maps.GeocoderStatus.OK) {
fillInAddress(results[0]); fillInAddress(results[0]);
self.displayVerificationWarning = true;
self.displayNoMatchWarning = false; self.displayNoMatchWarning = false;
} }
} }
); );
} else { } else {
// If there are no addresses found for the input then display empty address fields and warnings
self.addressModel.city = ""; self.addressModel.city = "";
self.addressModel.state = ""; self.addressModel.state = "";
self.addressModel.zipCode = ""; self.addressModel.zipCode = "";
@ -258,6 +291,7 @@ export default {
self.displayNoMatchWarning = true; self.displayNoMatchWarning = true;
} }
} }
}); });
function fillInAddress(place) { function fillInAddress(place) {
@ -266,7 +300,9 @@ export default {
} }
if (place && place.address_components) { if (place && place.address_components) {
self.matchFound = true;
self.addressModel.streetAddress = ""; self.addressModel.streetAddress = "";
self.$nextTick(function () {
self.showAddressFields = true; self.showAddressFields = true;
for (const component of place.address_components) { for (const component of place.address_components) {
@ -278,8 +314,7 @@ export default {
break; break;
} }
case "route": { case "route": {
self.addressModel.streetAddress += self.addressModel.streetAddress += " " + component.short_name;
" " + component.short_name;
break; break;
} }
case "locality": { case "locality": {
@ -297,20 +332,24 @@ export default {
} }
} }
self.displayVerificationWarning = false; self.displayVerificationWarning = self.matchingIndirectly;
self.displayNoMatchWarning = false;
} else {
self.displayVerificationWarning = true;
self.displayNoMatchWarning = false;
}
// after showing the address fields, disable the address autocomplete // after showing the address fields, disable the address autocomplete
window.google.maps.event.removeListener(autocompleteListener); window.google.maps.event.removeListener(autocompleteListener);
window.google.maps.event.clearInstanceListeners(autocomplete); window.google.maps.event.clearInstanceListeners(autocomplete);
addressField1.onchange = null; addressField1.onchange = null;
const pacContainer = document.querySelector(".pac-container"); const pacContainer = document.querySelector(".pac-container");
if (pacContainer) {
pacContainer.remove(); pacContainer.remove();
} }
});
} else {
self.displayVerificationWarning = true;
self.displayNoMatchWarning = false;
}
}
}) })
.catch(() => { .catch(() => {
// Failed to fetch script // Failed to fetch script
@ -322,15 +361,13 @@ export default {
this.setupAddressLookup(); this.setupAddressLookup();
}, },
watch: { watch: {
addressModel: { matchFound: {
handler(newValue) { handler(newValue) {
// Clear no match warning on address change if (newValue) {
if (newValue.city || newValue.state || newValue.zipCode) {
this.displayNoMatchWarning = false; this.displayNoMatchWarning = false;
} }
}, },
deep: true, }
},
}, },
components: { components: {
textboxQuestion, textboxQuestion,