Skip to content

[Twig][Live] null attribute values render without value (ie autofocus) #261

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

Merged
merged 1 commit into from
Feb 4, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public function testCanDehydrateAndHydrateComponentsWithAttributes(): void
$factory = self::getContainer()->get('ux.twig_component.component_factory');

/** @var ComponentWithAttributes $component */
$component = $factory->create('with_attributes', $attributes = ['class' => 'foo']);
$component = $factory->create('with_attributes', $attributes = ['class' => 'foo', 'value' => null]);

$this->assertSame($attributes, $component->attributes->all());

Expand Down
8 changes: 7 additions & 1 deletion src/TwigComponent/src/ComponentAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ public function __toString(): string
{
return array_reduce(
array_keys($this->attributes),
fn (string $carry, string $key) => sprintf('%s %s="%s"', $carry, $key, $this->attributes[$key]),
function (string $carry, string $key) {
if (null === $this->attributes[$key]) {
return "{$carry} {$key}";
}

return sprintf('%s %s="%s"', $carry, $key, $this->attributes[$key]);
},
''
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/TwigComponent/src/HasAttributesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public function mountAttributes(array $data): array
}

foreach ($data as $key => $value) {
if (!is_scalar($value)) {
throw new \LogicException(sprintf('Unable to use "%s" (%s) as an attribute. Attributes must be scalar. If you meant to mount this value on your component, make sure this is a writable property.', $key, get_debug_type($value)));
if (!is_scalar($value) && null !== $value) {
throw new \LogicException(sprintf('Unable to use "%s" (%s) as an attribute. Attributes must be scalar or null. If you meant to mount this value on your component, make sure this is a writable property.', $key, get_debug_type($value)));
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/TwigComponent/src/Resources/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,19 @@ When rendering the component, you can pass an array of html attributes to add:
My Component!
</div>

Set an attribute's value to ``null`` to exclude the value when rendering:

.. code-block:: twig

{# templates/components/my_component.html.twig #}
<input{{ attributes}}/>

{# render component #}
{{ component('my_component', { type: 'text', value: '', autofocus: null }) }}

{# renders as: #}
<input type="text" value="" autofocus/>

Defaults & Merging
~~~~~~~~~~~~~~~~~~

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{{ component('component_a', { propA: 'prop a value', propB: 'prop b value' }) }}
{{ component('with_attributes', { prop: 'prop value 1', class: 'bar', style: 'color:red;' }) }}
{{ component('with_attributes', { prop: 'prop value 1', class: 'bar', style: 'color:red;', value: '', autofocus: null }) }}
{{ component('with_attributes', { prop: 'prop value 2', attributes: { class: 'baz' }, type: 'submit', style: 'color:red;' }) }}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function testCanRenderComponentWithAttributes(): void
$output = self::getContainer()->get(Environment::class)->render('template_a.html.twig');

$this->assertStringContainsString('Component Content (prop value 1)', $output);
$this->assertStringContainsString('<button class="foo bar" type="button" style="color:red;">', $output);
$this->assertStringContainsString('<button class="foo bar" type="button" style="color:red;" value="" autofocus>', $output);
$this->assertStringContainsString('Component Content (prop value 2)', $output);
$this->assertStringContainsString('<button class="foo baz" type="submit" style="color:red;">', $output);
}
Expand Down
11 changes: 8 additions & 3 deletions src/TwigComponent/tests/Unit/ComponentAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ final class ComponentAttributesTest extends TestCase
{
public function testCanConvertToString(): void
{
$attributes = new ComponentAttributes(['class' => 'foo', 'style' => 'color:black;']);

$this->assertSame(' class="foo" style="color:black;"', (string) $attributes);
$attributes = new ComponentAttributes([
'class' => 'foo',
'style' => 'color:black;',
'value' => '',
'autofocus' => null,
]);

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

public function testCanSetDefaults(): void
Expand Down