From 4438b5ede3768602d2c6996b7486a81e81917cc9 Mon Sep 17 00:00:00 2001 From: Iosif Chatzimichail Date: Thu, 17 Oct 2024 02:24:29 +0300 Subject: [PATCH] Fixes #30 --- src/Http/Livewire/Traits/HandleFieldOptions.php | 14 ++++++++++++++ src/Http/Livewire/Traits/IsLivewireFilter.php | 1 + tests/Feature/LfCheckboxFilterTest.php | 15 ++++++++++++--- tests/Feature/LfRadioFilterTest.php | 15 ++++++++++++--- tests/Feature/LfSelectFilterTest.php | 15 ++++++++++++--- 5 files changed, 51 insertions(+), 9 deletions(-) diff --git a/src/Http/Livewire/Traits/HandleFieldOptions.php b/src/Http/Livewire/Traits/HandleFieldOptions.php index 679271d..2f3494d 100644 --- a/src/Http/Livewire/Traits/HandleFieldOptions.php +++ b/src/Http/Livewire/Traits/HandleFieldOptions.php @@ -29,6 +29,20 @@ protected function hasOptionsInConfig($field) return array_key_exists('options', $field->toArray()); } + protected function transformOptionsArray($field) + { + $options = collect($field->get('options')); + if (! is_array($options->first()) || ! array_key_exists('key', $options->first())) { + return $field; + } + $field->setConfig(array_merge( + $field->config(), + ['options' => $options->flatMap(fn ($option) => [$option['key'] => $option['value']])->all()] + )); + + return $field; + } + protected function addCountsArrayToConfig($field) { $field->setConfig(array_merge( diff --git a/src/Http/Livewire/Traits/IsLivewireFilter.php b/src/Http/Livewire/Traits/IsLivewireFilter.php index 048cdf4..89b3695 100644 --- a/src/Http/Livewire/Traits/IsLivewireFilter.php +++ b/src/Http/Livewire/Traits/IsLivewireFilter.php @@ -38,6 +38,7 @@ public function initiateField() if ($field->type() == 'terms') { $field = $this->addTermsToOptions($field); } elseif ($this->hasOptionsInConfig($field)) { + $field = $this->transformOptionsArray($field); $field = $this->addCountsArrayToConfig($field); } elseif ($this->hasCustomOptions()) { $field = $this->addCustomOptionsToConfig($field); diff --git a/tests/Feature/LfCheckboxFilterTest.php b/tests/Feature/LfCheckboxFilterTest.php index e5a7f4e..3621971 100644 --- a/tests/Feature/LfCheckboxFilterTest.php +++ b/tests/Feature/LfCheckboxFilterTest.php @@ -42,9 +42,18 @@ public function setUp(): void 'display' => 'Checkbox', 'listable' => 'hidden', 'options' => [ - 'option1' => 'Option 1', - 'option2' => 'Option 2', - 'option3' => 'Option 3', + [ + 'key' => 'option1', + 'value' => 'Option 1', + ], + [ + 'key' => 'option2', + 'value' => 'Option 2', + ], + [ + 'key' => 'option3', + 'value' => 'Option 3', + ], ], ], ], diff --git a/tests/Feature/LfRadioFilterTest.php b/tests/Feature/LfRadioFilterTest.php index 5b8e9c6..8a855b3 100644 --- a/tests/Feature/LfRadioFilterTest.php +++ b/tests/Feature/LfRadioFilterTest.php @@ -42,9 +42,18 @@ public function setUp(): void 'display' => 'Radio', 'listable' => 'hidden', 'options' => [ - 'option1' => 'Option 1', - 'option2' => 'Option 2', - 'option3' => 'Option 3', + [ + 'key' => 'option1', + 'value' => 'Option 1', + ], + [ + 'key' => 'option2', + 'value' => 'Option 2', + ], + [ + 'key' => 'option3', + 'value' => 'Option 3', + ], ], ], ], diff --git a/tests/Feature/LfSelectFilterTest.php b/tests/Feature/LfSelectFilterTest.php index 11e34af..7318d5d 100644 --- a/tests/Feature/LfSelectFilterTest.php +++ b/tests/Feature/LfSelectFilterTest.php @@ -41,9 +41,18 @@ public function setUp(): void 'display' => 'Select', 'listable' => 'hidden', 'options' => [ - 'option1' => 'Option 1', - 'option2' => 'Option 2', - 'option3' => 'Option 3', + [ + 'key' => 'option1', + 'value' => 'Option 1', + ], + [ + 'key' => 'option2', + 'value' => 'Option 2', + ], + [ + 'key' => 'option3', + 'value' => 'Option 3', + ], ], ], ],