Skip to content

Instantly share code, notes, and snippets.

@ten1seven
Created January 27, 2017 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ten1seven/8ee7df854eb144689a1f6f687d3a3672 to your computer and use it in GitHub Desktop.
Save ten1seven/8ee7df854eb144689a1f6f687d3a3672 to your computer and use it in GitHub Desktop.
Proposed changes to message preview script
import debounce from 'debounce'
export default class MessagePreview {
constructor(el) {
this.el = el
this.setVariables()
this.setUpListeners()
this.updatePreview()
}
setVariables() {
this.textarea = this.el.querySelector('textarea')
this.text = this.el.querySelector('[data-message-preview="custom-text"]')
this.buttons = document.querySelectorAll('[data-message-preview="continue"]')
this.buttonsList = Array.from(this.buttons)
}
setUpListeners() {
this.textarea.addEventListener('keyup', this.updatePreview.bind(this))
this.buttonsList.forEach((button) => {
button.addEventListener('click', this.continueForm.bind(this))
})
}
updatePreview() {
this.text.innerHTML = this.textarea.value.replace(/\n/g, '<br />')
if (this.textarea.value.length) {
this.buttonsList.forEach((button) => {
button.classList.remove('-inactive')
button.classList.add('-primary')
button.removeAttribute('aria-hidden')
})
} else {
this.buttonsList.forEach((button) => {
button.classList.add('-inactive')
button.classList.remove('-primary')
button.setAttribute('aria-hidden', 'true')
})
this.text.innerHTML = ''
}
}
continueForm(e) {
if (!this.textarea.value.length) {
e.preventDefault()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment