Fix datetime picker after Livewire updates and per-instance options (#38)
Closes #38 (closed)
Root cause
bbs-input.datetime initialised itself with x-init="datetimepicker = initDatetimePicker($refs)". That function, the flatpickr script and the stylesheet were only output through @push('body') / @push('head').
- Livewire updates: Update responses only contain the component HTML and never the pushed stacks. A picker that first appears through an update (a conditionally shown field, a modal that renders on selection) on a page that had no picker on the initial render therefore threw
initDatetimePicker is not defined. Flatpickr itself was missing as well. - Shared options: The
<script>defininginitDatetimePicker()was not inside@once. Every picker redefined the global function with its own options baked in, so all pickers ended up with the options of the last picker rendered. A picker added by an update also used the options of the page's last initial picker.
Fix
- The initialisation now lives in the component's own
x-data(init()/createPicker()/destroy()), and each picker carries its own options via@js($options). There is no global function anymore. - If flatpickr or its stylesheet isn't on the page yet (the update case), the picker loads them itself from the same unpkg URLs as before (
flatpickr@4.6.13, airbnb theme iftheme="airbnb") and adds the colour style. Parallel pickers share one loading promise (window.bbsFlatpickrLoading). WithloadFromCdn="false"the component loads no script, as before: it uses the page'swindow.flatpickr, waiting forwindow.loadif needed, and logs a clear error if flatpickr is missing. - Full page loads behave as before. Stylesheet, colour style and script still come through the
head/bodystacks, now all inside@once, so the picker initialises right away without a loader round trip. - Removed pickers destroy their flatpickr instance (Alpine
destroy()), so re-rendered fields no longer leave orphaned.flatpickr-calendarelements in<body>.setDate/closecalls are guarded while the picker is still loading. - No API change: props, attributes,
wire:model/@entangle(including.live) and the value format are unchanged.
Why not @assets or a JS file in the consumer's bundle
- An Alpine component in the consumer's Vite bundle (as in annika-fuhrmann-website !14 (merged)) works, but every consumer would have to change their bundle.
@assetswould technically work inside the anonymous component. In Livewire 4.4.7,ExtendedCompilerEnginebinds$thisto the rendering Livewire component for nested Blade views as well, and the update payload's assets are awaited before morphing. I checked this by reading the code, not in a browser test. I didn't use it because the per-instance options have to live inx-dataanyway. The self-contained loader also doesn't depend on Livewire's asset-key/memo internals, and it covers HTML that neither stacks nor@assetsreach.
Consumer upgrade
- Bump to the next version (suggested 1.1.1: bug fix, backwards compatible). Nothing else is needed for projects that use the package view. The layout still needs
@stack('head')and@stack('body'), as before. - Published overrides (
resources/views/vendor/blade-components/components/input/datetime.blade.php, 11 projects): the published view replaces the package view, so those projects keep the broken behaviour until the override is dealt with.- If the override is unchanged from an old package version, delete it.
- If it contains project-specific changes, copy the new package view and re-apply the changes. The
x-datablock and the@pushblocks at the end must come from the new version. - Nothing may call the global
initDatetimePicker()anymore, because it no longer exists.
loadFromCdn="false": the page must providewindow.flatpickritself, as before.
The CHANGELOG entry (1.1.1) and the README component section describe the same steps. No tag or release was created.
Verification
Harness (new, export-ignored): testbench.yaml plus workbench/ (a Livewire component datetime-demo on /datetime with an @extends layout and head/body stacks). Query flags: initial (a picker on the initial render), fields (show the conditional fields initially), own-flatpickr (loadFromCdn=false, the page loads flatpickr). The conditional fields are two pickers with different options (Y-m-d with wire:model.live; d.m.Y H:i with enableTime, minuteIncrement=15).
Playwright (npm run test:browser → tests/Browser/datetime-picker.mjs, starts testbench serve itself, Chromium headless). Four checks, all passing on Livewire 4.4.7 (package dev setup) and on Livewire 3.8.10 (a scratch copy with composer require livewire/livewire:^3.6 -W):
- A page without a picker, then a Livewire update renders two pickers. Both initialise with their own
dateFormat/enableTime/minuteIncrement, show the server value (2026-10-01), and the stylesheet applies. Script, stylesheet and colour style are each added exactly once. Clicking a day updates the Livewire property (2026-10-15). Hiding the fields destroys the calendars (0 left), and showing them again re-initialises them without loading the script again. No page errors. - One picker on the initial render, two more added by an update: all three keep their own
dateFormat. Script and stylesheet are only those from the stacks. Picking a day in thed.m.Y H:ipicker updates the property. - Three pickers on the initial render keep their own options.
loadFromCdn=falsewith page-provided flatpickr: the update pickers initialise, no second script is loaded, and the stylesheet is still added.
Old component as a control: the same script against the main version of datetime.blade.php fails all 4 checks on both Livewire versions (initDatetimePicker is not defined, plus 'd.m.Y' !== 'Y-m-d' from the shared-options bug).
PHPUnit: new tests/DatetimeComponentTest.php (4 tests). It covers the update HTML via Livewire::test(): own options per picker, loader present, no initDatetimePicker. It also covers the full page via the workbench route (per-picker options, link/style/script output once), loadFromCdn=false, and the airbnb theme and colour. Full suite: vendor/bin/testbench package:test --parallel, 26 tests, 89 assertions, green on b7d06f77. The package has no artisan, so php artisan test doesn't apply. The one PHPUnit deprecation is the existing phpunit.xml schema warning.
Prepared by BBS Copilot