Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Adds support for compiling component attributes of the form `<c-component attribute={{ $value }} />`
- Adds support for compiling component attributes of the form `<c-component attribute={{{ $value }}} />`
- Adds support for compiling component attributes of the form `<c-component attribute={!! $value !!} />`
- Multi-word prop values will be available on nested components when passing `$attributes` to a child component

## [v1.0.4](https://github.com/Stillat/dagger/compare/v1.0.3...v1.0.4) - 2025-01-21

Expand Down
2 changes: 1 addition & 1 deletion src/Compiler/TemplateCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ protected function compileNodes(array $nodes): string
$compiledComponentTemplate = <<<'PHP'
<?php
try {
$__componentData = $compiledParams;
$__componentData = \Stillat\Dagger\Runtime\Attributes::mergeNestedAttributes($compiledParams, $compiledPropNames);
/** COMPILE:PROPS_DEFAULT */
/** COMPILE_IF_VAR:componentGlobalScope $targetVar = get_defined_vars(); */
$__slotContainerVarSuffix = new \Stillat\Dagger\Runtime\SlotContainer;
Expand Down
47 changes: 47 additions & 0 deletions src/Runtime/Attributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Stillat\Dagger\Runtime;

use Illuminate\Support\Str;
use Illuminate\View\ComponentAttributeBag;

class Attributes
{
public static function mergeNestedAttributes(array $data, array $props): array
{
if (! isset($data['attributes']) || empty($props)) {
return $data;
}

$attributes = $data['attributes'];

if ($attributes instanceof ComponentAttributeBag) {
$attributeData = $attributes->all();
} elseif (is_array($attributes)) {
$attributeData = $attributes;
} else {
return $data;
}

$adjustedData = [];

foreach ($attributeData as $key => $value) {
$camelCased = Str::camel($key);

if (isset($props[$camelCased])) {
$key = $camelCased;
}

$adjustedData[$key] = $value;
}

unset($attributeData);
$data = array_merge($adjustedData, $data);

if ($data['attributes'] === $attributes) {
unset($data['attributes']);
}

return $data;
}
}
24 changes: 24 additions & 0 deletions tests/Compiler/AttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,27 @@
$this->render($template, ['value' => '&&'])
);
});

test('passed attributes are merged into the data', function () {
// Framework Reference: https://github.com/laravel/framework/issues/48956

$this->assertSame(
'the one second value | ',
$this->render('<c-attribute_merging.bar one="the one" two-word="second value" />'),
);

$this->assertSame(
'none second value | ',
$this->render('<c-attribute_merging.bar two-word="second value" />'),
);

$this->assertSame(
'none none | ',
$this->render('<c-attribute_merging.bar />'),
);

$this->assertSame(
'the one second value | class="one two three"',
$this->render('<c-attribute_merging.bar one="the one" two-word="second value" class="one two three" />'),
);
});
1 change: 1 addition & 0 deletions tests/resources/components/attribute_merging/bar.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<c-attribute_merging.foo :$attributes></c-attribute_merging.foo>
5 changes: 5 additions & 0 deletions tests/resources/components/attribute_merging/foo.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@props([
'one' => 'none',
'twoWord' => 'none',
])
{{ $one }} {{ $twoWord }} | {{ $attributes }}
Loading