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

Commit 8f2d38f

Browse files
committed
Fix for #20
1 parent 5ac410e commit 8f2d38f

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed

src/Components/FormRadio.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@
22

33
namespace ProtoneMedia\LaravelFormComponents\Components;
44

5-
class FormRadio extends FormCheckbox
5+
class FormRadio extends Component
66
{
7+
use HandlesValidationErrors;
8+
use HandlesBoundValues;
9+
10+
public string $name;
11+
public string $label;
12+
public $value;
13+
public bool $checked = false;
14+
715
public function __construct(
816
string $name,
917
string $label = '',
@@ -12,6 +20,19 @@ public function __construct(
1220
bool $default = false,
1321
bool $showErrors = false
1422
) {
15-
parent::__construct($name, $label, $value, $bind, $default, $showErrors);
23+
$this->name = $name;
24+
$this->label = $label;
25+
$this->value = $value;
26+
$this->showErrors = $showErrors;
27+
28+
if (old($name)) {
29+
$this->checked = old($name) == $value;
30+
}
31+
32+
if (!session()->hasOldInput() && $this->isNotWired()) {
33+
$boundValue = $this->getBoundValue($bind, $name);
34+
35+
$this->checked = (is_null($boundValue) ? $default : $boundValue) == $this->value;
36+
}
1637
}
1738
}

tests/Feature/RadioTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace ProtoneMedia\LaravelFormComponents\Tests\Feature;
4+
5+
use Illuminate\Http\Request;
6+
use ProtoneMedia\LaravelFormComponents\Tests\TestCase;
7+
8+
class RadioTest extends TestCase
9+
{
10+
/** @test */
11+
public function it_check_the_right_element_as_default()
12+
{
13+
$this->registerTestRoute('default-radio');
14+
15+
$this->visit('/default-radio')
16+
->seeElement('input[value="a"]:checked')
17+
->seeElement('input[value="b"]:not(:checked)');
18+
}
19+
20+
/** @test */
21+
public function it_does_check_the_right_input_element_after_a_validation_error()
22+
{
23+
$this->registerTestRoute('radio-validation', function (Request $request) {
24+
$data = $request->validate([
25+
'radio' => 'required|in:a',
26+
]);
27+
});
28+
29+
$this->visit('/radio-validation')
30+
->select('b', 'radio')
31+
->press('Submit')
32+
->seeElement('input[value="a"]:not(:checked)')
33+
->seeElement('input[value="b"]:checked');
34+
}
35+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@php
2+
$target = ['radio' => 'a'];
3+
@endphp
4+
5+
<x-form>
6+
@bind($target)
7+
<x-form-group>
8+
<x-form-radio name="radio" value="a" />
9+
</x-form-group>
10+
11+
<x-form-group>
12+
<x-form-radio name="radio" value="b" />
13+
</x-form-group>
14+
@endbind
15+
16+
<x-form-submit />
17+
</x-form>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<x-form>
2+
<x-form-group>
3+
<x-form-radio name="radio" value="a" />
4+
</x-form-group>
5+
6+
<x-form-group>
7+
<x-form-radio name="radio" value="b" />
8+
</x-form-group>
9+
10+
<x-form-submit />
11+
</x-form>

0 commit comments

Comments
 (0)