cannery/assets/js/maintain_attrs.js
shibao c0179f48bd
Some checks reported errors
continuous-integration/drone/push Build was killed
mix format
2022-03-04 23:17:53 -05:00

12 lines
500 B
JavaScript

// maintain user adjusted attributes, like textbox length on phoenix liveview
// update. https://github.com/phoenixframework/phoenix_live_view/issues/1011
export default {
attrs () {
const attrs = this.el.getAttribute('data-attrs')
if (attrs) { return attrs.split(', ') } else { return [] }
},
beforeUpdate () { this.prevAttrs = this.attrs().map(name => [name, this.el.getAttribute(name)]) },
updated () { this.prevAttrs.forEach(([name, val]) => this.el.setAttribute(name, val)) }
}