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

77 lines
2.8 KiB
PHP

{{--
Label + input + bantuan/galat. Type "password" otomatis mendapat tombol tampil/sembunyikan (hanya jika JS aktif).
Atribut lain (required, autofocus, autocomplete, inputmode, maxlength...) diteruskan ke <input>.
--}}
@props([
'name',
'label',
'type' => 'text',
'value' => null,
'hint' => null,
'error' => null,
'optional' => false,
'id' => null,
'prefix' => null,
'suffix' => null,
])
@php
$id ??= 'field-'.\Illuminate\Support\Str::slug($name);
$isPassword = $type === 'password';
$message = $error ?? (isset($errors) ? $errors->first($name) : null);
$current = $isPassword ? null : old($name, $value);
$helpId = $id.'-help';
// Lebar prefiks/sufiks diperkirakan dengan satuan ch agar teks input tidak menimpa satuan
$pads = collect(['--pad-start' => $prefix, '--pad-end' => $suffix])
->filter(fn ($text) => filled($text))
->map(fn ($text, $var) => $var.': calc('.mb_strlen($text).'ch + 1.75rem)')
->implode('; ');
@endphp
<div class="field" @if ($isPassword) x-data="passwordField" @endif>
<label class="field__label" for="{{ $id }}">
{{ $label }}
@if ($optional) <span class="field__optional">(opsional)</span> @endif
</label>
<div class="field__control">
@if (filled($prefix)) <span class="field__affix field__affix--start">{{ $prefix }}</span> @endif
<input
id="{{ $id }}"
name="{{ $name }}"
type="{{ $type }}"
@if ($current !== null) value="{{ $current }}" @endif
@if ($message) aria-invalid="true" @endif
@if ($attributes->get('required')) aria-required="true" @endif
aria-describedby="{{ $helpId }}"
@if ($pads) style="{{ $pads }}" @endif
@if ($isPassword) x-bind:type="show ? 'text' : 'password'" @endif
{{ $attributes->class(['field__input', 'field__input--password' => $isPassword]) }}
>
@if (filled($suffix)) <span class="field__affix field__affix--end">{{ $suffix }}</span> @endif
@if ($isPassword)
<button
type="button"
class="field__toggle"
aria-label="Tampilkan kata sandi"
aria-pressed="false"
x-on:click="toggle()"
x-bind:aria-pressed="show"
>
<x-heroicon-o-eye x-show="!show" />
<x-heroicon-o-eye-slash x-show="show" x-cloak />
</button>
@endif
</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>