Skip to content

Commit

Permalink
Add explanation support
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-alrek committed Feb 14, 2023
1 parent 322fece commit 6ad8246
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Apility/Forms/Concerns/Attributes/Explanation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Apility\Forms\Concerns\Attributes;

use Apility\Forms\View\Components\Field;

trait Explanation
{
public function explanation()
{
/** @var Field $this */

return once(fn () => $this->field->getFormFieldOptions()['explanation'] ?? null);
}

public function explanationId()
{
/** @var Field $this */

if ($this->explanation() !== null) {
$id = method_exists($this, 'id') ? $this->id() : null;
return implode('-', array_filter([$id ?? null, 'explanation']));
}
}
}
3 changes: 3 additions & 0 deletions src/Apility/Forms/View/Components/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Apility\Forms\View\Components;

use Apility\Forms\Concerns\Attributes\Explanation;
use Illuminate\View\Component;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\View;
Expand All @@ -11,6 +12,8 @@

abstract class Field extends Component
{
use Explanation;

public FormFieldContract $field;

public function __construct(FormFieldContract $field)
Expand Down
13 changes: 13 additions & 0 deletions src/resources/views/components/fields/input.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@
'step' => $step ?? null,
'title' => $title ?? null,
'value' => $value ?? null,
'explanation' => $explanationId ?? null
])
])
>
@if($explanation)
aria-describedby="{{ implode('-', array_filter([$id ?? null, 'explanation'])) }}"
@endif
>
@if($explanation ?? null)
<div
id="{{ $explanationId ?? null }}"
class="form-text"
>
{{ $explanation }}
</div>
@endif
3 changes: 3 additions & 0 deletions src/resources/views/partials/explanation.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@if($explanationId() !== null)
aria-describedby="{{ $explanationId }}"
@endif

0 comments on commit 6ad8246

Please sign in to comment.