Skip to content

[TwigComponent] Convert camelCase attributes to dashed attributes #875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion src/TwigComponent/src/ComponentAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function (string $carry, string $key) {
return match ($value) {
true => "{$carry} {$key}",
false => $carry,
default => sprintf('%s %s="%s"', $carry, $key, $value),
default => sprintf('%s %s="%s"', $carry, strtolower(preg_replace('/(?<!^)[A-Z]/', '-$0', $key)), $value),
};
},
''
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% component table with { caption: 'data table', headers: ['key', 'value'], data: [[1, 2], [3, 4]] } %}
{% component table with { caption: 'data table', headers: ['key', 'value'], data: [[1, 2], [3, 4]], dataSort: 'asc' } %}
{% block th %}custom th ({{ parent() }}){% endblock %}
{% block td %}custom td ({{ parent() }}){% endblock %}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<twig:table caption='data table' :headers='["key", "value"]' :data='[[1, 2], [3, 4]]'>
<twig:table caption='data table' :headers='["key", "value"]' :data='[[1, 2], [3, 4]]' dataSort='asc'>
<twig:block name="th">custom th ({{ parent() }})</twig:block>
<twig:block name="td">custom td ({{ parent() }})</twig:block>

<twig:block name="footer">
My footer
</twig:block>
</twig:table>
</twig:table>
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,12 @@ public function testCanRenderComponentWithAttributes(): void
'style' => 'color:red;',
'value' => '',
'autofocus' => true,
'data-size' => 'md',
'dataVariant' => 'success',
]);

$this->assertStringContainsString('Component Content (prop value 1)', $output);
$this->assertStringContainsString('<button class="foo bar" type="button" style="color:red;" value="" autofocus>', $output);
$this->assertStringContainsString('<button class="foo bar" type="button" style="color:red;" value="" autofocus data-size="md" data-variant="success">', $output);

$output = $this->renderComponent('with_attributes', [
'prop' => 'prop value 2',
Expand Down Expand Up @@ -146,6 +148,7 @@ public function testCanRenderEmbeddedComponent(): void
{
$output = self::getContainer()->get(Environment::class)->render('embedded_component.html.twig');

$this->assertStringContainsString('data-sort="asc"', $output);
$this->assertStringContainsString('<caption>data table</caption>', $output);
$this->assertStringContainsString('custom th (key)', $output);
$this->assertStringContainsString('custom td (1)', $output);
Expand Down
1 change: 1 addition & 0 deletions src/TwigComponent/tests/Integration/ComponentLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function testComponentSyntaxCanRenderEmbeddedComponent(): void
{
$output = self::getContainer()->get(Environment::class)->render('tags/embedded_component.html.twig');

$this->assertStringContainsString('data-sort="asc"', $output);
$this->assertStringContainsString('<caption>data table</caption>', $output);
$this->assertStringContainsString('custom th (key)', $output);
$this->assertStringContainsString('custom td (1)', $output);
Expand Down
4 changes: 3 additions & 1 deletion src/TwigComponent/tests/Unit/ComponentAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ public function testCanConvertToString(): void
'style' => 'color:black;',
'value' => '',
'autofocus' => true,
'data-size' => 'md',
'dataVariant' => 'success',
]);

$this->assertSame(' class="foo" style="color:black;" value="" autofocus', (string) $attributes);
$this->assertSame(' class="foo" style="color:black;" value="" autofocus data-size="md" data-variant="success"', (string) $attributes);
}

public function testCanSetDefaults(): void
Expand Down