Created
December 21, 2021 13:04
-
-
Save jonneroelofs/b4be067329cb12777c4fcf2658013bc2 to your computer and use it in GitHub Desktop.
Flatpickr wrapped with Alpine.js in blade component for Laravel Livewire
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div x-data="datepicker(@entangle($attributes->wire('model')))" class="relative"> | |
<div class="flex flex-col"> | |
<label>Date</label> | |
<div class="flex items-center gap-2"> | |
<input type="text" x-ref="myDatepicker" x-model="value"> | |
<span class="cursor-pointer underline" x-on:click="reset"> | |
<x-icon.x></x-icon.x> | |
</span> | |
</div> | |
</div> | |
</div> | |
@once | |
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css"> | |
<script src="https://cdn.jsdelivr.net/npm/flatpickr"></script> | |
<script> | |
document.addEventListener('alpine:init', () => { | |
Alpine.data('datepicker', (model) => ({ | |
value: model, | |
init(){ | |
this.pickr = flatpickr(this.$refs.myDatepicker, {}) | |
this.$watch('value', function(newValue){ | |
this.pickr.setDate(newValue); | |
}.bind(this)); | |
}, | |
reset(){ | |
this.value = null; | |
} | |
})) | |
}) | |
</script> | |
@endonce |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="bg-dark"> | |
<div class="grid place-items-center min-h-screen"> | |
<div class="shadow rounded p-4 border bg-white h-[278px] flex flex-col gap-4"> | |
<h1 class="text-xl font-semibold text-gray-700">Event form</h1> | |
<div class="space-y-4"> | |
<x-datepicker label="Starts at" wire:model="event.starts_at"> | |
</x-datepicker> | |
<x-datepicker label="Ends at" wire:model="event.ends_at"> | |
</x-datepicker> | |
</div> | |
<div> | |
<button | |
class="bg-success-500 text-white px-2 py-1 rounded" | |
wire:click="startToday()" | |
> | |
Start Today | |
</button> | |
</div> | |
</div> | |
</div> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Livewire; | |
use App\Models\Event; | |
use Livewire\Component; | |
class EventForm extends Component | |
{ | |
public $event; | |
public function rules() | |
{ | |
return [ | |
'event.starts_at' => ['required'], | |
'event.ends_at' => ['required'], | |
]; | |
} | |
public function mount() | |
{ | |
$this->event = Event::make(); | |
} | |
public function startToday() | |
{ | |
$this->event->starts_at = today()->format('Y-m-d'); | |
$this->event->ends_at = today()->format('Y-m-d'); | |
} | |
public function render() | |
{ | |
return view('livewire.event-form'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Greetings, I'm sharing my version for month selection. It works with multiple inputs and Livewire navigation.
This particular case is for selecting months, although it can be adapted for full date selection.