Skip to content

Instantly share code, notes, and snippets.

@romanbsd
Created January 23, 2024 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romanbsd/e8e083cd65be1835db1ff4a83b0d476b to your computer and use it in GitHub Desktop.
Save romanbsd/e8e083cd65be1835db1ff4a83b0d476b to your computer and use it in GitHub Desktop.
import { Controller } from '@hotwired/stimulus'
import { TempusDominus } from '@eonasdan/tempus-dominus'
function findParentBySelector(element, selector) {
while (element && element.parentElement) {
if (element.parentElement.matches(selector)) {
return element.parentElement;
}
element = element.parentElement;
}
return null; // Return null if no matching parent is found
}
// 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: findParentBySelector(this.element, '.modal'),
localization: {
hourCycle: 'h24',
format: 'yyyy-MM-dd HH:mm'
},
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()
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment