@sanjeevkamat Yes, you have to declare the variables.
Here is an example of how I have coded an anonymous select component. $items can be categories, rooms, months, ...
class Select extends Component
{
public $selectedId;
public $all;
public $allLabel;
public $items;
public $field;
/**
* Create a new component instance.
*
* @return void
*/
public function __construct($selectedId = null, $all = true, $allLabel, $items, $field = 'name')
{
$this->selectedId = $selectedId;
$this->all = $all;
$this->allLabel = $allLabel;
$this->items = $items;
$this->field = $field;
}
...
}
And the view.
<div {{ $attributes->merge(['class' => 'flex items-center']) }}>
<select class="appearance-none w-full outline-none bg-gray-500 rounded text-white p-3">
@if ($all)
<option value="" wire:key="item-0">{{ $allLabel }}</option>
@endif
@foreach ($items as $item)
<option value="{{ $item->id }}" wire:key="{{ 'item-'.$item->id }}" @selected($item['id'] == $selectedId)>{{ $item->$field }}</option>
@endforeach
</select>
<span class="absolute right-8 text-white py-3 pointer-events-none"><i class="fa-solid fa-chevron-down"></i></span>
</div>