Sha256: 2b69a83b834023a8da064a74c7cee1138f653daf06e34e17e010f5d904230a27

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

import { Controller } from "@hotwired/stimulus"
import flatpickr from "flatpickr"

export default class extends Controller {
  static targets = [ "details" ]
  static values  = {
    type: String,
    disable: Array,
    mode: { type: String, default: "single" },
    dateFormat: { type: String, default: "F d, Y" },
    dateTimeFormat: { type: String, default: "F d, Y H:i" }
  }

  connect() {
    if (this.typeValue == "time") {
      this.flatpickr = flatpickr(this.element, this.#timeOptions)
    } else if (this.typeValue == "datetime") {
      this.flatpickr = flatpickr(this.element, this.#dateTimeOptions)
    } else {
      this.flatpickr = flatpickr(this.element, this.#basicOptions)
    }
  }

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

  get #timeOptions() {
    return { dateFormat: "H:i", enableTime: true, noCalendar: true }
  }

  get #dateTimeOptions() {
    return {
      altInput: true,
      altFormat: this.dateTimeFormatValue,
      dateFormat: "Y-m-d H:i",
      disable: this.disableValue,
      mode: this.modeValue,
      enableTime: true
    }
  }

  get #basicOptions() {
    return {
      altInput: true,
      altFormat: this.dateFormatValue,
      dateFormat: "Y-m-d",
      disable: this.disableValue,
      mode: this.modeValue
    }
  }
}

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
css-zero-0.0.54 lib/generators/css_zero/add/templates/app/javascript/controllers/datepicker_controller.js