Skip to content

Commit

Permalink
Refactor button and toggle components. (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Dec 31, 2023
1 parent 5cb2253 commit 2f82472
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 79 deletions.
5 changes: 5 additions & 0 deletions src/Attribute/Component/HasIcon.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
*/
trait HasIcon
{
protected bool $icon = true;
protected array $iconAttributes = [];
protected string $iconClass = '';
protected bool $iconContainer = false;
protected array $iconContainerAttributes = [];
protected string $iconContainerTag = 'div';
protected string $iconContent = '';
protected string $iconFilePath = '';
protected string $iconTag = 'svg';

/**
* @return array The `HTML` attributes of the icon of the menu item.
Expand Down Expand Up @@ -196,6 +200,7 @@ private function renderIconTag(): string
$iconTag = match ($this->iconTag) {
'svg' => Svg::widget()
->attributes($this->iconAttributes)
->class($this->iconClass)
->content($this->iconContent)
->filePath($this->iconFilePath)
->render(),
Expand Down
2 changes: 2 additions & 0 deletions src/Attribute/Component/HasToggle.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
*/
trait HasToggle
{
protected bool $toggle = true;
protected array $toggleAttributes = [];
protected string $toggleClass = '';
protected bool $toggleClassOverride = false;
protected string $toggleContent = '';
protected string $toggleTag = 'span';
protected string $toggleId = '';

/**
Expand Down
3 changes: 0 additions & 3 deletions src/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ final class Button extends Element
use Attribute\Input\HasType;

protected array $attributes = [];
protected bool $container = false;
protected string $containerTag = 'div';
protected string $tagName = 'button';
protected string $type = 'button';

/**
* This method is used to configure the widget with the provided default definitions.
Expand Down
6 changes: 0 additions & 6 deletions src/ButtonToggle.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,7 @@ final class ButtonToggle extends Element
use Attribute\Input\HasName;

protected array $attributes = [];
protected bool $icon = true;
protected string $iconTag = 'svg';
protected bool $iconContainer = false;
protected string $iconContainerTag = 'div';
protected string $tagName = 'button';
protected bool $toggle = true;
protected string $toggleTag = 'span';

/**
* Set tag name to `a` for the toggle.
Expand Down
1 change: 0 additions & 1 deletion src/Input/Base/AbstractButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ abstract class AbstractButton extends Element implements LabelInterface, InputIn
use Attribute\Input\HasValue;

protected array $attributes = [];
protected string $type = 'button';

/**
* This method is used to configure the widget with the provided default definitions.
Expand Down
90 changes: 21 additions & 69 deletions tests/Attribute/Component/HasIconTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ public function testClass(): void
$instance = new class () {
use HasIcon;

private bool $icon = false;
private bool $iconContainer = false;
private string $iconContainerTag = 'div';
private string $iconTag = 'i';

public function getIconClass(): string
{
return $this->iconClass;
Expand All @@ -39,11 +34,6 @@ public function testContainerClass(): void
$instance = new class () {
use HasIcon;

private bool $icon = false;
private bool $iconContainer = false;
private string $iconContainerTag = 'div';
private string $iconTag = 'i';

public function getIconContainerClass(): string
{
return $this->iconContainerAttributes['class'] ?? '';
Expand All @@ -69,11 +59,6 @@ public function testGetIconAttributes(): void
{
$instance = new class () {
use HasIcon;

private bool $icon = false;
private bool $iconContainer = false;
private string $iconContainerTag = 'div';
private string $iconTag = 'i';
};

$this->assertEmpty($instance->getIconAttributes());
Expand All @@ -87,11 +72,6 @@ public function testGetIconContainerAttributes(): void
{
$instance = new class () {
use HasIcon;

private bool $icon = false;
private bool $iconContainer = false;
private string $iconContainerTag = 'div';
private string $iconTag = 'i';
};

$this->assertEmpty($instance->getIconContainerAttributes());
Expand All @@ -105,11 +85,6 @@ public function testGetIconContent(): void
{
$instance = new class () {
use HasIcon;

private bool $icon = false;
private bool $iconContainer = false;
private string $iconContainerTag = 'div';
private string $iconTag = 'i';
};

$this->assertEmpty($instance->getIconContent());
Expand All @@ -123,11 +98,6 @@ public function testImmutability(): void
{
$instance = new class () {
use HasIcon;

private bool $icon = false;
private bool $iconContainer = false;
private string $iconContainerTag = 'div';
private string $iconTag = 'i';
};

$this->assertNotSame($instance, $instance->iconAttributes([]));
Expand All @@ -146,11 +116,6 @@ public function testRenderIconTag(): void
$instance = new class () {
use HasIcon;

private bool $icon = false;
private bool $iconContainer = false;
private string $iconContainerTag = 'div';
private string $iconTag = 'i';

public function getIconTag(): string
{
return $this->iconTag;
Expand All @@ -174,11 +139,6 @@ public function testRenderIconTagWithClass(): void
$instance = new class () {
use HasIcon;

private bool $icon = true;
private bool $iconContainer = false;
private string $iconContainerTag = 'div';
private string $iconTag = 'i';

public function getIconTag(): string
{
return $this->iconTag;
Expand All @@ -192,21 +152,23 @@ public function render(): string

$this->assertEmpty($instance->render());

$instance = $instance->iconClass('class');
$instance = $instance->iconClass('class')->iconContent('content');

$this->assertSame('<i class="class"></i>', $instance->render());
Assert::equalsWithoutLE(
<<<HTML
<svg class="class">
content
</svg>
HTML,
$instance->render()
);
}

public function testRenderIconTagWithContainer(): void
{
$instance = new class () {
use HasIcon;

private bool $icon = true;
private bool $iconContainer = true;
private string $iconContainerTag = 'div';
private string $iconTag = 'i';

public function getIconTag(): string
{
return $this->iconTag;
Expand All @@ -220,12 +182,14 @@ public function render(): string

$this->assertEmpty($instance->render());

$instance = $instance->iconClass('class');
$instance = $instance->iconClass('class')->iconContainer(true)->iconContent('content');

Assert::equalsWithoutLE(
<<<HTML
<div>
<i class="class"></i>
<svg class="class">
content
</svg>
</div>
HTML,
$instance->render()
Expand All @@ -237,11 +201,6 @@ public function testRenderIconTagWithContent(): void
$instance = new class () {
use HasIcon;

private bool $icon = true;
private bool $iconContainer = false;
private string $iconContainerTag = 'div';
private string $iconTag = 'i';

public function getIconTag(): string
{
return $this->iconTag;
Expand All @@ -255,21 +214,21 @@ public function render(): string

$this->assertEmpty($instance->render());

$instance = $instance->iconContent('content');
$instance = $instance->iconContent('content')->iconTag('i');

$this->assertSame('<i>content</i>', $instance->render());
Assert::equalsWithoutLE(
<<<HTML
<i>content</i>
HTML,
$instance->render()
);
}

public function testRenderIconTagWithSVG(): void
{
$instance = new class () {
use HasIcon;

private bool $icon = true;
private bool $iconContainer = false;
private string $iconContainerTag = 'div';
private string $iconTag = 'svg';

public function getIconTag(): string
{
return $this->iconTag;
Expand Down Expand Up @@ -298,18 +257,13 @@ public function testTag(): void
$instance = new class () {
use HasIcon;

private bool $icon = false;
private bool $iconContainer = false;
private string $iconContainerTag = 'div';
private string $iconTag = 'i';

public function getIconTag(): string
{
return $this->iconTag;
}
};

$this->assertSame('i', $instance->getIconTag());
$this->assertSame('svg', $instance->getIconTag());

$instance = $instance->iconTag('span');

Expand All @@ -323,8 +277,6 @@ public function testTagException(): void

$instance = new class () {
use HasIcon;

protected string $iconTag = 'i';
};

$instance->iconTag('');
Expand Down

0 comments on commit 2f82472

Please sign in to comment.