Skip to content

Commit

Permalink
Add override parameter to activeClass method. (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Dec 30, 2023
1 parent 2368ecf commit 9f2571b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/Attribute/Component/HasActiveClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@
trait HasActiveClass
{
protected string $activeClass = 'active';
protected bool $override = false;

/**
* Set the `CSS` class to be appended to the active class.
*
* @param string $value The `CSS` class to be appended to the active class.
* @param bool $override Whether to override the current active class or not.
*
* @return static A new instance of the current class with the specified active class.
*/
public function activeClass(string $value): static
public function activeClass(string $value, bool $override = false): static
{
$new = clone $this;
$new->activeClass = $value;
$new->override = $override;

return $new;
}
Expand Down
29 changes: 28 additions & 1 deletion tests/Attribute/Component/HasActiveClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,33 @@ public function testImmutability(): void
use HasActiveClass;
};

$this->assertNotSame($instance, $instance->activeClass(''));
$this->assertNotSame($instance, $instance->activeClass('', true));
}

public function testOverride(): void
{
$instance = new class () {
use HasActiveClass;

public function getActiveClass(): string
{
return $this->activeClass;
}

public function getOverride(): bool
{
return $this->override;
}
};

$instance = $instance->activeClass('active');

$this->assertSame('active', $instance->getActiveClass());
$this->assertFalse($instance->getOverride());

$instance = $instance->activeClass('active', true);

$this->assertSame('active', $instance->getActiveClass());
$this->assertTrue($instance->getOverride());
}
}

0 comments on commit 9f2571b

Please sign in to comment.