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 f632fe5
Show file tree
Hide file tree
Showing 3 changed files with 75 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
42 changes: 42 additions & 0 deletions tests/Unit/DialogBoxBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@ class DialogBoxBuilderTest extends TestCase
{
use ProphecyTrait;

/**
* @test
*/
public function addingContent(): void
{
$dialogBuilder = new DialogBoxBuilder();
$editableItem1 = new EditableItem('type1', 'name1');
$editableItem2 = new EditableItem('type2', 'name2');
$editableItem3 = new EditableItem('type3', 'name3');

$expected = new PanelItem('', [$editableItem1, $editableItem2, $editableItem3]);

$dialogBox = $dialogBuilder
->addContent($editableItem1, $editableItem2)
->addContent($editableItem3)
->build();

self::assertSame($expected->toArray(), $dialogBox->getItems());
}

/**
* @test
*/
Expand All @@ -34,4 +54,26 @@ public function addingTwoTabsWithSomeItems(): void

self::assertSame($expected->toArray(), $dialogBox->getItems());
}

/**
* @test
*/
public function addingContentAndThenTabs(): void
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('You cannot add tabs and content at the same time.');

(new DialogBoxBuilder())->addContent()->addTab('Test');
}

/**
* @test
*/
public function addingTabsAndThenContent(): void
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('You cannot add content and tabs at the same time.');

(new DialogBoxBuilder())->addTab('Test')->addContent();
}
}

0 comments on commit f632fe5

Please sign in to comment.