2021-09-10 16:02:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\Views\Components\Forms;
|
|
|
|
|
|
|
|
class Field extends FormComponent
|
|
|
|
{
|
|
|
|
protected string $as = 'Input';
|
|
|
|
|
|
|
|
protected string $label = '';
|
|
|
|
|
2021-09-20 15:45:38 +00:00
|
|
|
protected ?string $helper = null;
|
2021-09-10 16:02:25 +00:00
|
|
|
|
2021-09-20 15:45:38 +00:00
|
|
|
protected ?string $hint = null;
|
2021-09-10 16:02:25 +00:00
|
|
|
|
|
|
|
public function render(): string
|
|
|
|
{
|
2021-09-20 15:45:38 +00:00
|
|
|
$helperText = '';
|
|
|
|
if ($this->helper !== null) {
|
|
|
|
$helperId = $this->id . 'Help';
|
|
|
|
$helperText = '<Forms.Helper id="' . $helperId . '">' . $this->helper . '</Forms.Helper>';
|
|
|
|
$this->attributes['aria-describedby'] = $helperId;
|
|
|
|
}
|
2021-09-10 16:02:25 +00:00
|
|
|
|
|
|
|
$labelAttributes = [
|
2023-06-12 14:47:38 +00:00
|
|
|
'for' => $this->id,
|
2021-09-10 16:02:25 +00:00
|
|
|
'isOptional' => $this->required ? 'false' : 'true',
|
|
|
|
];
|
2021-09-20 15:45:38 +00:00
|
|
|
if ($this->hint) {
|
|
|
|
$labelAttributes['hint'] = $this->hint;
|
2021-09-10 16:02:25 +00:00
|
|
|
}
|
|
|
|
$labelAttributes = stringify_attributes($labelAttributes);
|
|
|
|
|
|
|
|
// remove field specific attributes to inject the rest to Form Component
|
|
|
|
$fieldComponentAttributes = $this->attributes;
|
|
|
|
unset($fieldComponentAttributes['as']);
|
|
|
|
unset($fieldComponentAttributes['label']);
|
|
|
|
unset($fieldComponentAttributes['class']);
|
2022-01-23 19:00:08 +00:00
|
|
|
unset($fieldComponentAttributes['helper']);
|
|
|
|
unset($fieldComponentAttributes['hint']);
|
2021-09-10 16:02:25 +00:00
|
|
|
|
2021-10-21 13:12:38 +00:00
|
|
|
$fieldComponentAttributes['class'] = 'mb-1';
|
|
|
|
|
|
|
|
$element = __NAMESPACE__ . '\\' . $this->as;
|
|
|
|
$fieldElement = new $element($fieldComponentAttributes);
|
2021-09-10 16:02:25 +00:00
|
|
|
|
|
|
|
return <<<HTML
|
|
|
|
<div class="flex flex-col {$this->class}">
|
|
|
|
<Forms.Label {$labelAttributes}>{$this->label}</Forms.Label>
|
2021-10-21 13:12:38 +00:00
|
|
|
{$fieldElement->render()}
|
2021-09-10 16:02:25 +00:00
|
|
|
{$helperText}
|
|
|
|
</div>
|
|
|
|
HTML;
|
|
|
|
}
|
|
|
|
}
|