Skip to content

Instantly share code, notes, and snippets.

@romanbsd
Created June 16, 2024 08:29
Show Gist options
  • Save romanbsd/54c9d6a560a9a5150ff54a8866c00998 to your computer and use it in GitHub Desktop.
Save romanbsd/54c9d6a560a9a5150ff54a8866c00998 to your computer and use it in GitHub Desktop.
Timepicker controller for bootstrap
import { Controller } from '@hotwired/stimulus'
import { TempusDominus } from '@eonasdan/tempus-dominus'
// Connects to data-controller="datetime"
export default class extends Controller {
connect() {
this.element.classList.add('bg-white')
this.element.setAttribute('readonly', true)
this.widget = new TempusDominus(this.element, {
container: this.element.closest('.modal'),
localization: {
hourCycle: 'h24',
format: 'yyyy-MM-dd HH:mm:ss'
},
display: {
sideBySide: true,
components: {
seconds: true
},
icons: {
type: 'icons',
time: 'fas fa-clock',
date: 'fas fa-calendar',
up: 'fas fa-arrow-up',
down: 'fas fa-arrow-down',
previous: 'fas fa-chevron-left',
next: 'fas fa-chevron-right',
today: 'fas fa-calendar-check',
clear: 'fas fa-trash',
close: 'fas fa-times',
}
}
})
this.element.addEventListener('change.td', e => {
const date = e.detail.date
date.setMilliseconds(0)
this.element.value = date.toISOString()
})
}
disconnect() {
this.widget.dispose()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment