Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow adding content directly (without tabs) #22

Merged
merged 1 commit into from
Sep 27, 2024
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
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();
}
}
Loading