Skip to content

Commit c279be4

Browse files
Add bool typehint to dataBsToggle() method and add default value to true in data attributes. (#2)
1 parent 603d68f commit c279be4

14 files changed

+110
-70
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Change Log
22

3-
## 0.1.1 Under development
3+
## 0.1.1 March 7, 2024
4+
5+
- Bug #2: Add bool typehint to `dataBsToggle()` method and add default value to `true` in `dataBsTarget()`,
6+
`dataBstoggle()`, `dataDismissTarget()`, `dataDrawerTarget()`, `dataDrawerTarget()`, `dataDropdownToggle()` and
7+
`dataToggle()` (@terabytesoftw)
48

59
## 0.1.0 March 5, 2024
610

src/Data/HasDataBsTarget.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ abstract public function dataAttributes(array $values): static;
2121
*
2222
* @return static A new instance of the current class with the specified toggle attributes.
2323
*/
24-
public function dataBsTarget(bool|string $value): static
24+
public function dataBsTarget(bool|string $value = true): static
2525
{
2626
if (is_string($value)) {
2727
return $this->dataAttributes([DataAttributeValues::BS_TARGET => $value]);

src/Data/HasDataBsToggle.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,27 @@
99
*/
1010
trait HasDataBsToggle
1111
{
12+
protected bool|string $dataBsToggle = false;
13+
1214
abstract public function dataAttributes(array $values): static;
1315

1416
/**
1517
* Set the `HTML` data-bs-toggle attribute for the toggle.
1618
*
17-
* @param string $value The data-bs-toggle attribute value.
19+
* @param bool|string $value The data-bs-toogle attribute value. If true, the value of the id attribute will be
20+
* used.
1821
*
1922
* @return static A new instance of the current class with the specified toggle attributes.
2023
*/
21-
public function dataBsToggle(string $value): static
24+
public function dataBsToggle(bool|string $value = true): static
2225
{
23-
return $this->dataAttributes([DataAttributeValues::BS_TOGGLE => $value]);
26+
if (is_string($value)) {
27+
return $this->dataAttributes([DataAttributeValues::BS_TOGGLE => $value]);
28+
}
29+
30+
$new = clone $this;
31+
$new->dataBsToggle = $value;
32+
33+
return $new;
2434
}
2535
}

src/Data/HasDataDismissTarget.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ abstract public function dataAttributes(array $values): static;
1919
* @param bool|string $value The data-dismiss-target attribute value. If true, the value of the id attribute will be
2020
* used.
2121
*/
22-
public function dataDismissTarget(bool|string $value): static
22+
public function dataDismissTarget(bool|string $value = true): static
2323
{
2424
if (is_string($value)) {
2525
return $this->dataAttributes([DataAttributeValues::DISMISS_TARGET => $value]);

src/Data/HasDataDrawerTarget.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ abstract public function dataAttributes(array $values): static;
2121
*
2222
* @return static A new instance of the current class with the specified toggle attributes.
2323
*/
24-
public function dataDrawerTarget(bool|string $value): static
24+
public function dataDrawerTarget(bool|string $value = true): static
2525
{
2626
if (is_string($value)) {
2727
return $this->dataAttributes([DataAttributeValues::DRAWER_TARGET => $value]);

src/Data/HasDataDropdownToggle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ abstract public function dataAttributes(array $values): static;
2121
*
2222
* @return static A new instance of the current class with the specified toggle attributes.
2323
*/
24-
public function dataDropdownToggle(bool|string $value): static
24+
public function dataDropdownToggle(bool|string $value = true): static
2525
{
2626
if (is_string($value)) {
2727
return $this->dataAttributes([DataAttributeValues::DROPDOWN_TOGGLE => $value]);

src/Data/HasDataToggle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ abstract public function dataAttributes(array $values): static;
2121
*
2222
* @return static A new instance of the current class with the specified toggle attributes.
2323
*/
24-
public function dataToggle(bool|string $value): static
24+
public function dataToggle(bool|string $value = true): static
2525
{
2626
if (is_string($value)) {
2727
return $this->dataAttributes([DataAttributeValues::TOGGLE => $value]);

tests/Data/HasDataBsTargetTest.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,24 @@ public function testDataBsTarget(): void
1515
use HasDataBsTarget;
1616

1717
public array $attributes = [];
18-
};
19-
20-
$instance = $instance->dataBsTarget('value');
21-
22-
$this->assertSame(['data-bs-target' => 'value'], $instance->attributes);
23-
}
24-
25-
public function testDataBsTargetWithTrue(): void
26-
{
27-
$instance = new class () {
28-
use HasData;
29-
use HasDataBsTarget;
30-
31-
public array $attributes = [];
3218

3319
public function getDataBsTarget(): bool|string
3420
{
3521
return $this->dataBsTarget;
3622
}
3723
};
3824

39-
$instance = $instance->dataBsTarget(true);
25+
$instance = $instance->dataBsTarget('value');
26+
27+
$this->assertSame(['data-bs-target' => 'value'], $instance->attributes);
28+
29+
$instance = $instance->dataBsTarget();
4030

4131
$this->assertTrue($instance->getDataBsTarget());
32+
33+
$instance = $instance->dataBsTarget(false);
34+
35+
$this->assertFalse($instance->getDataBsTarget());
4236
}
4337

4438
public function testImmutability(): void

tests/Data/HasDataBsToggleTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,35 @@ public function testDataBsToggle(): void
1515
use HasDataBsToggle;
1616

1717
public array $attributes = [];
18+
19+
public function getDataBsToggle()
20+
{
21+
return $this->dataBsToggle;
22+
}
1823
};
1924

2025
$instance = $instance->dataBsToggle('value');
2126

2227
$this->assertSame(['data-bs-toggle' => 'value'], $instance->attributes);
28+
29+
$instance = $instance->dataBsToggle();
30+
31+
$this->assertTrue($instance->getDataBsToggle());
32+
33+
$instance = $instance->dataBsToggle(false);
34+
35+
$this->assertFalse($instance->getDataBsToggle());
36+
}
37+
38+
public function testImmutability(): void
39+
{
40+
$instance = new class () {
41+
use HasData;
42+
use HasDataBsToggle;
43+
44+
protected array $attributes = [];
45+
};
46+
47+
$this->assertNotSame($instance, $instance->dataBsToggle());
2348
}
2449
}

tests/Data/HasDataCollapseToggleTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,16 @@ public function testDataCollapseToggle(): void
2121

2222
$this->assertSame(['data-collapse-toggle' => 'value'], $instance->attributes);
2323
}
24+
25+
public function testImmutability(): void
26+
{
27+
$instance = new class () {
28+
use HasData;
29+
use HasDataCollapseToggle;
30+
31+
protected array $attributes = [];
32+
};
33+
34+
$this->assertNotSame($instance, $instance->dataCollapseToggle(''));
35+
}
2436
}

0 commit comments

Comments
 (0)