Skip to content

Instantly share code, notes, and snippets.

@jonneroelofs
Created December 21, 2021 13:04
Show Gist options
  • Save jonneroelofs/b4be067329cb12777c4fcf2658013bc2 to your computer and use it in GitHub Desktop.
Save jonneroelofs/b4be067329cb12777c4fcf2658013bc2 to your computer and use it in GitHub Desktop.
Flatpickr wrapped with Alpine.js in blade component for Laravel Livewire
<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
<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>
<?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');
}
}
@evansjavier
Copy link

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.


<div x-data="datepicker(@entangle($attributes->wire('model')))" class="relative" wire:ignore>
    <div class="flex flex-col">
        <div class="flex items-center gap-2">
            <input type="text" x-ref="myDatepicker" x-model="value" placeholder="Select month" class="form-control">
        </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 src="https://cdn.jsdelivr.net/npm/flatpickr/dist/plugins/monthSelect/index.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/plugins/monthSelect/style.css">
    <script>
        document.addEventListener('alpine:init', () => {
            Alpine.data('datepicker', (model) => ({
                value: model,
                init() {
                    this.pickr = flatpickr(this.$refs.myDatepicker, {
                        altInput: true,
                        altFormat: "F Y",
                        dateFormat: "Y-m",
                        plugins: [
                            new monthSelectPlugin({
                                shorthand: true,
                                dateFormat: "Y-m",
                                altFormat: "F Y"
                            })
                        ]
                    });

                    if (this.value) {
                        this.pickr.setDate(this.value, true, "Y-m");
                    }

                    this.$watch('value', function (newValue) {
                        this.pickr.setDate(newValue, true, "Y-m");
                    }.bind(this));
                },
                reset() {
                    this.value = null;
                }
            }))
        })
    </script>
@endonce

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment