Skip to content

Commit

Permalink
Render attributes whose value is false (#2845)
Browse files Browse the repository at this point in the history
  • Loading branch information
steffen25 authored Nov 17, 2020
1 parent 0a69182 commit 418275f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/Tags/Concerns/RendersAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ protected function renderAttributes($attributes)
{
return collect($attributes)
->map(function ($value, $attribute) {
if ($value === null || $value === false) {
if ($value === null) {
return;
}

$value = $value === true ? 'true' : $value;
$value = is_bool($value)
? $value
? 'true'
: 'false'
: $value;

return sprintf('%s="%s"', $attribute, $value);
})
Expand Down
30 changes: 26 additions & 4 deletions tests/Tags/Concerns/RendersAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public function it_renders_attributes_from_array()
':name' => 'first_name',
'disabled' => 'true',
'autocomplete' => true,
'dont_render_falses' => false,
'focusable' => false,
'dont_render_nulls' => null,
]);

$this->assertEquals('class="m-0 mb-1" :name="first_name" disabled="true" autocomplete="true"', $output);
$this->assertEquals('class="m-0 mb-1" :name="first_name" disabled="true" autocomplete="true" focusable="false"', $output);
}

/** @test */
Expand All @@ -44,14 +44,14 @@ public function it_renders_attributes_from_params()
'class' => 'm-0 mb-1',
':name' => 'first_name',
'attr:src' => 'avatar.jpg',
'dont_render_falses' => false,
'focusable' => false,
'dont_render_nulls' => null,
'disabled' => 'true',
'autocomplete' => true,
])
->renderAttributesFromParams();

$this->assertEquals('class="m-0 mb-1" name="Han" src="avatar.jpg" disabled="true" autocomplete="true"', $output);
$this->assertEquals('class="m-0 mb-1" name="Han" src="avatar.jpg" focusable="false" disabled="true" autocomplete="true"', $output);
}

/** @test */
Expand All @@ -77,6 +77,28 @@ public function it_wont_render_attributes_for_known_params_unless_attr_prepended

$this->assertEquals('class="m-0 mb-1" src="avatar.jpg"', $output);
}

/** @test */
public function it_will_render_falsy_attributes()
{
$this->assertEquals('', $this->tag->renderAttributesFromParams());

$output = $this->tag
->setContext(['first_name' => 'Han'])
->setParameters([
'class' => 'm-0 mb-1',
':name' => 'first_name',
'attr:src' => 'avatar.jpg',
'focusable' => false,
'dont_render_nulls' => null,
'disabled' => 'true',
'autocomplete' => true,
'aria-hidden' => true,
])
->renderAttributesFromParams();

$this->assertEquals('class="m-0 mb-1" name="Han" src="avatar.jpg" focusable="false" disabled="true" autocomplete="true" aria-hidden="true"', $output);
}
}

class FakeTagWithRendersAttributes extends Tags
Expand Down

0 comments on commit 418275f

Please sign in to comment.