22
33namespace Rawilk \FormComponents \Components \Inputs ;
44
5- use function config ;
5+ use Illuminate \ Database \ Eloquent \ Model ;
66use Illuminate \Support \Arr ;
77use Illuminate \Support \Collection ;
88use 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}
0 commit comments