Skip to content

Commit 9bc70c6

Browse files
authored
Merge pull request #37 from VitexSoftware/fix/formgroup-bootstrap5
Fix/formgroup bootstrap5
2 parents c47ae13 + a3f6849 commit 9bc70c6

2 files changed

Lines changed: 69 additions & 14 deletions

File tree

src/Ease/TWB5/FormGroup.php

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,77 @@
1616
namespace Ease\TWB5;
1717

1818
/**
19-
* Description of FormGroup.
19+
* Bootstrap 5 form group.
2020
*
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>
2226
*/
2327
class FormGroup extends \Ease\Html\DivTag
2428
{
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']));
3590
}
3691
}
3792
}

src/Ease/TWB5/Navbar.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ public function __construct($brand = null, $name = 'navbar', $properties = [])
6767
parent::__construct(null, $properties);
6868
Part::twBootstrapize();
6969

70-
$this->leftContent = new UlTag(null, ['class' => 'navbar-nav ms-auto flex-nowrap navbar-expand mb-2 mb-lg-0', 'style' => '--bs-scroll-height: 100px;']);
71-
$this->rightContent = new UlTag(null, ['class' => 'navbar-nav ml-auto']); // TODO
70+
$this->leftContent = new UlTag(null, ['class' => 'navbar-nav flex-nowrap mb-2 mb-lg-0', 'style' => '--bs-scroll-height: 100px;']);
71+
$this->rightContent = new UlTag(null, ['class' => 'navbar-nav ms-auto flex-nowrap mb-2 mb-lg-0']);
7272

7373
$this->containerFluid = $this->addItem(new \Ease\Html\DivTag([new ATag($this->mainpage, $brand, ['class' => 'navbar-brand']), $this->navBarToggler()], ['class' => 'container-fluid']));
7474
}

0 commit comments

Comments
 (0)