From: Mark Otto Date: Mon, 13 Jul 2026 04:01:17 +0000 (-0700) Subject: Datepicker: populate input with preselected dates on load (#42628) X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=45d5f60277230e21bf33bad455f549a22f3aa79e;p=thirdparty%2Fbootstrap.git Datepicker: populate input with preselected dates on load (#42628) Fixes #42616 Input and button datepickers are only created on focus/click, so data-bs-selected-dates never showed up until you interacted with the field. Only inline datepickers were auto-initialized on load. Extend the DOMContentLoaded auto-init to also cover any datepicker with preselected dates so the value renders immediately. --- diff --git a/js/src/datepicker.js b/js/src/datepicker.js index 11085b5c3e..e2f668c421 100644 --- a/js/src/datepicker.js +++ b/js/src/datepicker.js @@ -472,9 +472,12 @@ EventHandler.on(document, EVENT_FOCUSIN_DATA_API, SELECTOR_DATA_TOGGLE, function Datepicker.getOrCreateInstance(this).show() }) -// Auto-initialize inline datepickers on DOMContentLoaded +// Auto-initialize datepickers that need to render on load: +// - inline datepickers (always visible) +// - datepickers with preselected dates (so the value populates without focus) EventHandler.on(document, `DOMContentLoaded${EVENT_KEY}${DATA_API_KEY}`, () => { - for (const element of document.querySelectorAll(`${SELECTOR_DATA_TOGGLE}[data-bs-inline="true"]`)) { + const selector = `${SELECTOR_DATA_TOGGLE}[data-bs-inline="true"], ${SELECTOR_DATA_TOGGLE}[data-bs-selected-dates]` + for (const element of document.querySelectorAll(selector)) { Datepicker.getOrCreateInstance(element) } }) diff --git a/js/tests/unit/datepicker.spec.js b/js/tests/unit/datepicker.spec.js index f8efc45fc1..bc8458d1a8 100644 --- a/js/tests/unit/datepicker.spec.js +++ b/js/tests/unit/datepicker.spec.js @@ -121,6 +121,16 @@ describe('Datepicker', () => { expect(datepicker._config.selectionMode).toEqual('multiple') expect(datepicker._config.firstWeekday).toEqual(0) }) + + it('should populate the input value with preselected dates', () => { + fixtureEl.innerHTML = '' + + const inputEl = fixtureEl.querySelector('input') + const datepicker = new Datepicker(inputEl) + + expect(datepicker._config.selectedDates).toEqual(['2026-06-10', '2026-06-18']) + expect(inputEl.value).not.toEqual('') + }) }) describe('show', () => { @@ -842,6 +852,25 @@ describe('Datepicker', () => { expect(toggleSpy).not.toHaveBeenCalled() }) + + it('should auto-initialize datepickers with preselected dates on DOMContentLoaded', () => { + return new Promise(resolve => { + fixtureEl.innerHTML = '' + + const inputEl = fixtureEl.querySelector('input') + + expect(Datepicker.getInstance(inputEl)).toBeNull() + + const domContentLoadedEvent = createEvent('DOMContentLoaded') + document.dispatchEvent(domContentLoadedEvent) + + setTimeout(() => { + expect(Datepicker.getInstance(inputEl)).not.toBeNull() + expect(inputEl.value).not.toEqual('') + resolve() + }) + }) + }) }) describe('getInstance', () => {