Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions src/Forms/Fieldset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
declare(strict_types=1);

namespace Eightfold\HtmlBuilder\Forms;

use Stringable;

use Eightfold\HtmlBuilder\Element;
use Eightfold\HtmlBuilder\PropertyInterface;

use Eightfold\HtmlBuilder\Forms\SelectType;

class Fieldset implements Stringable
{
// phpcs:disable
private string $warningId {
get => $this->name . '-warning';
}
// phpcs:enable

/**
* @var string[]
*/
private array $wrapperProperties = [];

/**
* @var string[]
*/
private array $labelProperties = [];

/**
* @var array<string|Stringable>
*/
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);
}
}
19 changes: 11 additions & 8 deletions src/Forms/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Eightfold\HtmlBuilder\PropertyInterface;

use Eightfold\HtmlBuilder\Forms\SelectType;
use Eightfold\HtmlBuilder\Forms\Fieldset;

class Select implements Stringable
{
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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
// );
}
}
Loading