Skip to content

Commit ce95664

Browse files
authored
Merge pull request #37 from wmde/kebab-case
Handle kebab-case attributes in component calls
2 parents 2d28b47 + 67d182b commit ce95664

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/Component.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ private function handleComponent( DOMElement $node, array $data ): bool {
126126
$name = $attribute->name;
127127
$value = $attribute->value;
128128
}
129+
// template kebab-case -> JS camelCase
130+
$name = preg_replace_callback( '/-(\w)/', fn ( $m ) => strtoupper( $m[1] ), $name );
129131
$componentData[$name] = $value;
130132
}
131133
$rendered = $this->app->renderComponentToDOM( $componentName, $componentData );

tests/php/AppTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,17 @@ public function testNestedComponentObjectProp(): void {
6161
$this->assertSame( '<div><p>obj = { a: A, b: B }</p></div>', $result );
6262
}
6363

64+
public function testComponentPropKebabCase(): void {
65+
$app = new App( [] );
66+
$app->registerComponentTemplate(
67+
'root',
68+
'<div><x-a some-long-prop="A B C"></x-a><x-a :some-long-prop="someLongVar"></x-a></div>'
69+
);
70+
$app->registerComponentTemplate( 'x-a', '<p>{{ someLongProp }}</p>' );
71+
72+
$result = $app->renderComponent( 'root', [ 'someLongVar' => 'X Y Z' ] );
73+
74+
$this->assertSame( '<div><p>A B C</p><p>X Y Z</p></div>', $result );
75+
}
76+
6477
}

0 commit comments

Comments
 (0)