Hi all,
I have a Livewire component:
class AddTask extends Component
{
public $name;
public $due_date;
public $assigned_to;
public $calendar;
public $description;
public function addTask()
{
$this->validate([
'name' => 'required|min:3',
// 'due_date' => 'required',
'assigned_to' => 'required',
'calendar' => 'required'
]);
Tasks::create([
'user_id' => user()->id,
'name' => $this->name,
'due_date' => $this->due_date,
'assigned_to' => 1,
'calendar' => $this->calendar,
'description' => $this->description
]);
session()->flash('success', 'Setup now complete.');
return back();
}
public function render()
{
return view('livewire.tasks.add-task');
}
}
I then have this in a blade file which calls a Laravel component
<div>
<x-modal title="Add task">
<x-slot name="trigger">
<button class="text-xs bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">Add New Task</button>
</x-slot>
<form>
<div class="flex w-full sm:content-center flex-wrap mb-6">
<label for="name" class="block text-gray-700 text-sm font-bold mb-2">
{{ __('Name') }}
</label>
<input wire:model="name" id="name" type="text" class="form-input w-full @error('name') border-red-500 @enderror" value="{{ old('name') }}">
@error('name')
<p class="text-red-500 text-xs italic mt-4">
{{ $message }}
</p>
@enderror
</div>
<x-slot name="submit">
<button type="submit"
wire:click="addTask"
class="inline-flex justify-center w-full rounded-md border border-transparent px-4 py-2 bg-teal-600 text-base leading-6 font-medium text-white shadow-sm hover:bg-teal-500 focus:outline-none focus:border-teal-700 focus:shadow-outline-teal transition ease-in-out duration-150 sm:text-sm sm:leading-5"
>
Add Task
</button>
</x-slot>
</form>
</x-modal>
</div>
x-model component, just standard stuff with Tailwind and AlpineJS
<div x-data="{ on: false }">
<span @click="on = true">{{ $trigger }}</span>
<div class="fixed bottom-0 inset-x-0 px-4 pb-4 sm:inset-0 sm:flex sm:items-center sm:justify-center" x-show="on">
<div
class="fixed inset-0 transition-opacity"
x-show="on"
x-transition:enter="ease-out duration-300"
x-transition:enter-start="opacity-0"
x-transition:enter-end="opacity-100"
x-transition:leave="ease-in duration-200"
x-transition:leave-start="opacity-100"
x-transition:leave-end="opacity-0"
>
<div class="absolute inset-0 bg-gray-500 opacity-75"></div>
</div>
<div class="bg-white rounded-lg overflow-hidden shadow-xl transform transition-all sm:max-w-lg sm:w-full"
role="dialog"
aria-modal="true"
aria-labelledby="modal-headline"
x-show="on"
x-transition:enter="ease-out duration-300"
x-transition:enter-start="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
x-transition:enter-end="opacity-100 translate-y-0 sm:scale-100"
x-transition:leave="ease-in duration-200"
x-transition:leave-start="opacity-100 translate-y-0 sm:scale-100"
x-transition:leave-end="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
>
<div class="bg-white px-4 pt-5 pb-4 sm:p-6 sm:pb-4">
<div class="sm:flex sm:items-start">
<div class="mt-3 text-center sm:mt-0 sm:ml-4 sm:text-left w-full">
<h3 class="text-lg leading-6 font-medium text-gray-900"
id="modal-headline"
>
{{ $title ?? '' }}
</h3>
<div class="mt-2">
<p class="text-sm leading-5 text-gray-500">
{{ $slot }}
</p>
</div>
</div>
</div>
</div>
<div class="bg-gray-50 px-4 py-3 sm:px-6 sm:flex sm:flex-row-reverse">
<span class="flex w-full rounded-md shadow-sm sm:ml-3 sm:w-auto">
{{ $submit }}
</span>
<span class="mt-3 flex w-full rounded-md shadow-sm sm:mt-0 sm:w-auto">
<button type="button"
@click="on = false"
class="inline-flex justify-center w-full rounded-md border border-gray-300 px-4 py-2 bg-white text-base leading-6 font-medium text-gray-700 shadow-sm hover:text-gray-500 focus:outline-none focus:border-blue-300 focus:shadow-outline-blue transition ease-in-out duration-150 sm:text-sm sm:leading-5"
>
{{ $cancelLabel ?? 'Cancel' }}
</button>
</span>
</div>
</div>
</div>
</div>
My question is within the Livewire component just before this takes place
session()->flash('success', 'Setup now complete.');
return back();
How would I fire off the event to close the modal window and show the flash message? What do I need javascript wise and what should be called in the Livewire component?