Skip to content

Commit

Permalink
fix(frontend): prevent re-cloning of find button in geo input
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Apr 4, 2024
1 parent b2cb7f1 commit 7168c47
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
35 changes: 20 additions & 15 deletions packages/frontend/components/geo-input/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,41 @@ export const GeoInputFieldComponent = class extends HTMLElement {
constructor() {
super();

this.$geoInput = this.querySelector("input");
this.$geoInput = this.querySelector(".geo-input");
this.$geoInputButton = this.querySelector(".geo-input__button");
this.$geoInputButtonTemplate = this.querySelector("#geo-input-button");
this.$errorMessageTemplate = this.querySelector("#error-message");

this.i18nDenied = this.getAttribute("i18n-denied");
this.i18nFailed = this.getAttribute("i18n-failed");

this.$findButtonTemplate = this.querySelector("#find-button");
this.$errorMessageTemplate = this.querySelector("#error-message");
}

connectedCallback() {
if (!navigator.geolocation) {
return;
}

// Create group to hold input and button
const $inputButtonGroup = document.createElement("div");
$inputButtonGroup.classList.add("input-button-group");
if (!this.$geoInputButton) {
// Create group to hold input and button
const $inputButtonGroup = document.createElement("div");
$inputButtonGroup.classList.add("input-button-group");

// Create and display find location button
let $findButton = this.$findButtonTemplate.content.cloneNode(true);
// Create find location button
const $geoInputButton =
this.$geoInputButtonTemplate.content.cloneNode(true);

// Wrap input within `input-button-group` container
wrapElement(this.$geoInput, $inputButtonGroup);
// Wrap input within `input-button-group` container
wrapElement(this.$geoInput, $inputButtonGroup);

// Add button to `input-button-group` container
$inputButtonGroup.append($findButton);
// Add find location button to `input-button-group` container
$inputButtonGroup.append($geoInputButton);

// Update `this.$geoInputButton`
this.$geoInputButton = this.querySelector(".geo-input__button");
}

// Add event to cloned button
$findButton = this.querySelector("button");
$findButton.addEventListener("click", () => this.find());
this.$geoInputButton.addEventListener("click", () => this.find());
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/frontend/components/geo-input/template.njk
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"i18n-failed": __("geoInput.failed")
}
},
classes: opts.classes,
classes: classes("geo-input", opts),
id: id,
name: opts.name,
value: opts.value,
Expand All @@ -19,9 +19,9 @@
optional: opts.optional,
errorMessage: opts.errorMessage
}) %}
<template id="find-button">
<template id="geo-input-button">
{{ button({
classes: "button--secondary",
classes: "geo-input__button button--secondary",
type: "button",
text: __("geoInput.getCurrentPosition")
}) | indent(4) }}
Expand Down

0 comments on commit 7168c47

Please sign in to comment.