Sha256: bf265fd99255ab18f7e5ec4aab4276e16f196b4a6262a1c666b9be8c88f5efb9

Contents?: true

Size: 1.58 KB

Versions: 43

Compression:

Stored size: 1.58 KB

Contents

import { Controller } from 'stimulus'
import { DateTime } from 'luxon'
import { castBoolean } from '@/js/helpers/cast_boolean'
import flatpickr from 'flatpickr'

export default class extends Controller {
  static targets = ['input']

  connect() {
    const options = {
      enableTime: false,
      enableSeconds: false,
      // eslint-disable-next-line camelcase
      time_24hr: false,
      locale: {
        firstDayOfWeek: 0,
      },
      altInput: true,
    }
    const enableTime = castBoolean(this.inputTarget.dataset.enableTime)

    // Set the format of the displayed input field.
    options.altFormat = this.inputTarget.dataset.pickerFormat

    // Set first day of the week.
    options.locale.firstDayOfWeek = this.inputTarget.dataset.firstDayOfWeek

    // Enable time if needed.
    options.enableTime = enableTime
    options.enableSeconds = enableTime

    let currentValue

    // enable timezone display
    if (enableTime) {
      currentValue = DateTime.fromISO(this.inputTarget.value, { zone: window.timezone })
      currentValue = currentValue.setZone(this.inputTarget.dataset.timezone)
      currentValue = currentValue.toISO()

      options.dateFormat = 'Y-m-d H:i:S'
      // eslint-disable-next-line camelcase
      options.time_24hr = castBoolean(this.inputTarget.dataset.time24hr)
      // this.timezone = Intl.DateTimeFormat().resolvedOptions().timeZone
      options.appTimezone = this.inputTarget.dataset.timezone
    } else {
      currentValue = new Date(this.inputTarget.value)
    }

    options.defaultDate = currentValue

    flatpickr(this.inputTarget, options)
  }
}

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
avo-1.3.5 app/packs/js/controllers/fields/date_field_controller.js
avo-1.3.4 app/packs/js/controllers/fields/date_field_controller.js
avo-1.3.3 app/packs/js/controllers/fields/date_field_controller.js
avo-1.3.2 app/packs/js/controllers/fields/date_field_controller.js
avo-1.3.1 app/packs/js/controllers/fields/date_field_controller.js
avo-1.3.0 app/packs/js/controllers/fields/date_field_controller.js
avo-1.2.11.pre.4 app/packs/js/controllers/fields/date_field_controller.js
avo-1.2.11.pre.3 app/packs/js/controllers/fields/date_field_controller.js
avo-1.2.11.pre.2 app/packs/js/controllers/fields/date_field_controller.js
avo-1.2.11.pre.1 app/packs/js/controllers/fields/date_field_controller.js
avo-1.2.10 app/packs/js/controllers/fields/date_field_controller.js
avo-1.2.9 app/packs/js/controllers/fields/date_field_controller.js
avo-1.2.8 app/packs/js/controllers/fields/date_field_controller.js
avo-1.2.7 app/packs/js/controllers/fields/date_field_controller.js
avo-1.2.6 app/packs/js/controllers/fields/date_field_controller.js
avo-1.2.6.pre.1 app/packs/js/controllers/fields/date_field_controller.js
avo-1.2.5 app/packs/js/controllers/fields/date_field_controller.js
avo-1.2.4 app/packs/js/controllers/fields/date_field_controller.js
avo-1.2.3 app/packs/js/controllers/fields/date_field_controller.js
avo-1.3.0.pre.1 app/packs/js/controllers/fields/date_field_controller.js