themesberg / flowbite

Open-source UI component library and front-end development framework based on Tailwind CSS
https://flowbite.com
MIT License
7.74k stars 727 forks source link

Flowbite Datepicker Issue with Livewire #508

Closed krutsednar closed 1 year ago

krutsednar commented 1 year ago

Livewire Component not mounting value of datepicker and is NULL upon selecting date

                <div class="relative max-w-sm">
                    <label class="form-label mb-2 text-sm font-medium text-gray-900 dark:text-white" for="date_filed">{{ trans('cruds.leaveApplication.fields.date_filed') }}</label>
                    <div class="absolute py-2.5 left-0 flex items-center pl-3 pointer-events-none">
                      <svg aria-hidden="true" class="w-5 h-5 text-gray-500 dark:text-gray-400" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M6 2a1 1 0 00-1 1v1H4a2 2 0 00-2 2v10a2 2 0 002 2h12a2 2 0 002-2V6a2 2 0 00-2-2h-1V3a1 1 0 10-2 0v1H7V3a1 1 0 00-1-1zm0 5a1 1 0 000 2h8a1 1 0 100-2H6z" clip-rule="evenodd"></path></svg>
                    </div>
                    <input datepicker datepicker-autohide type="text" wire:model="leaveApplication.date_filed" id="date_filed" name="date_filed" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full pl-10 p-2.5  dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="Select date">
                    <div class="validation-message">
                        {{ $errors->first('leaveApplication.date_filed') }}
                    </div>
                    <div class="help-block">
                        {{ trans('cruds.leaveApplication.fields.date_filed_helper') }}
                    </div>
                </div>
zoltanszogyenyi commented 1 year ago

Hey @krutsednar,

Please check out the new datepicker guide with LiveWire on the Flowbite Docs.

Cheers, Zoltan

djelusive commented 1 year ago

Hey @zoltanszogyenyi the link you've provided is for Pheonix. Not for Livewire. Pheonix uses a live hook situation. Where Livewire is for Laravel. Am I missing something?

thiagogalvao commented 10 months ago

Does anyone has a workaround for this issue?

thiagogalvao commented 10 months ago

For anyone to land here this javascript solved my issue.

Replace the "date_start" element id by your own input element id value. Replace the "start" with your component attribute name.

<script> window.addEventListener("load", function() { elStart = document.getElementById("date_start"); elStart.addEventListener("blur", (event) => { @this.set('start', event.target.value); }); }); </script>

blunde commented 10 months ago

For anyone to land here this javascript solved my issue.

Replace the "date_start" element id by your own input element id value. Replace the "start" with your component attribute name.

<script> window.addEventListener("load", function() { elStart = document.getElementById("date_start"); elStart.addEventListener("blur", (event) => { @this.set('start', event.target.value); }); }); </script>

I have this problem. This script helps, but doesn't change when date is selected. Only when the input field is not active.

thiagogalvao commented 10 months ago

yes, you are right. It should work when the input loses its focus. For other use cases, it may be possible to use another event listener like "change" or "click".

stuckfakur commented 10 months ago

<script> const elEnd = document.getElementById("date_filed"); elEnd.addEventListener("click", (event) => { @this.set('leaveApplication.date_filed', event.target.value); }); </script>

how about this?

DiegoMagdaleno commented 9 months ago

Found a better solution! For all Livewire users, here is how to get this working with a model you define in the Livewire side.

First of all, Livewire comes with Alpine.JS, a Powerful and modern JS library.

Per the docs, we are able to listen to custom events, when reading the code of the Datepicker, I found it emits a changeDate, this event however, has camelCasing, per HTML reference, attributes cannot have camelCase names. Thanfully, Alpine provides us with a method, .camel in order to read events with a camel case, so now, in our input method we can do:

<input datepicker datepicker-autohide type="text" 
wire:model="leaveApplication.date_filed"
id="date_filed" 
name="date_filed" 
class="..," 
placeholder="Select date" 
@change-date.camel="@this.set('leaveApplication.date_filed'. $event.target.value)">

Hope this is helpful :)

Design-M commented 3 months ago

Found a better solution! For all Livewire users, here is how to get this working with a model you define in the Livewire side.

First of all, Livewire comes with Alpine.JS, a Powerful and modern JS library.

Per the docs, we are able to listen to custom events, when reading the code of the Datepicker, I found it emits a changeDate, this event however, has camelCasing, per HTML reference, attributes cannot have camelCase names. Thanfully, Alpine provides us with a method, .camel in order to read events with a camel case, so now, in our input method we can do:

<input datepicker datepicker-autohide type="text" 
wire:model="leaveApplication.date_filed"
id="date_filed" 
name="date_filed" 
class="..," 
placeholder="Select date" 
@change-date.camel="@this.set('leaveApplication.date_filed'. $event.target.value)">

Hope this is helpful :)

only one correction @change-date.camel="@this.set('leaveApplication.date_filed', $event.target.value)"> Coma after 'leaveApplication.date_filed' - work with livewire 3.

anglin-rogersoft commented 2 months ago

Found a better solution! For all Livewire users, here is how to get this working with a model you define in the Livewire side. First of all, Livewire comes with Alpine.JS, a Powerful and modern JS library. Per the docs, we are able to listen to custom events, when reading the code of the Datepicker, I found it emits a changeDate, this event however, has camelCasing, per HTML reference, attributes cannot have camelCase names. Thanfully, Alpine provides us with a method, .camel in order to read events with a camel case, so now, in our input method we can do:

<input datepicker datepicker-autohide type="text" 
wire:model="leaveApplication.date_filed"
id="date_filed" 
name="date_filed" 
class="..," 
placeholder="Select date" 
@change-date.camel="@this.set('leaveApplication.date_filed'. $event.target.value)">

Hope this is helpful :)

only one correction @change-date.camel="@this.set('leaveApplication.date_filed', $event.target.value)"> Coma after 'leaveApplication.date_filed' - work with livewire 3.

@change-date.camel="$wire.leaveApplication.date_filed = $event.target.value"

This one is worked for me.