Skip to content

Commit

Permalink
Feat(web-twig): Allow transfer of attributes directly to input element
Browse files Browse the repository at this point in the history
refs #DS-812
  • Loading branch information
literat committed Jun 11, 2023
1 parent 75548bb commit 0c9db8d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
5 changes: 4 additions & 1 deletion packages/web-twig/src/Twig/PropsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ public function renderMainProps(Environment $environment, $props = [], $allowedA
/**
* @param array<string, mixed> $props
* @param array<string, mixed> $allowedAttributes
* @param array<string, mixed> $inputProps
*/
public function renderInputProps(Environment $environment, array $props, $allowedAttributes = []): string
public function renderInputProps(Environment $environment, array $props, $allowedAttributes = [], array $inputProps = []): string
{
$transferringAttributes = [];
foreach ($props as $propName => $propValue) {
Expand All @@ -82,6 +83,8 @@ public function renderInputProps(Environment $environment, array $props, $allowe
}
}

$transferringAttributes = array_merge($transferringAttributes, $inputProps);

return $environment->render('@partials/inputProps.twig', [
'transferringAttributes' => $transferringAttributes,
]);
Expand Down
20 changes: 15 additions & 5 deletions packages/web-twig/tests/Twig/PropsExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,10 @@ public function renderMainPropsDataProvider(): array
* @dataProvider renderInputPropsDataProvider
* @param array<string, mixed> $props
* @param array<string, mixed> $allowedAttributes
* @param array<string, mixed> $inputProps
* @param array<string, mixed> $expectedRenderProps
*/
public function testShouldRenderInputProps(array $props, array $allowedAttributes, array $expectedRenderProps): void
public function testShouldRenderInputProps(array $props, array $allowedAttributes, array $inputProps, array $expectedRenderProps): void
{
$expectedResponse = '';
$environment = m::mock(Environment::class);
Expand All @@ -156,7 +157,7 @@ public function testShouldRenderInputProps(array $props, array $allowedAttribute
->with('@partials/inputProps.twig', $expectedRenderProps)
->andReturn($expectedResponse);

$renderResponse = $this->propsExtension->renderInputProps($environment, $props, $allowedAttributes);
$renderResponse = $this->propsExtension->renderInputProps($environment, $props, $allowedAttributes, $inputProps);

$this->assertSame($expectedResponse, $renderResponse);
}
Expand All @@ -167,7 +168,7 @@ public function testShouldRenderInputProps(array $props, array $allowedAttribute
public function renderInputPropsDataProvider(): array
{
return [
'empty props' => [[], [], [
'empty props' => [[], [], [], [
'transferringAttributes' => [],
]],
'filter only allowed attributes' => [[
Expand All @@ -176,7 +177,7 @@ public function renderInputPropsDataProvider(): array
'max' => '6',
'autocomplete' => 'on',
'placeholder' => 'Your name',
], ['autocomplete', 'placeholder'], [
], ['autocomplete', 'placeholder'], [], [
'transferringAttributes' => [
'min' => '1',
'max' => '6',
Expand All @@ -190,13 +191,22 @@ public function renderInputPropsDataProvider(): array
'max' => '6',
'autocomplete' => 'on',
'placeholder' => null,
], ['autocomplete', 'placeholder'], [
], ['autocomplete', 'placeholder'], [], [
'transferringAttributes' => [
'min' => '1',
'max' => '6',
'autocomplete' => 'on',
],
]],
'pass down input props' => [[], [], [
'data-test' => 'test-id',
'autocomplete' => 'on',
], [
'transferringAttributes' => [
'data-test' => 'test-id',
'autocomplete' => 'on',
],
]],
];
}

Expand Down

0 comments on commit 0c9db8d

Please sign in to comment.