Files
siakad/resources/views/components/form/select.blade.php
T
2026-10-24 11:45:32 +07:00

64 lines
2.2 KiB
PHP

{{--
Label + <select> + bantuan/galat, selaras dengan form.field. `options` = nilai => label; atau isi slot dengan <option>.
Atribut lain (required, disabled, x-model, onchange...) diteruskan ke <select>. old() menang atas value.
--}}
@props([
'name',
'label',
'options' => [],
'value' => null,
'placeholder' => null,
'hint' => null,
'error' => null,
'optional' => false,
'id' => null,
])
@php
$key = trim(str_replace(['[', ']'], ['.', ''], $name), '.');
$id ??= 'field-'.\Illuminate\Support\Str::slug($key, '-');
$message = $error ?? (isset($errors) ? $errors->first($key) : null);
$current = old($key, $value);
$helpId = $id.'-help';
$hasSlot = trim((string) $slot) !== '';
@endphp
<div class="field">
<label class="field__label" for="{{ $id }}">
{{ $label }}
@if ($optional) <span class="field__optional">(opsional)</span> @endif
</label>
<div class="field__control">
<select
id="{{ $id }}"
name="{{ $name }}"
@if ($message) aria-invalid="true" @endif
@if ($attributes->get('required')) aria-required="true" @endif
aria-describedby="{{ $helpId }}"
{{ $attributes->class(['field__input', 'field__select']) }}
>
@if ($placeholder !== null)
<option value="">{{ $placeholder }}</option>
@endif
@if ($hasSlot)
{{ $slot }}
@else
@foreach ($options as $optionValue => $optionLabel)
<option value="{{ $optionValue }}" @selected($current !== null && (string) $current === (string) $optionValue)>{{ $optionLabel }}</option>
@endforeach
@endif
</select>
<span class="field__chevron" aria-hidden="true"><x-heroicon-m-chevron-down /></span>
</div>
<p id="{{ $helpId }}" @class(['field__help', 'field__help--error' => $message]) aria-live="polite" @if ($message) role="alert" @endif>
@if ($message)
<x-heroicon-s-exclamation-circle />
<span>{{ $message }}</span>
@elseif ($hint)
<span>{{ $hint }}</span>
@endif
</p>
</div>