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 b534570
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/assets/javascripts/folio/input/character_counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ window.Folio.Stimulus.register('f-input-character-counter', class extends window
if (this.maxValue) {
wrap.classList.toggle('text-danger', length > this.maxValue)
}
console.log(current)

current.innerText = length
}

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 +86,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 +101,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 b534570

Please sign in to comment.