To address the issue of targeting the correct element for wire:loading when calling a $parent method in Livewire 3, you need to ensure that the wire:target is correctly set to the method being called. However, since you're calling a parent method, you need to make sure that the child component's loading state is correctly managed.
Here's a step-by-step solution:
-
Parent Component: Define the
downloadCSVmethod in your parent component.
// ParentComponent.php
public function downloadCSV($callId)
{
// Your logic to download CSV
}
-
Child Component: In your child component, you will call the parent method using
$parent.
// ChildComponent.php
public function triggerDownloadCSV($callId)
{
$this->emitUp('downloadCSV', $callId);
}
-
Child Blade View: In your child component's view, set up the button with
wire:clickandwire:loadingdirectives.
<!-- child-component.blade.php -->
<button wire:click="triggerDownloadCSV({{ $call->call_id }})" wire:loading.attr="disabled">
Download CSV
</button>
<div wire:loading wire:target="triggerDownloadCSV({{ $call->call_id }})">
Loading...
</div>
- Parent Blade View: Ensure the parent component listens for the event emitted by the child component.
<!-- parent-component.blade.php -->
<div>
@livewire('child-component')
<script>
Livewire.on('downloadCSV', callId => {
@this.downloadCSV(callId);
});
</script>
</div>
Explanation:
-
Child Component: The
triggerDownloadCSVmethod in the child component emits an event to the parent component usingemitUp. -
Child Blade View: The button in the child view uses
wire:clickto call thetriggerDownloadCSVmethod. Thewire:loadingdirective is used to show a loading state while the method is being processed. -
Parent Blade View: The parent component listens for the
downloadCSVevent and calls thedownloadCSVmethod with the providedcallId.
This setup ensures that the loading state is managed within the child component, and the parent method is called correctly without interfering with the child's loading state.
By following these steps, you should be able to customize the wire:target of the child element to be triggered correctly.