A Stimulus controller that counts the number of characters in any input fields.
$ yarn add stimulus-character-counter
And use it in your JS file:
import { Application } from "stimulus"
import CharacterCounter from "stimulus-character-counter"
const application = Application.start()
application.register("character-counter", CharacterCounter)
In your view:
<div data-controller="character-counter">
<textarea data-target="character-counter.input"></textarea>
<p>
There are <strong data-target="character-counter.counter"></strong> characters in this textarea.
</p>
</div>
You can use inheritance to extend the functionality of any Stimulus components.
import CharacterCounter from "stimulus-character-counter"
export default class extends CharacterCounter {
connect() {
super.connect()
console.log("Do what you want here.")
this.count() // Will return the number of characters in the input/texterea.
}
}
These controllers will automatically have access to targets defined in the parent class.
If you override the connect, disconnect or any other methods from the parent, you'll want to call super.method()
to make sure the parent functionality is executed.
$ yarn install
$ yarn dev
Prettier and ESLint are responsible to lint and format this component:
$ yarn lint
$ yarn format
Do not hesitate to contribute to the project by adapting or adding features ! Bug reports or pull requests are welcome.
This project is released under the MIT license.