From 891456c233c2132ea59d3fe59ae6e0074f0fb453 Mon Sep 17 00:00:00 2001 From: Renato Moura Date: Tue, 28 Nov 2023 10:01:16 -0300 Subject: [PATCH] Add Feature floating labels --- src/Elements/FormGroup.php | 19 +++++++++++++++++-- src/Elements/GroupWrapper.php | 7 +++++++ tests/FormGroupTest.php | 12 ++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/Elements/FormGroup.php b/src/Elements/FormGroup.php index 980c365..bf63157 100644 --- a/src/Elements/FormGroup.php +++ b/src/Elements/FormGroup.php @@ -15,6 +15,8 @@ class FormGroup extends Element protected ?InvalidFeedback $invalidFeedback = null; + protected bool $isFloating = false; + public function __construct(Label $label, Element $control) { $this->label = $label; @@ -28,8 +30,13 @@ public function render(): string $html = 'renderAttributes(); $html .= '>'; - $html .= $this->label; - $html .= $this->control; + if ($this->isFloating) { + $html .= $this->control; + $html .= $this->label; + } else { + $html .= $this->label; + $html .= $this->control; + } $html .= $this->renderInvalidFeedback(); $html .= $this->renderFormText(); $html .= ''; @@ -37,6 +44,14 @@ public function render(): string return $html; } + public function floating(): ?self + { + $this->isFloating = true; + $this->addClass('form-floating'); + + return $this; + } + public function formText(string $text): ?self { if (isset($this->formText)) { diff --git a/src/Elements/GroupWrapper.php b/src/Elements/GroupWrapper.php index 699a636..5417e63 100644 --- a/src/Elements/GroupWrapper.php +++ b/src/Elements/GroupWrapper.php @@ -28,6 +28,13 @@ public function formText(string $text): self return $this; } + public function floating(): self + { + $this->formGroup->floating(); + + return $this; + } + public function __toString(): string { return $this->render(); diff --git a/tests/FormGroupTest.php b/tests/FormGroupTest.php index 27982a9..5470255 100644 --- a/tests/FormGroupTest.php +++ b/tests/FormGroupTest.php @@ -99,4 +99,16 @@ public function testCanIncludeHtmlInLabels() $result = $formGroup->render(); $this->assertEquals($expected, $result); } + + public function testCanRenderWithFloatingLabels() + { + $label = $this->builder->label('Email'); + $text = $this->builder->text('email'); + $formGroup = new FormGroup($label, $text); + $formGroup->floating(); + + $expected = '
'; + $result = $formGroup->render(); + $this->assertEquals($expected, $result); + } }