Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions js/src/datepicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,14 @@ class Datepicker extends BaseComponent {
protected declare _themeAncestor: Element | null | undefined
protected declare _onOutside: (event: Event) => void
protected declare _onTriggerClick: () => void
protected declare _hideTimeout: ReturnType<typeof setTimeout> | null

constructor(element?: string | Element | null, config?: Partial<DatepickerConfig> | null) {
super(element, config)

this._calendar = null
this._isShown = false
this._hideTimeout = null

this._initCalendar()
}
Expand Down Expand Up @@ -172,11 +174,12 @@ class Datepicker extends BaseComponent {
}

async hide(): Promise<void> {
if (this._config.inline) {
return // Inline calendars are always visible
if (this._hideTimeout) {
clearTimeout(this._hideTimeout)
this._hideTimeout = null
}

if (!this._calendar || !this._isShown) {
if (!this._calendar || !this._config || !this._isShown || this._config.inline) {
return
}

Expand All @@ -192,6 +195,11 @@ class Datepicker extends BaseComponent {
}

override dispose(): void {
if (this._hideTimeout) {
clearTimeout(this._hideTimeout)
this._hideTimeout = null
}

if (this._themeObserver) {
this._themeObserver.disconnect()
this._themeObserver = null
Expand Down Expand Up @@ -489,7 +497,14 @@ class Datepicker extends BaseComponent {
(this._config.selectionMode === 'multiple-ranged' && selectedDates.length >= 2)

if (shouldHide) {
setTimeout(() => this.hide(), HIDE_DELAY)
if (this._hideTimeout) {
clearTimeout(this._hideTimeout)
}

this._hideTimeout = setTimeout(() => {
this._hideTimeout = null
this.hide()
}, HIDE_DELAY)
}
}

Expand Down
17 changes: 17 additions & 0 deletions js/tests/unit/datepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,23 @@ describe('Datepicker', () => {

expect(Datepicker.getInstance(inputEl)).toBeNull()
})

it('should clear pending hide timeout on dispose without throwing', async () => {
fixtureEl.innerHTML = '<input type="text" data-bs-toggle="datepicker">'

const inputEl = fixtureEl.querySelector('input')
const datepicker = new Datepicker(inputEl)

datepicker._maybeHideAfterSelection(['2026-10-31'])
expect(datepicker._hideTimeout).not.toBeNull()

datepicker.dispose()
expect(datepicker._hideTimeout).toBeNull()

await new Promise(resolve => {
setTimeout(resolve, 350)
})
})
})

describe('options', () => {
Expand Down