|
16 | 16 | namespace Ease\TWB5; |
17 | 17 |
|
18 | 18 | /** |
19 | | - * Description of FormGroup. |
| 19 | + * Bootstrap 5 form group. |
20 | 20 | * |
21 | | - * @author vitex |
| 21 | + * Wraps a label, a form control and optional helper text using Bootstrap 5 |
| 22 | + * markup (.mb-3 wrapper, .form-label label, .form-text helper). Replaces the |
| 23 | + * Bootstrap 4 .form-group based widget. |
| 24 | + * |
| 25 | + * @author Vítězslav Dvořák <info@vitexsoftware.cz> |
22 | 26 | */ |
23 | 27 | class FormGroup extends \Ease\Html\DivTag |
24 | 28 | { |
25 | | - public function __construct($label, \Ease\Html\Input $input, $desc = '') |
26 | | - { |
27 | | - $id = $input->setTagID(); |
28 | | - parent::__construct(new \Ease\Html\LabelTag($id, $label)); |
29 | | - $input->addTagClass('form-control'); |
30 | | - $input->setTagProperties(['aria-describedby' => 'desc'.$id]); |
31 | | - $this->addItem($input); |
32 | | - |
33 | | - if ($desc) { |
34 | | - $this->addItem(new DivTag($desc, ['id' => 'desc'.$id, 'class' => 'form-text'])); |
| 29 | + /** |
| 30 | + * Bootstrap 5 form group. |
| 31 | + * |
| 32 | + * @param mixed $label field label (string, Tag or array) |
| 33 | + * @param mixed $content form control widget (or any renderable content) |
| 34 | + * @param string $placeholder placeholder text put into the control |
| 35 | + * @param mixed $helptext helper text rendered below the control |
| 36 | + * @param string $addTagClass CSS class applied to the control (Bootstrap: form-control) |
| 37 | + */ |
| 38 | + public function __construct( |
| 39 | + $label = null, |
| 40 | + $content = null, |
| 41 | + $placeholder = null, |
| 42 | + $helptext = null, |
| 43 | + $addTagClass = 'form-control', |
| 44 | + ) { |
| 45 | + parent::__construct(null, ['class' => 'mb-3']); |
| 46 | + |
| 47 | + // Resolve an id used to bind the label to the control. |
| 48 | + $id = null; |
| 49 | + |
| 50 | + if (\is_object($content) && method_exists($content, 'getTagID')) { |
| 51 | + $id = $content->getTagID(); |
| 52 | + } |
| 53 | + |
| 54 | + if (empty($id) && \is_string($label) && $label !== '') { |
| 55 | + $id = \Ease\Functions::lettersOnly($label); |
| 56 | + } |
| 57 | + |
| 58 | + if (empty($id)) { |
| 59 | + $id = 'formgroup_'.\Ease\Functions::randomString(); |
| 60 | + } |
| 61 | + |
| 62 | + if ($label !== null && $label !== '') { |
| 63 | + $this->addItem(new \Ease\Html\LabelTag((string) $id, $label, ['class' => 'form-label'])); |
| 64 | + } |
| 65 | + |
| 66 | + if (\is_object($content)) { |
| 67 | + if ($addTagClass && method_exists($content, 'addTagClass')) { |
| 68 | + $content->addTagClass($addTagClass); |
| 69 | + } |
| 70 | + |
| 71 | + if ($placeholder && method_exists($content, 'setTagProperties')) { |
| 72 | + $content->setTagProperties(['placeholder' => $placeholder]); |
| 73 | + } |
| 74 | + |
| 75 | + if (method_exists($content, 'setTagID')) { |
| 76 | + $content->setTagID((string) $id); |
| 77 | + } |
| 78 | + |
| 79 | + if ($helptext && method_exists($content, 'setTagProperties')) { |
| 80 | + $content->setTagProperties(['aria-describedby' => 'desc'.$id]); |
| 81 | + } |
| 82 | + |
| 83 | + $this->addItem($content); |
| 84 | + } elseif ($content !== null) { |
| 85 | + $this->addItem($content); |
| 86 | + } |
| 87 | + |
| 88 | + if ($helptext) { |
| 89 | + $this->addItem(new \Ease\Html\DivTag($helptext, ['id' => 'desc'.$id, 'class' => 'form-text'])); |
35 | 90 | } |
36 | 91 | } |
37 | 92 | } |
0 commit comments