diff --git a/CHANGELOG.md b/CHANGELOG.md index 7513053..1d5f885 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Adds support for compiling component attributes of the form `` - Adds support for compiling component attributes of the form `` - Adds support for compiling component attributes of the form `` +- 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 diff --git a/src/Compiler/TemplateCompiler.php b/src/Compiler/TemplateCompiler.php index 0c9c3f7..1724c9f 100644 --- a/src/Compiler/TemplateCompiler.php +++ b/src/Compiler/TemplateCompiler.php @@ -444,7 +444,7 @@ protected function compileNodes(array $nodes): string $compiledComponentTemplate = <<<'PHP' 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; + } +} diff --git a/tests/Compiler/AttributesTest.php b/tests/Compiler/AttributesTest.php index dcd2b3f..b3195a4 100644 --- a/tests/Compiler/AttributesTest.php +++ b/tests/Compiler/AttributesTest.php @@ -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(''), + ); + + $this->assertSame( + 'none second value | ', + $this->render(''), + ); + + $this->assertSame( + 'none none | ', + $this->render(''), + ); + + $this->assertSame( + 'the one second value | class="one two three"', + $this->render(''), + ); +}); diff --git a/tests/resources/components/attribute_merging/bar.blade.php b/tests/resources/components/attribute_merging/bar.blade.php new file mode 100644 index 0000000..ac3a906 --- /dev/null +++ b/tests/resources/components/attribute_merging/bar.blade.php @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tests/resources/components/attribute_merging/foo.blade.php b/tests/resources/components/attribute_merging/foo.blade.php new file mode 100644 index 0000000..d8e7942 --- /dev/null +++ b/tests/resources/components/attribute_merging/foo.blade.php @@ -0,0 +1,5 @@ +@props([ + 'one' => 'none', + 'twoWord' => 'none', +]) +{{ $one }} {{ $twoWord }} | {{ $attributes }} \ No newline at end of file