From 78f56fd2bcb7254aa1bc384c801e087475ca793c Mon Sep 17 00:00:00 2001 From: lion Date: Mon, 18 Dec 2023 19:23:38 +0800 Subject: [PATCH] Fixed: If 'select' key is indecies, lost 'selected' attribute. Added unit test (#2769) * Fixed: If 'select' value is number, lost 'selected' attribute --- resources/views/fields/select.blade.php | 2 +- tests/Unit/Screen/Fields/SelectTest.php | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/resources/views/fields/select.blade.php b/resources/views/fields/select.blade.php index f1e41c2156..9fabff367e 100644 --- a/resources/views/fields/select.blade.php +++ b/resources/views/fields/select.blade.php @@ -12,7 +12,7 @@ @isset($value) @if (is_array($value) && in_array($key, $value)) selected @elseif (isset($value[$key]) && $value[$key] == $option) selected - @elseif (!$isOptionList && $key == $value) selected + @elseif (is_string($value) && $key == $value) selected @endif @endisset >{{$option}} diff --git a/tests/Unit/Screen/Fields/SelectTest.php b/tests/Unit/Screen/Fields/SelectTest.php index 61cbd8a867..ec90a133ea 100644 --- a/tests/Unit/Screen/Fields/SelectTest.php +++ b/tests/Unit/Screen/Fields/SelectTest.php @@ -78,6 +78,21 @@ public function testSetValue(): void $this->assertStringContainsString('value="second" selected', $view); } + public function testSetIndicesValue(): void + { + $select = Select::make('Indices') + ->value('1') + ->options([ + '0' => 'First Value', + '1' => 'Second Value', + '2' => 'Third Value' + ]); + + $view = self::minifyRenderField($select); + + $this->assertStringContainsString('value="1" selected', $view); + } + public function testAutoFocus(): void { $select = Select::make('about')