Add support for optgroups in select lists

This commit is contained in:
Christopher C. Wells 2021-01-02 15:12:58 -08:00
parent ab10dd0342
commit 3bc346848c
3 changed files with 17 additions and 9 deletions

View File

@ -8,17 +8,17 @@ use Illuminate\View\Component;
class Select extends Component class Select extends Component
{ {
public Collection $options; public Collection|array $options;
public ?string $selectedValue; public ?string $selectedValue;
/** /**
* Select constructor. * Select constructor.
* *
* @param \Illuminate\Support\Collection $options * @param \Illuminate\Support\Collection|array $options
* @param ?string $selectedValue * @param ?string $selectedValue
*/ */
public function __construct( public function __construct(
Collection $options, Collection|array $options,
?string $selectedValue = '', ?string $selectedValue = '',
) { ) {
$this->options = $options; $this->options = $options;

View File

@ -0,0 +1,13 @@
@foreach ($options as $option)
@if(is_iterable($option['value']))
<optgroup label="{{ $option['label'] }}">
<x-inputs.select-options :options="$option['value']"
:selectedValue="$selectedValue" />
</optgroup>
@else
<option value="{{ $option['value'] }}"
@if ($option['value'] == $selectedValue) selected @endif>
{{ $option['label'] }}
</option>
@endif
@endforeach

View File

@ -1,9 +1,4 @@
<select {{ $attributes->merge(['class' => 'rounded-md shadow-sm border-gray-300 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50']) }}> <select {{ $attributes->merge(['class' => 'rounded-md shadow-sm border-gray-300 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50']) }}>
{{ $slot }} {{ $slot }}
@foreach ($options as $option) <x-inputs.select-options :options="$options" :selectedValue="$selectedValue" />
<option value="{{ $option['value'] }}"
@if ($option['value'] == $selectedValue) selected @endif>
{{ $option['label'] }}
</option>
@endforeach
</select> </select>