Sha256: 1f4884cff8a19681ae081dca9815f8dba29ab24d6ad0b1cfba933f8a4ef915bf

Contents?: true

Size: 1.24 KB

Versions: 3

Compression:

Stored size: 1.24 KB

Contents

import { Controller } from "@hotwired/stimulus"
import flatpickr from "https://cdn.skypack.dev/flatpickr@4.6.13?min"

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 { ...this.#baseOptions, altFormat: this.dateTimeFormatValue, dateFormat: "Y-m-d H:i", enableTime: true }
  }

  get #basicOptions() {
    return { ...this.#baseOptions, altFormat: this.dateFormatValue, dateFormat: "Y-m-d" }
  }

  get #baseOptions() {
    return { altInput: true, disable: this.disableValue, mode: this.modeValue }
  }
}

Version data entries

3 entries across 3 versions & 1 rubygems

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