Skip to content

Commit

Permalink
Merge pull request #10 from Stillat/attribute-bag-passing
Browse files Browse the repository at this point in the history
Adds support for passing attribute bag
  • Loading branch information
JohnathonKoster authored Jan 23, 2025
2 parents c9d49bf + b90c681 commit 47b332f
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- `@aware` variables are automatically removed from the attribute bag, without needing to redefine them in `@props`
- Adds support for passing attributes via. the `<c-component {{ $attributes }} />` attribute

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

Expand Down
22 changes: 22 additions & 0 deletions src/Compiler/AttributeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,26 @@ public function compileValue(ParameterNode $parameter): string
return $parameter->valueNode->content;
}

protected function transformParameters(array $parameters): array
{
$newParams = [];

foreach ($parameters as $parameter) {
if ($parameter->type == ParameterType::AttributeEcho) {
$newParams[] = ParameterFactory::makeVariableReference(
'attributes',
(string) str($parameter->content)->trim()->substr(2, -2)->trim()
);

continue;
}

$newParams[] = $parameter;
}

return $newParams;
}

/**
* @param ParameterNode[] $parameters
*/
Expand All @@ -99,6 +119,8 @@ public function toCompiledArray(array $parameters, array $propNames = []): array
return [];
}

$parameters = $this->transformParameters($parameters);

$compiledParameters = [];

foreach ($parameters as $parameter) {
Expand Down
19 changes: 19 additions & 0 deletions src/Compiler/ParameterFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Stillat\Dagger\Compiler;

use Stillat\BladeParser\Nodes\Components\ParameterNode;
use Stillat\BladeParser\Nodes\Components\ParameterType;

class ParameterFactory
{
public static function makeVariableReference(string $variableName, string $value): ParameterNode
{
$param = new ParameterNode;
$param->type = ParameterType::DynamicVariable;
$param->value = $value;
$param->name = $param->materializedName = $variableName;

return $param;
}
}
24 changes: 24 additions & 0 deletions tests/Compiler/AttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,27 @@
$this->render('<c-button data-thing="the thing" title="The Title" />')
);
});

test('attribute passing', function () {
$daggerTemplate = <<<'BLADE'
<c-attribute_passing_root class="mt-4" data-foo="bar" />
BLADE;

$bladeTemplate = <<<'BLADE'
<x-attribute_passing_root class="mt-4" data-foo="bar" />
BLADE;

$expected = <<<'EXPECTED'
Root: Child: data-thing="value" class="mt-4" data-foo="bar"After Child: class="mt-4" data-foo="bar"
EXPECTED;

$this->assertSame(
$expected,
$this->render($daggerTemplate)
);

$this->assertSame(
$expected,
$this->render($bladeTemplate)
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Child: {{ $attributes->merge(['data-thing' => 'value']) }}
2 changes: 2 additions & 0 deletions tests/resources/components/attribute_passing_root.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Root: <c-attribute_passing_child {{ $attributes }} />
After Child: {{ $attributes }}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Child: {{ $attributes->merge(['data-thing' => 'value']) }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Root: <x-attribute_passing_child {{ $attributes }} />
After Child: {{ $attributes }}

0 comments on commit 47b332f

Please sign in to comment.