Created
June 22, 2023 17:26
-
-
Save aseroff/e69020e283db86d86e03946e628c3482 to your computer and use it in GitHub Desktop.
text with character limit html & stimulus controller
This file contains hidden or 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
%div{data: {controller: 'text-with-limit'}} | |
- limit = 1000 | |
%label Message: | |
= form.text_area :custom_message, value: default_message, maxlength: limit, rows: 9, data: { 'text-with-limit-target': 'field', action: 'keyup->text-with-limit#updateCounter' } | |
.right | |
%small | |
Chars left: | |
%span{data: {'text-with-limit-target': 'counter'}}= limit - default_message.length | |
\/ | |
= limit |
This file contains hidden or 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
import { Controller } from '@hotwired/stimulus' | |
export default class extends Controller { | |
static targets = [ 'field', 'counter' ] | |
updateCounter() { | |
this.counterTarget.textContent = this.fieldTarget.maxLength - this.fieldTarget.value.length | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment