P粉8074716042023-08-26 14:38:51
I did some debugging here and found that in the code of the date picker, we can use the "changeDate" event to connect it with Livewire. Not sure why this wasn't documented. The following is the code:
Component view:
<div> <input type="text" wire:model="test" placeholder="测试"> <input name="start" id="startInput" inline-datepicker="" wire:model="start" datepicker-format="dd.mm.yyyy" type="text" placeholder="{{$start}}" value="{{$start}}" datepicker-autohide> <br> <br> 当前属性: <hr> <div> {{$test}} <br> {{$start}} </div> </div>
Components:
namespace App\Http\Livewire; use Livewire\Component; class SomeComponent extends Component { public $test; public $start; protected $listeners = ['changeDate' => 'changeDate']; public function changeDate($date) { $this->start = $date; } public function mount() { $this->start = now()->format('d.m.Y'); } public function render() { return view('livewire.some-component'); } }
And the HTML code that contains the Livewire component, and the js code that listens to the Flowbite date picker event and triggers the Livewire event after that.
<body> <div> <br> <br> @livewire('some-component') </div> <script> document.getElementById("startInput").addEventListener("changeDate", function (e) { Livewire.emit('changeDate', e.detail.datepicker.inputField.value) }); </script> @livewireScripts </body>
Works as expected in my environment. cheers