Skip to content

Instantly share code, notes, and snippets.

@thiagomajesk
Created April 11, 2022 03:49
Show Gist options
  • Save thiagomajesk/f3c77e6dfbbc27ed7ba9613d41271222 to your computer and use it in GitHub Desktop.
Save thiagomajesk/f3c77e6dfbbc27ed7ba9613d41271222 to your computer and use it in GitHub Desktop.
LiveView Tips and Tricks
/*
* Resets a input to a value when a form is updated by LiveView.
* Add phx-hook="FormReset" to the form and phx-reset="" to the input you want to reset.
* The attribute phx-reset must contain the value that the input value will be reseted to.
*/
Hooks.FormReset = {
updated() {
let input = this.el.querySelector('[phx-reset]:not(.invalid-feedback)')
let value = input.getAttribute('phx-reset')
input.value = value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment