Skip to content

Commit

Permalink
Allow adding content directly (without tabs)
Browse files Browse the repository at this point in the history
Note: You can only add either content or tabs
  • Loading branch information
jdreesen committed Sep 27, 2024
1 parent 47fed54 commit 5faf20e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/DialogBoxBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ class DialogBoxBuilder
{
private EditableDialogBoxConfiguration $config;
private TabPanelItem $tabs;
private PanelItem $content;

public function __construct()
{
$this->config = new EditableDialogBoxConfiguration();
$this->tabs = new TabPanelItem();
}

/**
Expand Down Expand Up @@ -55,11 +55,34 @@ public function height(int $height): static
return $this;
}

/**
* @return $this
*/
public function addContent(EditableItem ...$items): static
{
if (isset($this->tabs)) {
throw new \LogicException('You cannot add content and tabs at the same time.');
}

$this->content ??= new PanelItem('', []);

foreach ($items as $item) {
$this->content->addItem($item);
}

return $this;
}

/**
* @return $this
*/
public function addTab(string $title, EditableItem ...$items): static
{
if (isset($this->content)) {
throw new \LogicException('You cannot add tabs and content at the same time.');
}

$this->tabs ??= new TabPanelItem();
$this->tabs->addTab(new PanelItem($title, array_values($items)));

return $this;
Expand Down Expand Up @@ -105,8 +128,10 @@ public function createLink(string $name): LinkItem

public function build(): EditableDialogBoxConfiguration
{
if (!$this->tabs->isEmpty()) {
$this->config->setItems($this->tabs->toArray());
$items = $this->content ?? $this->tabs ?? null;

if ($items && !$items->isEmpty()) {
$this->config->setItems($items->toArray());
}

return $this->config;
Expand Down
5 changes: 5 additions & 0 deletions src/EditableDialogBox/LayoutItem/PanelItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public function __construct(string $title, array $items)
$this->title = $title;
}

public function addItem(DialogBoxItem $item): static
{
return parent::addItem($item);
}

protected function getAttributes(): array
{
return ['title' => $this->title] + parent::getAttributes();
Expand Down

0 comments on commit 5faf20e

Please sign in to comment.