Skip to content

Commit

Permalink
Fixes #30
Browse files Browse the repository at this point in the history
  • Loading branch information
afonic committed Oct 16, 2024
1 parent 019fdbe commit 4438b5e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 9 deletions.
14 changes: 14 additions & 0 deletions src/Http/Livewire/Traits/HandleFieldOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions src/Http/Livewire/Traits/IsLivewireFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 12 additions & 3 deletions tests/Feature/LfCheckboxFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
],
],
],
Expand Down
15 changes: 12 additions & 3 deletions tests/Feature/LfRadioFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
],
],
],
Expand Down
15 changes: 12 additions & 3 deletions tests/Feature/LfSelectFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
],
],
],
Expand Down

0 comments on commit 4438b5e

Please sign in to comment.