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
handling "false" selected values
  • Loading branch information
ricardoh committed Jan 3, 2022
commit 1d9af7b1bc8721cc9b6c9deb6d8eefe72413027a
4 changes: 3 additions & 1 deletion src/Components/FormSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public function __construct(
if ($this->isNotWired()) {
$inputName = static::convertBracketsToDots(Str::before($name, '[]'));

$default = $this->getBoundValue($bind, $inputName) ?: $default;
if (empty($default)) {
$default = $this->getBoundValue($bind, $inputName);
}

$this->selectedKey = old($inputName, $default);

Expand Down
28 changes: 28 additions & 0 deletions tests/Feature/SelectBooleanValueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace ProtoneMedia\LaravelFormComponents\Tests\Feature;

use ProtoneMedia\LaravelFormComponents\Tests\TestCase;

class SelectBooleanValueTest extends TestCase
{
/** @test */
public function it_shows_the_select_field()
{
$this->registerTestRoute('select-boolean-value');

$this->visit('/select-boolean-value')
->seeElement('option[value="1"]')
->seeElement('option[value="0"]');
}

/** @test */
public function it_shows_the_false_value_selected()
{
$this->registerTestRoute('select-boolean-value');

$this->visit('/select-boolean-value')
->seeElement('option[value="1"]')
->seeElement('option[value="0"][selected="selected"]');
}
}
7 changes: 7 additions & 0 deletions tests/Feature/views/select-boolean-value.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<x-form>
@bind(['select' => '0'])
<x-form-select name="select" :options="['1' => 'Yes', '0' => 'No']" />
@endbind

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