Skip to content

Commit

Permalink
Add component key support
Browse files Browse the repository at this point in the history
  • Loading branch information
PhiloNL committed Aug 2, 2024
1 parent 032f220 commit 50cbd4b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
8 changes: 4 additions & 4 deletions js/wire-extender.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function renderComponents(components)
.then(response => response.json())
.then(data => {
for (let component in data.components) {
let el = document.querySelector(`[data-component-id="${component}"]`);
let el = document.querySelector(`[data-component-key="${component}"]`);
el.innerHTML = data.components[component];
}

Expand All @@ -103,12 +103,12 @@ document.addEventListener('DOMContentLoaded', function() {
let components = [];

document.querySelectorAll('livewire').forEach((el) => {
if (!el.hasAttribute('data-component-id')) {
el.setAttribute('data-component-id', Math.random().toString(36).substring(2));
if (!el.hasAttribute('data-component-key')) {
el.setAttribute('data-component-key', Math.random().toString(36).substring(2));
}

components.push({
id: el.getAttribute('data-component-id'),
key: el.getAttribute('data-component-key'),
name: el.getAttribute('data-component'),
params: el.getAttribute('data-params')
});
Expand Down
5 changes: 3 additions & 2 deletions src/Http/Controllers/EmbedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class EmbedController
public function __invoke(Request $request)
{
$components = collect($request->json('components', []))->mapWithKeys(function ($component) {
$componentId = $component['id'];
$componentKey = $component['key'];
$componentName = $component['name'];
$componentParams = json_decode($component['params'], true) ?? [];

Expand All @@ -21,7 +21,8 @@ public function __invoke(Request $request)
}

return [
$componentId => Blade::render('@livewire($component, $params)', [
$componentKey => Blade::render('@livewire($component, $params, key($key))', [
'key' => $componentKey,
'component' => $componentName,
'params' => $componentParams,
]),
Expand Down

0 comments on commit 50cbd4b

Please sign in to comment.