Skip to content
This repository was archived by the owner on Jan 2, 2024. It is now read-only.

Commit 29cbe5c

Browse files
authored
Merge pull request #18 from protonemedia/select-bugfix-17
Fix for #17
2 parents 9f5d2ac + 7d1c58b commit 29cbe5c

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

src/Components/FormSelect.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace ProtoneMedia\LaravelFormComponents\Components;
44

5+
use Illuminate\Support\Arr;
6+
57
class FormSelect extends Component
68
{
79
use HandlesValidationErrors;
@@ -47,14 +49,6 @@ public function isSelected($key): bool
4749
return false;
4850
}
4951

50-
if ($this->selectedKey === $key) {
51-
return true;
52-
}
53-
54-
if (is_array($this->selectedKey) && in_array($key, $this->selectedKey)) {
55-
return true;
56-
}
57-
58-
return false;
52+
return in_array($key, Arr::wrap($this->selectedKey));
5953
}
6054
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace ProtoneMedia\LaravelFormComponents\Tests\Feature;
4+
5+
use Illuminate\Http\Request;
6+
use ProtoneMedia\LaravelFormComponents\Tests\TestCase;
7+
8+
class SelectWithoutKeysTest extends TestCase
9+
{
10+
/** @test */
11+
public function it_makes_the_values_numeric()
12+
{
13+
$this->registerTestRoute('select-without-keys', function (Request $request) {
14+
$request->validate([
15+
'select' => 'required|in:a,b',
16+
]);
17+
});
18+
19+
$this->visit('/select-without-keys')
20+
->seeInElement('option[value="0"]', 'a')
21+
->seeInElement('option[value="1"]', 'b')
22+
->seeInElement('option[value="2"]', 'c');
23+
}
24+
25+
/** @test */
26+
public function it_shows_a_validation_error()
27+
{
28+
$this->registerTestRoute('select-without-keys', function (Request $request) {
29+
$request->validate([
30+
'select' => 'required|in:0,1',
31+
]);
32+
});
33+
34+
$this->visit('/select-without-keys')
35+
->select('2', 'select')
36+
->press('Submit')
37+
->seeElement('option[value="0"]:not(:selected)')
38+
->seeElement('option[value="1"]:not(:selected)')
39+
->seeElement('option[value="2"]:selected');
40+
}
41+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<x-form>
2+
<x-form-select name="select" :options="['a', 'b', 'c']" />
3+
<x-form-submit />
4+
</x-form>

0 commit comments

Comments
 (0)