Skip to content

Commit

Permalink
feat(input): add input character counter change listener
Browse files Browse the repository at this point in the history
  • Loading branch information
VladaTrefil committed May 24, 2024
1 parent 5807e1d commit 5936ded
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/assets/javascripts/folio/input/character_counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ window.Folio.Stimulus.register('f-input-character-counter', class extends window

addElementToFormGroup () {
const formGroup = this.element.closest('.form-group')
const input = formGroup.querySelector('input, textarea')

if (!formGroup) {
throw new Error('Missing parent form-group element.')
Expand Down Expand Up @@ -84,6 +85,8 @@ window.Folio.Stimulus.register('f-input-character-counter', class extends window
}
}

this.bindChangeEventListener(input)

wrap.appendChild(document.createTextNode(` ${Folio.i18n(window.Folio.Input.CharacterCounter.i18n, 'shortForCharacter')}`))

this.element.insertAdjacentElement('afterend', wrap)
Expand All @@ -97,4 +100,10 @@ window.Folio.Stimulus.register('f-input-character-counter', class extends window
if (!wrap.parentNode) return
wrap.parentNode.removeChild(wrap)
}

bindChangeEventListener (input) {
if (input) {
input.addEventListener('change', this.onInput.bind(this))
}
}
})

0 comments on commit 5936ded

Please sign in to comment.