Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Crud/src/Components/Fragment.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use MoonShine\Support\Enums\JsEvent;
use MoonShine\UI\Components\AbstractWithComponents;
use MoonShine\UI\Traits\HasAsync;
use Closure;
use Throwable;

class Fragment extends AbstractWithComponents implements HasAsyncContract
Expand All @@ -27,6 +28,8 @@ class Fragment extends AbstractWithComponents implements HasAsyncContract
protected string $view = 'moonshine::components.fragment';

protected ?int $interval = null;

protected Closure|bool $updateOnLoad = false;

/**
* @param iterable<array-key, ComponentContract> $components
Expand Down Expand Up @@ -109,6 +112,16 @@ public function withSelectorsParams(array $selectors): static
);
}

/**
* @param (Closure(static): bool)|bool $condition
*/
public function updateOnLoad(Closure|bool $condition = true): static
{
$this->updateOnLoad = $condition;

return $this;
}

public function autoUpdate(int $ms): static
{
if ($ms <= 0) {
Expand Down Expand Up @@ -141,6 +154,7 @@ protected function viewData(): array
{
return [
'interval' => $this->interval,
'updateOnLoad' => value($this->updateOnLoad, $this),
];
}
}
2 changes: 1 addition & 1 deletion src/UI/resources/js/Components/Fragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default (asyncUpdateRoute = '') => ({

t.asyncUpdateRoute = mergeURLString(t.asyncUpdateRoute, bodyParams.toString())

const eventDetailQuery = getQueryString(this.$event.detail)
const eventDetailQuery = this.$event ? getQueryString(this.$event.detail) : null;
Comment thread
lee-to marked this conversation as resolved.

if (eventDetailQuery) {
t.asyncUpdateRoute = mergeURLString(t.asyncUpdateRoute, eventDetailQuery)
Expand Down
9 changes: 9 additions & 0 deletions src/UI/resources/views/components/fragment.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@props([
'name',
'interval' => null,
'updateOnLoad' => null,
'components' => [],
])
@fragment($name)
Expand All @@ -13,6 +14,14 @@
</div>
@endfragment

@if($updateOnLoad)
<script>
document.addEventListener('DOMContentLoaded', () => {
window.dispatchEvent(new CustomEvent("fragment_updated:" + @js($name)))
});
</script>
@endif

@if($interval)
<script>
setInterval(() => {
Expand Down