49 lines
1.1 KiB
PHP
49 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use Livewire\Component;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class SelectForm extends Component
|
|
{
|
|
public $value;
|
|
public $name;
|
|
public $label;
|
|
public $required = false;
|
|
public $optionsList = [];
|
|
public $url;
|
|
|
|
public function mount($name, $label, $required = false, $url = null, $optionsList = [])
|
|
{
|
|
$this->name = $name;
|
|
$this->label = $label;
|
|
$this->required = $required;
|
|
$this->url = $url;
|
|
$this->optionsList = $optionsList;
|
|
if ($url) {
|
|
$this->fetchOptions();
|
|
}
|
|
}
|
|
|
|
public function fetchOptions()
|
|
{
|
|
try {
|
|
$response = Http::get($this->url);
|
|
$this->optionsList = $response->json('data') ?? [];
|
|
} catch (\Exception $e) {
|
|
session()->flash('error', 'Failed to load options: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function updatedValue()
|
|
{
|
|
$this->dispatch('selectChanged', $this->value);
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.select-form');
|
|
}
|
|
}
|