diff --git a/core/js/src/datepicker.ts b/core/js/src/datepicker.ts index f25b040b3..750e8ee55 100644 --- a/core/js/src/datepicker.ts +++ b/core/js/src/datepicker.ts @@ -43,6 +43,8 @@ const DATE_PATTERN = /^(\d{4})-(\d{2})-(\d{2})$/ // Vanilla Calendar Pro 3.4+ throws on an option whose extension is not registered const PLUGIN_EXTENSIONS = ['annotations', 'months', 'motion', 'time', 'weeks'] as const +type ExtensionOptions = { extensions?: object[] } + // The public API is typed with local copies of the Vanilla Calendar Pro types. // `vanilla-calendar-pro` is an optional peer dependency, so the published // `dist/types` must not import it: a project that never loads the datepicker @@ -559,12 +561,12 @@ class Datepicker extends BaseComponent { } /** @internal */ - _buildCalendarOptions(): Options { + _buildCalendarOptions(): Options & ExtensionOptions { // The plugin uses 'system' for auto-detection, Bootstrap and Tabler use 'auto' const theme = this._getEffectiveTheme() const vcpTheme = !theme || theme === 'auto' ? 'system' : theme - const calendarOptions: Options = { + const calendarOptions: Options & ExtensionOptions = { ...this._config.vcpOptions, inputMode: !this._isInline, positionToInput: this._config.placement, @@ -610,11 +612,12 @@ class Datepicker extends BaseComponent { } /** @internal */ - _getPluginExtensions(): NonNullable { - const extensions = [...((this._config.vcpOptions as Options).extensions ?? [])] + _getPluginExtensions(): object[] { + const extensions = [...((this._config.vcpOptions as ExtensionOptions).extensions ?? [])] + const plugin = window.VanillaCalendarPro as Record | undefined for (const name of PLUGIN_EXTENSIONS) { - const extension = window.VanillaCalendarPro?.[name] + const extension = plugin?.[name] if (extension && !extensions.includes(extension)) { extensions.push(extension) } diff --git a/core/js/tests/unit/datepicker.spec.ts b/core/js/tests/unit/datepicker.spec.ts index bf0ad5414..2544aa22c 100644 --- a/core/js/tests/unit/datepicker.spec.ts +++ b/core/js/tests/unit/datepicker.spec.ts @@ -154,13 +154,24 @@ describe('Datepicker', () => { expect(fixtureEl.querySelector('[data-vc="time"]')).not.toBeNull() }) - it('should keep extensions passed through vcpOptions', () => { + it('should register the plugin extensions and keep those passed through vcpOptions', () => { + const months = { name: 'months' } + const custom = { name: 'custom' } + window.VanillaCalendarPro = { ...VanillaCalendarPro, months } as unknown as typeof VanillaCalendarPro fixtureEl.innerHTML = '' - const instance = new Datepicker(input(), { vcpOptions: { extensions: [VanillaCalendarPro.time] } }) + const instance = new Datepicker(input(), { vcpOptions: { extensions: [custom, months] } }) - expect(instance.calendar!.extensions).toContain(VanillaCalendarPro.time) - expect(instance.calendar!.extensions).toContain(VanillaCalendarPro.months) + expect(instance._buildCalendarOptions().extensions).toEqual([custom, months]) + }) + + it('should not pass extensions when the plugin has none', () => { + window.VanillaCalendarPro = { Calendar: VanillaCalendarPro.Calendar } as unknown as typeof VanillaCalendarPro + fixtureEl.innerHTML = '' + + const instance = new Datepicker(input()) + + expect(instance._buildCalendarOptions()).not.toHaveProperty('extensions') }) }) diff --git a/core/package.json b/core/package.json index e1a5a4d75..8add9cc8e 100644 --- a/core/package.json +++ b/core/package.json @@ -287,7 +287,7 @@ "tom-select": "^2.6.2", "typed.js": "^3.0.0", "typescript": "^7.0.2", - "vanilla-calendar-pro": "^3.4.0", + "vanilla-calendar-pro": "^3.3.2", "vitest": "4.1.11" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0d91c2786..f5d87b3c0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -214,8 +214,8 @@ importers: specifier: ^7.0.2 version: 7.0.2 vanilla-calendar-pro: - specifier: ^3.4.0 - version: 3.4.0 + specifier: ^3.3.2 + version: 3.3.2 vitest: specifier: 4.1.11 version: 4.1.11(@types/node@26.4.0)(@vitest/browser-playwright@4.1.11)(@vitest/coverage-istanbul@4.1.11)(esbuild@0.28.1)(sass@1.103.1)(terser@5.51.2)(tsx@4.23.13)(yaml@2.9.0) @@ -4601,8 +4601,8 @@ packages: validate-npm-package-license@3.0.4: resolution: {integrity: sha512-DpKm2Ui/xN7/HQKCtpZxoRWBhZ9Z0kqtygG8XCgNQ8ZlDnxuQmWhj566j8fN4Cu3/JmbhsDo7fcAJq4s9h27Ew==} - vanilla-calendar-pro@3.4.0: - resolution: {integrity: sha512-HLWjzHOWvMIrjI77tXK5bYzp0s5TeBSrZC247STjcTAdWI7kqEwWT6Xvu20vqdwzF498H1Da8l4WpdgVIGtc1Q==} + vanilla-calendar-pro@3.3.2: + resolution: {integrity: sha512-RaLpDtvQFeKi0ajOpGBedLNrr0YoiYAF+0sppk4XZV/U57AuihJIhw8h2whUvIG1ZSR3LrdSXk3lHLkCPGlHvg==} vfile-location@5.0.3: resolution: {integrity: sha512-5yXvWDEgqeiYiBe1lbxYF7UMAIm/IcopxMHrMQDq3nvKcjPKIhZklUKL+AE7J7uApI4kwe2snsK+eI6UTj9EHg==} @@ -9590,7 +9590,7 @@ snapshots: spdx-correct: 3.2.0 spdx-expression-parse: 3.0.1 - vanilla-calendar-pro@3.4.0: {} + vanilla-calendar-pro@3.3.2: {} vfile-location@5.0.3: dependencies: