Skip to content
This repository was archived by the owner on Jan 2, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix for radio inputs with value "0"
  • Loading branch information
pascalbaljet committed Feb 11, 2021
commit 389a467ecabfff8a35b9ea5f54cd2d44d045c8ef
8 changes: 6 additions & 2 deletions src/Components/FormRadio.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct(
string $label = '',
$value = 1,
$bind = null,
bool $default = false,
bool $default = null,
bool $showErrors = false
) {
$this->name = $name;
Expand All @@ -32,7 +32,11 @@ public function __construct(
if (!session()->hasOldInput() && $this->isNotWired()) {
$boundValue = $this->getBoundValue($bind, $name);

$this->checked = (is_null($boundValue) ? $default : $boundValue) == $this->value;
if (!is_null($boundValue)) {
$this->checked = $boundValue == $this->value;
} elseif (!is_null($default)) {
$this->checked = $default == $this->value;
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions tests/Feature/RadioTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ public function it_check_the_right_element_as_default()
$this->registerTestRoute('default-radio');

$this->visit('/default-radio')
->seeElement('input[value="1"]:checked')
->seeElement('input[value="0"]:not(:checked)');
}

/** @test */
public function it_check_the_right_element_as_default_with_a_bound_target()
{
$this->registerTestRoute('default-radio-bind');

$this->visit('/default-radio-bind')
->seeElement('input[value="a"]:checked')
->seeElement('input[value="b"]:not(:checked)');
}
Expand Down
17 changes: 17 additions & 0 deletions tests/Feature/views/default-radio-bind.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@php
$target = ['radio' => 'a'];
@endphp

<x-form>
@bind($target)
<x-form-group>
<x-form-radio name="radio" value="a" />
</x-form-group>

<x-form-group>
<x-form-radio name="radio" value="b" />
</x-form-group>
@endbind

<x-form-submit />
</x-form>
6 changes: 2 additions & 4 deletions tests/Feature/views/default-radio.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
@endphp

<x-form>
@bind($target)
<x-form-group>
<x-form-radio name="radio" value="a" />
<x-form-radio name="radio" value="1" default="1" />
</x-form-group>

<x-form-group>
<x-form-radio name="radio" value="b" />
<x-form-radio name="radio" value="0" />
</x-form-group>
@endbind

<x-form-submit />
</x-form>