-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate character counter to a web component
migrate the data attribute approach to a web component. I does not need a listener and it should be a pretty straight forward way of migrating small peaces of Javascript.
- Loading branch information
1 parent
2610aff
commit 1f9b6f6
Showing
4 changed files
with
40 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,37 @@ | ||
export default function CharCounter(field) { | ||
const $field = $(field) | ||
const $display = $('<small class="alchemy-char-counter"/>') | ||
$field.after($display) | ||
/** | ||
* Show the character counter below input fields and textareas | ||
*/ | ||
class CharCounter extends HTMLElement { | ||
constructor() { | ||
super() | ||
|
||
const maxChar = $field.data("alchemy-char-counter") | ||
const translation = Alchemy.t("allowed_chars", maxChar) | ||
this.maxChar = this.dataset.count | ||
this.translation = Alchemy.t("allowed_chars", this.maxChar) | ||
this.formField = this.getFormField() | ||
|
||
function countChars() { | ||
const charLength = $field.val().length | ||
if (this.formField) { | ||
this.createDisplayElement() | ||
this.countCharacters() | ||
this.formField.addEventListener("keyup", () => this.countCharacters()) // add arrow function to get a implicit this - binding | ||
} | ||
} | ||
|
||
$display.removeClass("too-long") | ||
$display.text(`${charLength} ${translation}`) | ||
getFormField() { | ||
const formFields = this.querySelectorAll("input, textarea") | ||
return formFields.length > 0 ? formFields[0] : undefined | ||
} | ||
|
||
if (charLength > maxChar) { | ||
$display.addClass("too-long") | ||
} | ||
createDisplayElement() { | ||
this.display = document.createElement("small") | ||
this.display.className = "alchemy-char-counter" | ||
this.formField.after(this.display) | ||
} | ||
|
||
countChars() | ||
$field.keyup(countChars) | ||
countCharacters() { | ||
const charLength = this.formField.value.length | ||
this.display.textContent = `${charLength} ${this.translation}` | ||
this.display.classList.toggle("too-long", charLength > this.maxChar) | ||
} | ||
} | ||
|
||
customElements.define("alchemy-char-counter", CharCounter) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters