From 33aa409346e11f0eaf1c40ea5fd87e0e0d2fa806 Mon Sep 17 00:00:00 2001 From: Josh Bruce Date: Sun, 4 Oct 2026 21:34:31 -0400 Subject: [PATCH] add: Fieldset --- src/Forms/Fieldset.php | 94 ++++++++++++++++++++++++++++++++++++++++++ src/Forms/Select.php | 19 +++++---- 2 files changed, 105 insertions(+), 8 deletions(-) create mode 100644 src/Forms/Fieldset.php diff --git a/src/Forms/Fieldset.php b/src/Forms/Fieldset.php new file mode 100644 index 0000000..ffda5fe --- /dev/null +++ b/src/Forms/Fieldset.php @@ -0,0 +1,94 @@ + $this->name . '-warning'; + } + // phpcs:enable + + /** + * @var string[] + */ + private array $wrapperProperties = []; + + /** + * @var string[] + */ + private array $labelProperties = []; + + /** + * @var array + */ + private array $elements = []; + + public static function create( + string|Stringable $label, + string|Stringable $name, + string|PropertyInterface $warningMessage = '' + ): self { + return new self($label, $name, $warningMessage); + } + + final private function __construct( + private readonly string|Stringable $label, + private readonly string|Stringable $name, + private readonly string|PropertyInterface $warningMessage = '' + ) { + } + + public function elements(string|Stringable ...$elements): self + { + $this->elements = $elements; + return $this; + } + + public function wrapperProps(string ...$properties): self + { + $this->wrapperProperties = $properties; + return $this; + } + + public function labelProps(string ...$properties): self + { + $this->labelProperties = $properties; + return $this; + } + + private function warningMessage(): string|PropertyInterface + { + if ($this->warningMessage === '') { + return ''; + } + + if (is_string($this->warningMessage)) { + return Element::p($this->warningMessage)->props( + 'id ' . $this->warningId + ); + } + + return $this->warningMessage->prop('id ' . $this->warningId); + } + + public function __toString(): string + { + $elements = $this->elements; + $elements[] = self::warningMessage(); + + return (string) Element::fieldset( + Element::legend($this->label)->props(...$this->labelProperties), + ...$elements + )->props(...$this->wrapperProperties); + } +} diff --git a/src/Forms/Select.php b/src/Forms/Select.php index aa04036..bd92adb 100644 --- a/src/Forms/Select.php +++ b/src/Forms/Select.php @@ -9,6 +9,7 @@ use Eightfold\HtmlBuilder\PropertyInterface; use Eightfold\HtmlBuilder\Forms\SelectType; +use Eightfold\HtmlBuilder\Forms\Fieldset; class Select implements Stringable { @@ -186,7 +187,7 @@ private function selectDropdown(): Element )->props(...$this->wrapperProperties); } - private function selectOther(): Element + private function selectOther(): Fieldset { $elements = []; $type = $this->type === SelectType::Checkbox ? 'checkbox' : 'radio'; @@ -218,13 +219,15 @@ private function selectOther(): Element $elements[] = Element::div($input, $label); } - if ($hasWarningMessage) { - $elements[] = $warningMessage; - } + return Fieldset::create( + $this->label, + $this->name, + $warningMessage + )->elements(...$elements)->labelProps(...$this->labelProperties); - return Element::fieldset( - Element::legend($this->label)->props(...$this->labelProperties), - ...$elements - ); + // return Element::fieldset( + // Element::legend($this->label)->props(...$this->labelProperties), + // ...$elements + // ); } }