Skip to content
This repository was archived by the owner on Mar 18, 2025. It is now read-only.

Commit 6c53c53

Browse files
authored
Support simple array options in x-select (#89)
1 parent 7bf0e42 commit 6c53c53

File tree

2 files changed

+57
-13
lines changed

2 files changed

+57
-13
lines changed

src/Components/Inputs/Select.php

+26-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Rawilk\FormComponents\Components\Inputs;
44

5-
use function config;
5+
use Illuminate\Database\Eloquent\Model;
66
use Illuminate\Support\Arr;
77
use Illuminate\Support\Collection;
88
use Illuminate\Support\HtmlString;
@@ -62,18 +62,7 @@ public function __construct(
6262
$this->disabledField = $disabledField ?? config('form-components.defaults.global.disabled_field', 'disabled');
6363
$this->childrenField = $childrenField ?? config('form-components.defaults.global.children_field', 'children');
6464

65-
$this->options = collect($options)
66-
->map(function ($value, $key) {
67-
// If the key is not numeric, we're going to assume this is the value.
68-
if (! is_numeric($key)) {
69-
return [
70-
$this->valueField => $key,
71-
$this->labelField => $value,
72-
];
73-
}
74-
75-
return $value;
76-
})->values();
65+
$this->options = $this->normalizeOptions($options);
7766
}
7867

7968
/**
@@ -99,4 +88,28 @@ public function inputClass(): string
9988
'input-error' => $this->hasErrorsAndShow($this->name),
10089
]);
10190
}
91+
92+
protected function normalizeOptions(array|Collection $options): Collection
93+
{
94+
return collect($options)
95+
->map(function ($value, $key) {
96+
// If the key is not numeric, we're going to assume this is the value.
97+
if (! is_numeric($key)) {
98+
return [
99+
$this->valueField => $key,
100+
$this->labelField => $value,
101+
];
102+
}
103+
104+
// If the value is a simple value, we need to convert it to an array.
105+
if (! is_iterable($value) && ! $value instanceof Model) {
106+
return [
107+
$this->valueField => $value,
108+
$this->labelField => $value,
109+
];
110+
}
111+
112+
return $value;
113+
});
114+
}
102115
}

tests/Components/Inputs/SelectTest.php

+31
Original file line numberDiff line numberDiff line change
@@ -307,3 +307,34 @@
307307
})
308308
->assertSeeInOrder(['Germany', 'Canada', 'United States', 'France']);
309309
});
310+
311+
test('simple array options are supported', function () {
312+
$options = [
313+
'option_1',
314+
'option_2',
315+
];
316+
317+
$template = <<<'HTML'
318+
<form>
319+
<x-select name="my_options" :options="$options" />
320+
</form>
321+
HTML;
322+
323+
Route::get('/test', fn () => Blade::render($template, ['options' => $options]));
324+
325+
get('/test')
326+
->assertFormExists('form', function (AssertForm $form) {
327+
$form->findSelect('select', function (AssertSelect $select) {
328+
$select->containsOptions(
329+
[
330+
'value' => 'option_1',
331+
'text' => 'option_1',
332+
],
333+
[
334+
'value' => 'option_2',
335+
'text' => 'option_2',
336+
],
337+
);
338+
});
339+
});
340+
});

0 commit comments

Comments
 (0)