Enhance hashcash_hidden_field_tag#12
Conversation
…d generate a unique ID
|
Indeed, it sounds useful to add options. If you change the id, the JS won't detect the input since it does |
|
@alexisbernard This suggestion has come out after using it in different applications. On the other hand, using a unique id allows us to have multiple forms without having any collisions. In order not to break previous implementations I have modified the selector. Currently we use a fairly simple stimulus controller, but it's still not a bad idea to separate the javascript and create an npm library with the javascript part. I think that would have a lot of benefits.
import { Controller } from '@hotwired/stimulus'
export default class extends Controller {
connect () {
const initializeHashcash = () => {
if (typeof Hashcash !== 'undefined') {
this.produceHashcash()
clearInterval(this.hashcashInterval)
}
}
this.hashcashInterval = setInterval(initializeHashcash, 100)
}
produceHashcash () {
this.hashcashInstance = new Hashcash(this.element)
}
disconnect () {
clearInterval(this.hashcashInterval)
}
}But we can think of putting all the logic into the controller, here is a very basic example. Let me know what you think 🙂 |
This pull request enhances the
hashcash_hidden_field_tahelper adding:Unique ID Assignment: Automatically generates a unique ID for the hidden field using SecureRandom.hex(4). This ID can be useful for DOM interactions and debugging, ensuring that each
hashcash_hidden_field_taghas a unique identifier.Flexible HTML Attributes: By accepting additional HTML attributes
**html_attributes, allows greater customization of the hidden field, making it more versatile.Default Stimulus Controller
data-controller: Adds a defaultdata-controller="hashcash"attribute, seamlessly integrating this helper with Stimulus. This makes it immediately available for client-side interactions managed by ahashcashStimulus controller, reducing the need to manually add the controller each time the helper is used.Default and Overridable Options:
id,data-hashcashanddata-controllercan be overridden if specific values are needed, offering a blend of helpful defaults and customization options.These changes should be backward-compatible with existing implementations of
hashcash_hidden_field_tag.