Skip to content

Commit d929fff

Browse files
committed
Fix responsive plugin with tests
1 parent 9179a0c commit d929fff

File tree

2 files changed

+64
-19
lines changed

2 files changed

+64
-19
lines changed

src/Html/Options/Plugins/Responsive.php

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,6 @@
1111
*/
1212
trait Responsive
1313
{
14-
/**
15-
* Set responsive option value.
16-
*
17-
* @param bool|array $value
18-
* @return $this
19-
* @see https://datatables.net/reference/option/responsive
20-
*/
21-
public function responsive(bool|array $value = true): static
22-
{
23-
$this->attributes['responsive'] = $value;
24-
25-
return $this;
26-
}
27-
2814
/**
2915
* Set responsive breakpoints option value.
3016
*
@@ -38,15 +24,21 @@ public function responsiveBreakpoints(array $value): static
3824
}
3925

4026
/**
41-
* Set responsive details option value.
27+
* Set responsive option value.
4228
*
4329
* @param bool|array $value
4430
* @return $this
45-
* @see https://datatables.net/reference/option/responsive.details
31+
* @see https://datatables.net/reference/option/responsive
4632
*/
47-
public function responsiveDetails(bool|array $value): static
33+
public function responsive(bool|array $value = true): static
4834
{
49-
return $this->responsive(['details' => $value]);
35+
if (is_array($value)) {
36+
$this->attributes['responsive'] = array_merge((array) $this->attributes['responsive'], $value);
37+
} else {
38+
$this->attributes['responsive'] = $value;
39+
}
40+
41+
return $this;
5042
}
5143

5244
/**
@@ -61,6 +53,25 @@ public function responsiveDetailsDisplay(array|string $value): static
6153
return $this->responsiveDetails(['display' => $value]);
6254
}
6355

56+
/**
57+
* Set responsive details option value.
58+
*
59+
* @param bool|array $value
60+
* @return $this
61+
* @see https://datatables.net/reference/option/responsive.details
62+
*/
63+
public function responsiveDetails(bool|array $value): static
64+
{
65+
$responsive = (array) $this->getResponsive();
66+
if (is_array($value)) {
67+
$responsive['details'] = array_merge((array) ($responsive['details'] ?? []), $value);
68+
} else {
69+
$responsive['details'] = $value;
70+
}
71+
72+
return $this->responsive($responsive);
73+
}
74+
6475
/**
6576
* Set responsive details renderer option value.
6677
*
@@ -108,4 +119,17 @@ public function responsiveOrthogonal(string $value): static
108119
{
109120
return $this->responsive(['orthogonal' => $value]);
110121
}
122+
123+
/**
124+
* @param string|null $key
125+
* @return mixed
126+
*/
127+
public function getResponsive(string $key = null): mixed
128+
{
129+
if (is_null($key)) {
130+
return $this->attributes['responsive'] ?? true;
131+
}
132+
133+
return $this->attributes['responsive'][$key] ?? false;
134+
}
111135
}

tests/BuilderOptionsPluginsTest.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,26 @@ public function it_has_keys_plugin()
158158
$this->assertEquals(1, $builder->getKeys('tabIndex'));
159159
}
160160

161-
161+
/** @test */
162+
public function it_has_responsive_plugin()
163+
{
164+
$builder = $this->getHtmlBuilder();
165+
$builder->responsive();
166+
167+
$this->assertTrue($builder->getAttribute('responsive'));
168+
$this->assertTrue($builder->getResponsive());
169+
170+
$builder->responsiveBreakpoints([1])
171+
->responsiveDetailsDisplay('display')
172+
->responsiveDetailsRenderer('renderer')
173+
->responsiveDetailsTarget('target')
174+
->responsiveDetailsType('type')
175+
->responsiveOrthogonal('orthogonal');
176+
$this->assertEquals([1], $builder->getResponsive('breakpoints'));
177+
$this->assertEquals('display', $builder->getResponsive('details')['display']);
178+
$this->assertEquals('renderer', $builder->getResponsive('details')['renderer']);
179+
$this->assertEquals('target', $builder->getResponsive('details')['target']);
180+
$this->assertEquals('type', $builder->getResponsive('details')['type']);
181+
$this->assertEquals('orthogonal', $builder->getResponsive('orthogonal'));
182+
}
162183
}

0 commit comments

Comments
 (0)