Skip to content

Commit 8f1781c

Browse files
committed
Fix select plugin with tests
1 parent c6ff6fb commit 8f1781c

File tree

2 files changed

+95
-53
lines changed

2 files changed

+95
-53
lines changed

src/Html/Options/Plugins/Select.php

Lines changed: 42 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,6 @@
1212
*/
1313
trait Select
1414
{
15-
/**
16-
* Set select option value.
17-
*
18-
* @param bool|array $value
19-
* @return $this
20-
* @see https://datatables.net/reference/option/select
21-
*/
22-
public function select(bool|array $value = true): static
23-
{
24-
$this->attributes['select'] = $value;
25-
26-
return $this;
27-
}
28-
2915
/**
3016
* Set select blurable option value.
3117
*
@@ -35,9 +21,7 @@ public function select(bool|array $value = true): static
3521
*/
3622
public function selectBlurable(bool $value = true): static
3723
{
38-
$this->attributes['select']['blurable'] = $value;
39-
40-
return $this;
24+
return $this->select(['blurable' => $value]);
4125
}
4226

4327
/**
@@ -49,9 +33,7 @@ public function selectBlurable(bool $value = true): static
4933
*/
5034
public function selectClassName(string $value = 'selected'): static
5135
{
52-
$this->attributes['select']['className'] = $value;
53-
54-
return $this;
36+
return $this->select(['className' => $value]);
5537
}
5638

5739
/**
@@ -80,9 +62,7 @@ public function selectAddClassName(string $class): static
8062
*/
8163
public function selectInfo(bool $value = true): static
8264
{
83-
$this->attributes['select']['info'] = $value;
84-
85-
return $this;
65+
return $this->select(['info' => $value]);
8666
}
8767

8868
/**
@@ -94,9 +74,7 @@ public function selectInfo(bool $value = true): static
9474
*/
9575
public function selectItems(string $value = 'row'): static
9676
{
97-
$this->attributes['select']['items'] = $value;
98-
99-
return $this;
77+
return $this->select(['items' => $value]);
10078
}
10179

10280
/**
@@ -107,9 +85,7 @@ public function selectItems(string $value = 'row'): static
10785
*/
10886
public function selectItemsRow(): static
10987
{
110-
$this->attributes['select']['items'] = Builder::SELECT_ITEMS_ROW;
111-
112-
return $this;
88+
return $this->select(['items' => Builder::SELECT_ITEMS_ROW]);
11389
}
11490

11591
/**
@@ -120,9 +96,7 @@ public function selectItemsRow(): static
12096
*/
12197
public function selectItemsColumn(): static
12298
{
123-
$this->attributes['select']['items'] = Builder::SELECT_ITEMS_COLUMN;
124-
125-
return $this;
99+
return $this->select(['items' => Builder::SELECT_ITEMS_COLUMN]);
126100
}
127101

128102
/**
@@ -133,9 +107,7 @@ public function selectItemsColumn(): static
133107
*/
134108
public function selectItemsCell(): static
135109
{
136-
$this->attributes['select']['items'] = Builder::SELECT_ITEMS_CELL;
137-
138-
return $this;
110+
return $this->select(['items' => Builder::SELECT_ITEMS_CELL]);
139111
}
140112

141113
/**
@@ -147,9 +119,7 @@ public function selectItemsCell(): static
147119
*/
148120
public function selectSelector(string $value = 'td'): static
149121
{
150-
$this->attributes['select']['selector'] = $value;
151-
152-
return $this;
122+
return $this->select(['selector' => $value]);
153123
}
154124

155125
/**
@@ -161,9 +131,7 @@ public function selectSelector(string $value = 'td'): static
161131
*/
162132
public function selectStyle(string $value = 'os'): static
163133
{
164-
$this->attributes['select']['style'] = $value;
165-
166-
return $this;
134+
return $this->select(['style' => $value]);
167135
}
168136

169137
/**
@@ -174,9 +142,7 @@ public function selectStyle(string $value = 'os'): static
174142
*/
175143
public function selectStyleApi(): static
176144
{
177-
$this->attributes['select']['style'] = Builder::SELECT_STYLE_API;
178-
179-
return $this;
145+
return $this->select(['style' => Builder::SELECT_STYLE_API]);
180146
}
181147

182148
/**
@@ -187,7 +153,23 @@ public function selectStyleApi(): static
187153
*/
188154
public function selectStyleSingle(): static
189155
{
190-
$this->attributes['select']['style'] = Builder::SELECT_STYLE_SINGLE;
156+
return $this->select(['style' => Builder::SELECT_STYLE_SINGLE]);
157+
}
158+
159+
/**
160+
* Set select option value.
161+
*
162+
* @param bool|array $value
163+
* @return $this
164+
* @see https://datatables.net/reference/option/select
165+
*/
166+
public function select(bool|array $value = true): static
167+
{
168+
if (is_array($value)) {
169+
$this->attributes['select'] = array_merge((array) $this->attributes['select'], $value);
170+
} else {
171+
$this->attributes['select'] = $value;
172+
}
191173

192174
return $this;
193175
}
@@ -200,9 +182,7 @@ public function selectStyleSingle(): static
200182
*/
201183
public function selectStyleMulti(): static
202184
{
203-
$this->attributes['select']['style'] = Builder::SELECT_STYLE_MULTI;
204-
205-
return $this;
185+
return $this->select(['style' => Builder::SELECT_STYLE_MULTI]);
206186
}
207187

208188
/**
@@ -213,9 +193,7 @@ public function selectStyleMulti(): static
213193
*/
214194
public function selectStyleOS(): static
215195
{
216-
$this->attributes['select']['style'] = Builder::SELECT_STYLE_OS;
217-
218-
return $this;
196+
return $this->select(['style' => Builder::SELECT_STYLE_OS]);
219197
}
220198

221199
/**
@@ -226,8 +204,19 @@ public function selectStyleOS(): static
226204
*/
227205
public function selectStyleMultiShift(): static
228206
{
229-
$this->attributes['select']['style'] = Builder::SELECT_STYLE_MULTI_SHIFT;
207+
return $this->select(['style' => Builder::SELECT_STYLE_MULTI_SHIFT]);
208+
}
230209

231-
return $this;
210+
/**
211+
* @param string|null $key
212+
* @return mixed
213+
*/
214+
public function getSelect(string $key = null): mixed
215+
{
216+
if (is_null($key)) {
217+
return $this->attributes['select'] ?? true;
218+
}
219+
220+
return $this->attributes['select'][$key] ?? false;
232221
}
233222
}

tests/BuilderOptionsPluginsTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Yajra\DataTables\Html\Tests;
44

5+
use Yajra\DataTables\Html\Builder;
56
use Yajra\DataTables\Html\Button;
67
use Yajra\DataTables\Html\SearchPane;
78

@@ -279,4 +280,56 @@ public function it_has_search_panes_plugin()
279280
$builder->searchPanes(SearchPane::make()->show()->cascadePanes());
280281
$this->assertEquals(['show' => true, 'cascadePanes' => true], $builder->getAttribute('searchPanes'));
281282
}
283+
284+
/** @test */
285+
public function it_has_select_plugin()
286+
{
287+
$builder = $this->getHtmlBuilder();
288+
$builder->select();
289+
290+
$this->assertTrue($builder->getAttribute('select'));
291+
$this->assertTrue($builder->getSelect());
292+
293+
$builder->selectBlurable()
294+
->selectClassName()
295+
->selectInfo()
296+
->selectItems()
297+
->selectSelector()
298+
->selectStyle();
299+
300+
$this->assertEquals(true, $builder->getSelect('blurable'));
301+
$this->assertEquals('selected', $builder->getSelect('className'));
302+
$this->assertEquals(true, $builder->getSelect('info'));
303+
$this->assertEquals('row', $builder->getSelect('items'));
304+
$this->assertEquals('td', $builder->getSelect('selector'));
305+
$this->assertEquals('os', $builder->getSelect('style'));
306+
307+
$builder->selectAddClassName('test');
308+
$this->assertEquals('selected test', $builder->getSelect('className'));
309+
310+
$builder->selectItemsRow();
311+
$this->assertEquals(Builder::SELECT_ITEMS_ROW, $builder->getSelect('items'));
312+
313+
$builder->selectItemsColumn();
314+
$this->assertEquals(Builder::SELECT_ITEMS_COLUMN, $builder->getSelect('items'));
315+
316+
$builder->selectItemsCell();
317+
$this->assertEquals(Builder::SELECT_ITEMS_CELL, $builder->getSelect('items'));
318+
319+
$builder->selectStyleSingle();
320+
$this->assertEquals(Builder::SELECT_STYLE_SINGLE, $builder->getSelect('style'));
321+
322+
$builder->selectStyleMulti();
323+
$this->assertEquals(Builder::SELECT_STYLE_MULTI, $builder->getSelect('style'));
324+
325+
$builder->selectStyleOS();
326+
$this->assertEquals(Builder::SELECT_STYLE_OS, $builder->getSelect('style'));
327+
328+
$builder->selectStyleMultiShift();
329+
$this->assertEquals(Builder::SELECT_STYLE_MULTI_SHIFT, $builder->getSelect('style'));
330+
331+
$builder->selectStyleApi();
332+
$this->assertEquals(Builder::SELECT_STYLE_API, $builder->getSelect('style'));
333+
}
334+
282335
}

0 commit comments

Comments
 (0)