Sha256: 1fffbc8280aea69b5cf8e99ad2b40b3fa5ec8ddd1ce03a7033d2e967e8e22cca

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 KB

Contents

import ApplicationController from './application_controller'

import flatpickr from 'flatpickr'

export default class extends ApplicationController {
  connect () {
    this.flatpickr = flatpickr(this.element, {
      ...this.options,
      onReady: this.setup.bind(this)
    })
  }

  disconnect () {
    this.flatpickr.destroy()
  }

  clear () {
    const isDisabled = this.element.disabled || this.element.classList.contains('disabled')

    if (!isDisabled) {
      this.flatpickr.clear()
    }
  }

  setup (selectedDates, dateStr, instance) {
    this._createClearButton(instance)
  }

  get options () {
    return {
      allowInput: true,
      altInput: true
    }
  }

  _createClearButton (instance) {
    if (this.element.dataset.allowClear) {
      const clearButton = document.createElement('button')

      clearButton.classList.add('clear-datepicker')

      clearButton.addEventListener('click', (e) => {
        e.preventDefault()
        this.clear()
      })

      instance.altInput.parentNode.insertBefore(clearButton, instance.altInput)
    }
  }
}

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
trestle-0.10.0 frontend/js/controllers/flatpickr_controller.js
trestle-0.10.0.pre2 frontend/js/controllers/flatpickr_controller.js
trestle-0.10.0.pre frontend/js/controllers/flatpickr_controller.js