Skip to content

Commit

Permalink
Add HasDataValue::class trait. (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Jan 26, 2024
1 parent 4eb897b commit d4ef12a
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 32 deletions.
1 change: 1 addition & 0 deletions src/Attribute/Component/HasToggle.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ private function renderToggleTag(): string
->attributes($this->toggleAttributes)
->class($this->toggleClass)
->content($this->toggleContent)
->id($this->toggleId)
->prefix($this->togglePrefix)
->suffix($this->toggleSuffix)
->tagName($this->toggleTag)
Expand Down
27 changes: 27 additions & 0 deletions src/Attribute/Custom/HasDataValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace PHPForge\Html\Attribute\Custom;

/**
* Is used by widgets that implement the data value method.
*/
trait HasDataValue
{
protected string $dataValue = '';

/**
* Set the `HTML` data-value attribute for the toggle.
*
* @param string $value The data-value attribute value.
*
* @return static A new instance of the current class with the specified toggle attributes.
*/
public function dataValue(string $value): static
{
$new = clone $this;
$new->dataValue = $value;
return $new;
}
}
22 changes: 11 additions & 11 deletions src/ButtonToggle.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ final class ButtonToggle extends Element
use Attribute\Custom\HasDataDrawerTarget;
use Attribute\Custom\HasDataDropdownToggle;
use Attribute\Custom\HasDataToggle;
use Attribute\Custom\HasDataValue;
use Attribute\Custom\HasTemplate;
use Attribute\HasClass;
use Attribute\HasData;
Expand Down Expand Up @@ -61,6 +62,7 @@ public function link(): self
protected function loadDefaultDefinitions(): array
{
return [
'id()' => [$this->generateId('button-toggle-')],
'template()' => ['{toggle}\n{icon}\n{content}'],
];
}
Expand All @@ -74,30 +76,28 @@ protected function run(): string
{
$attributes = $this->attributes;

$id = $this->generateId('button-toggle-');

if ($this->ariaControls === true) {
$attributes['aria-controls'] = $this->toggleId;
if ($this->ariaControls === true && $this->dataValue !== '') {
$attributes['aria-controls'] = $this->dataValue;
}

if ($this->dataBsTarget === true) {
$attributes['data-bs-target'] = "#$this->toggleId";
if ($this->dataBsTarget === true && $this->dataValue !== '') {
$attributes['data-bs-target'] = "#$this->dataValue";
}

if ($this->dataDismissTarget === true) {
$attributes['data-dismiss-target'] = $this->toggleId;
$attributes['data-dismiss-target'] = $this->dataValue;
}

if ($this->dataDrawerTarget === true) {
$attributes['data-drawer-target'] = $this->toggleId;
$attributes['data-drawer-target'] = $this->dataValue;
}

if ($this->dataDropdownToggle === true) {
$attributes['data-dropdown-toggle'] = $this->toggleId;
$attributes['data-dropdown-toggle'] = $this->dataValue;
}

if ($this->dataToggle === true) {
$attributes['data-toggle'] = $this->toggleId;
$attributes['data-toggle'] = $this->dataValue;
}

$tokenValues = [
Expand All @@ -113,7 +113,7 @@ protected function run(): string
->ariaDescribedBy($this->ariaDescribedBy)
->attributes($attributes)
->content($content)
->id($id)
->id($this->id)
->role($this->role)
->tagName($this->tagName)
->render();
Expand Down
20 changes: 20 additions & 0 deletions tests/Attribute/Custom/HasDataValueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace PHPForge\Html\Tests\Attribute\Custom;

use PHPForge\Html\Attribute\Custom\HasDataValue;
use PHPUnit\Framework\TestCase;

final class HasDataValueTest extends TestCase
{
public function testImmutability(): void
{
$instance = new class () {
use HasDataValue;
};

$this->assertNotSame($instance, $instance->dataValue(''));
}
}
12 changes: 6 additions & 6 deletions tests/ButtonToggle/BootstrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function testAccordion(): void
{
Assert::equalsWithoutLE(
<<<HTML
<button class="accordion-button" id="button-toggle-658716145f1d9" type="button" aria-expanded="true" data-bs-toggle="collapse" aria-controls="id" data-bs-target="#id">
<button class="accordion-button" id="button-toggle-658716145f1d9" type="button" aria-expanded="true" data-bs-toggle="collapse" aria-controls="value" data-bs-target="#value">
Accordion item #1
</button>
HTML,
Expand All @@ -28,8 +28,8 @@ public function testAccordion(): void
->content('Accordion item #1')
->dataBsToggle('collapse')
->dataBsTarget(true)
->dataValue('value')
->id('button-toggle-658716145f1d9')
->toggleId('id')
->render()
);
}
Expand All @@ -38,13 +38,13 @@ public function testAlert(): void
{
Assert::equalsWithoutLE(
<<<HTML
<button id="button-toggle-658716145f1d9" type="button" aria-label="Close" data-dismiss-target="id"></button>
<button id="button-toggle-658716145f1d9" type="button" aria-label="Close" data-dismiss-target="value"></button>
HTML,
ButtonToggle::widget()
->ariaLabel('Close')
->dataDismissTarget(true)
->dataValue('value')
->id('button-toggle-658716145f1d9')
->toggleId('id')
->render()
);
}
Expand Down Expand Up @@ -193,7 +193,7 @@ public function testNavBar(): void
{
Assert::equalsWithoutLE(
<<<HTML
<button class="navbar-toggler" id="button-toggle-658716145f1d9" type="button" aria-expanded="false" aria-label="Toggle navigation" data-bs-toggle="collapse" aria-controls="id" data-bs-target="#id">
<button class="navbar-toggler" id="button-toggle-658716145f1d9" type="button" aria-expanded="false" aria-label="Toggle navigation" data-bs-toggle="collapse" aria-controls="value" data-bs-target="#value">
<span class="navbar-toggler-icon"></span>
</button>
HTML,
Expand All @@ -204,9 +204,9 @@ public function testNavBar(): void
->class('navbar-toggler')
->dataBsToggle('collapse')
->dataBsTarget(true)
->dataValue('value')
->id('button-toggle-658716145f1d9')
->toggleClass('navbar-toggler-icon')
->toggleId('id')
->render()
);
}
Expand Down
12 changes: 6 additions & 6 deletions tests/ButtonToggle/FlowbiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function testAlert(): void
{
Assert::equalsWithoutLE(
<<<HTML
<button class="ms-auto -mx-1.5 -my-1.5 bg-blue-50 text-blue-500 rounded-lg focus:ring-2 focus:ring-blue-400 p-1.5 hover:bg-blue-200 inline-flex items-center justify-center h-8 w-8 dark:bg-gray-800 dark:text-blue-400 dark:hover:bg-gray-700" id="button-toggle-658716145f1d9" type="button" aria-label="Close" data-dismiss-target="id">
<button class="ms-auto -mx-1.5 -my-1.5 bg-blue-50 text-blue-500 rounded-lg focus:ring-2 focus:ring-blue-400 p-1.5 hover:bg-blue-200 inline-flex items-center justify-center h-8 w-8 dark:bg-gray-800 dark:text-blue-400 dark:hover:bg-gray-700" id="button-toggle-658716145f1d9" type="button" aria-label="Close" data-dismiss-target="value">
<span class="sr-only">Close</span>
<svg xmlns="http://www.w3.org/2000/svg" class="w-6 h-6" aria-hidden="true" fill="currentColor" viewBox="0 0 20 20"><path clip-rule="evenodd" fill-rule="evenodd" d="M2 4.75A.75.75 0 012.75 4h14.5a.75.75 0 010 1.5H2.75A.75.75 0 012 4.75zm0 10.5a.75.75 0 01.75-.75h7.5a.75.75 0 010 1.5h-7.5a.75.75 0 01-.75-.75zM2 10a.75.75 0 01.75-.75h14.5a.75.75 0 010 1.5H2.75A.75.75 0 012 10z"/></svg>
</button>
Expand All @@ -26,12 +26,12 @@ public function testAlert(): void
->ariaLabel('Close')
->class('ms-auto -mx-1.5 -my-1.5 bg-blue-50 text-blue-500 rounded-lg focus:ring-2 focus:ring-blue-400 p-1.5 hover:bg-blue-200 inline-flex items-center justify-center h-8 w-8 dark:bg-gray-800 dark:text-blue-400 dark:hover:bg-gray-700')
->dataDismissTarget(true)
->dataValue('value')
->id('button-toggle-658716145f1d9')
->iconFilePath(dirname(__DIR__, 2) . '/src/Base/Svg/toggle.svg')
->iconTag('svg')
->toggleClass('sr-only')
->toggleContent('Close')
->toggleId('id')
->render()
);
}
Expand All @@ -40,13 +40,14 @@ public function testDropdown(): void
{
Assert::equalsWithoutLE(
<<<HTML
<a id="button-toggle-658716145f1d9" type="button" role="role" data-dropdown-toggle="id">
<a id="button-toggle-658716145f1d9" type="button" role="role" data-dropdown-toggle="value">
<span class="sr-only">Toggle dropdown</span>
<svg xmlns="http://www.w3.org/2000/svg" class="w-6 h-6" aria-hidden="true" fill="currentColor" viewBox="0 0 20 20"><path clip-rule="evenodd" fill-rule="evenodd" d="M2 4.75A.75.75 0 012.75 4h14.5a.75.75 0 010 1.5H2.75A.75.75 0 012 4.75zm0 10.5a.75.75 0 01.75-.75h7.5a.75.75 0 010 1.5h-7.5a.75.75 0 01-.75-.75zM2 10a.75.75 0 01.75-.75h14.5a.75.75 0 010 1.5H2.75A.75.75 0 012 10z"/></svg>
</a>
HTML,
ButtonToggle::widget()
->dataDropdownToggle(true)
->dataValue('value')
->icon(true)
->iconFilePath(dirname(__DIR__, 2) . '/src/Base/Svg/toggle.svg')
->iconTag('svg')
Expand All @@ -55,7 +56,6 @@ public function testDropdown(): void
->role(true)
->toggleClass('sr-only')
->toggleContent('Toggle dropdown')
->toggleId('id')
->render()
);
}
Expand All @@ -64,7 +64,7 @@ public function testSidebar(): void
{
Assert::equalsWithoutLE(
<<<HTML
<button id="button-toggle-658716145f1d9" type="button" aria-controls="id" data-drawer-target="id" data-toggle="id">
<button id="button-toggle-658716145f1d9" type="button" aria-controls="value" data-drawer-target="value" data-toggle="value">
<span class="sr-only">Open sidebar</span>
<svg xmlns="http://www.w3.org/2000/svg" class="w-6 h-6" aria-hidden="true" fill="currentColor" viewBox="0 0 20 20"><path clip-rule="evenodd" fill-rule="evenodd" d="M2 4.75A.75.75 0 012.75 4h14.5a.75.75 0 010 1.5H2.75A.75.75 0 012 4.75zm0 10.5a.75.75 0 01.75-.75h7.5a.75.75 0 010 1.5h-7.5a.75.75 0 01-.75-.75zM2 10a.75.75 0 01.75-.75h14.5a.75.75 0 010 1.5H2.75A.75.75 0 012 10z"/></svg>
</button>
Expand All @@ -73,13 +73,13 @@ public function testSidebar(): void
->ariaControls(true)
->dataDrawerTarget(true)
->dataToggle(true)
->dataValue('value')
->icon(true)
->iconFilePath(dirname(__DIR__, 2) . '/src/Base/Svg/toggle.svg')
->iconTag('svg')
->id('button-toggle-658716145f1d9')
->toggleClass('sr-only')
->toggleContent('Open sidebar')
->toggleId('id')
->render()
);
}
Expand Down
26 changes: 17 additions & 9 deletions tests/ButtonToggle/RenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public function testAriaControls(): void
{
Assert::equalsWithoutLE(
<<<HTML
<button id="button-toggle-658716145f1d9" type="button" aria-controls="id"></button>
<button id="button-toggle-658716145f1d9" type="button" aria-controls="value"></button>
HTML,
ButtonToggle::widget()->ariaControls(true)->id('button-toggle-658716145f1d9')->toggleId('id')->render()
ButtonToggle::widget()->ariaControls(true)->dataValue('value')->id('button-toggle-658716145f1d9')->render()
);
}

Expand Down Expand Up @@ -99,12 +99,12 @@ public function testDataBsTargetWithTrue(): void
{
Assert::equalsWithoutLE(
<<<HTML
<button id="button-toggle-658716145f1d9" type="button" data-bs-target="#toggle-id"></button>
<button id="button-toggle-658716145f1d9" type="button" data-bs-target="#value"></button>
HTML,
ButtonToggle::widget()
->dataBsTarget(true)
->dataValue('value')
->id('button-toggle-658716145f1d9')
->toggleId('toggle-id')
->render()
);
}
Expand Down Expand Up @@ -143,12 +143,12 @@ public function testDataDismissTargetWithTrue(): void
{
Assert::equalsWithoutLE(
<<<HTML
<button id="button-toggle-658716145f1d9" type="button" data-dismiss-target="toggle-id"></button>
<button id="button-toggle-658716145f1d9" type="button" data-dismiss-target="value"></button>
HTML,
ButtonToggle::widget()
->dataDismissTarget(true)
->dataValue('value')
->id('button-toggle-658716145f1d9')
->toggleId('toggle-id')
->render()
);
}
Expand All @@ -167,10 +167,11 @@ public function testDataDrawerTargetWithTrue(): void
{
Assert::equalsWithoutLE(
<<<HTML
<button id="button-toggle-658716145f1d9" type="button" data-drawer-target="toggle-id"></button>
<button id="button-toggle-658716145f1d9" type="button" data-drawer-target="value"></button>
HTML,
ButtonToggle::widget()
->dataDrawerTarget(true)
->dataValue('value')
->id('button-toggle-658716145f1d9')
->toggleId('toggle-id')
->render()
Expand All @@ -191,10 +192,11 @@ public function testDataDropdownToggleWithTrue(): void
{
Assert::equalsWithoutLE(
<<<HTML
<button id="button-toggle-658716145f1d9" type="button" data-dropdown-toggle="toggle-id"></button>
<button id="button-toggle-658716145f1d9" type="button" data-dropdown-toggle="value"></button>
HTML,
ButtonToggle::widget()
->dataDropdownToggle(true)
->dataValue('value')
->id('button-toggle-658716145f1d9')
->toggleId('toggle-id')
->render()
Expand All @@ -215,16 +217,22 @@ public function testDataToggleWithTrue(): void
{
Assert::equalsWithoutLE(
<<<HTML
<button id="button-toggle-658716145f1d9" type="button" data-toggle="toggle-id"></button>
<button id="button-toggle-658716145f1d9" type="button" data-toggle="value"></button>
HTML,
ButtonToggle::widget()
->dataToggle(true)
->dataValue('value')
->id('button-toggle-658716145f1d9')
->toggleId('toggle-id')
->render()
);
}

public function testGenerateId(): void
{
$this->assertStringContainsString('<button id="button-toggle', ButtonToggle::widget()->render());
}

public function testName(): void
{
Assert::equalsWithoutLE(
Expand Down
20 changes: 20 additions & 0 deletions tests/Method.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace PHPForge\Html\Tests;

use PHPForge\Html\ButtonToggle;
use PHPUnit\Framework\TestCase;

final class Method extends TestCase
{
public function testPrintMethods(): void
{
$methods = get_class_methods(ButtonToggle::class);

sort($methods);
var_dump(implode("\n", $methods));
die;
}
}

0 comments on commit d4ef12a

Please sign in to comment.