Skip to content

Commit

Permalink
UI component class: support non-pre-defined props
Browse files Browse the repository at this point in the history
  • Loading branch information
distantnative committed Jan 22, 2025
1 parent 6ca0140 commit 76327b8
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Panel/Ui/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public function __construct(
public string|array|null $title = null,
public string $type = 'button',
public string|null $variant = null,
...$attrs
) {
$this->attrs = $attrs;
}

public function props(): array
Expand Down
2 changes: 2 additions & 0 deletions src/Panel/Ui/Buttons/ViewButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ public function __construct(
public string|array|null $title = null,
public string $type = 'button',
public string|null $variant = 'filled',
...$attrs
) {
$this->attrs = $attrs;
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/Panel/Ui/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
abstract class Component
{
protected string $key;
public array $attrs = [];

public function __construct(
public string $component,
public string|null $class = null,
public string|null $style = null
public string|null $style = null,
...$attrs
) {
$this->attrs = $attrs;
}

/**
Expand Down Expand Up @@ -70,6 +73,7 @@ public function props(): array
return [
'class' => $this->class,
'style' => $this->style,
...$this->attrs
];
}

Expand Down
18 changes: 18 additions & 0 deletions tests/Panel/Ui/ButtonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@
*/
class ButtonTest extends TestCase
{
/**
* @covers ::__construct
*/
public function testAttrs()
{
$button = new Button(
text: 'Attrs',
foo: 'bar'
);

$this->assertSame([
'foo' => 'bar',
'responsive' => true,
'text' => 'Attrs',
'type' => 'button',
], array_filter($button->props()));
}

/**
* @covers ::props
*/
Expand Down
22 changes: 22 additions & 0 deletions tests/Panel/Ui/Buttons/ViewButtonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Kirby\Cms\Page;
use Kirby\Panel\Areas\AreaTestCase;

use function PHPSTORM_META\map;

/**
* @coversDefaultClass \Kirby\Panel\Ui\Buttons\ViewButton
* @covers ::__construct
Expand All @@ -19,6 +21,26 @@ public function setUp(): void
$this->login();
}

/**
* @covers ::__construct
*/
public function testAttrs()
{
$button = new ViewButton(
text: 'Attrs',
foo: 'bar'
);

$this->assertSame([
'foo' => 'bar',
'responsive' => true,
'size' => 'sm',
'text' => 'Attrs',
'type' => 'button',
'variant' => 'filled'
], array_filter($button->props()));
}

/**
* @covers ::factory
*/
Expand Down
20 changes: 20 additions & 0 deletions tests/Panel/Ui/ComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@ class UiComponent extends Component
*/
class ComponentTest extends TestCase
{
/**
* @covers ::__contruct
*/
public function testAttrs()
{
$props = [
'component' => 'k-text',
'class' => 'k-test',
'foo' => 'bar'
];

$component = new UiComponent(...$props);

$this->assertSame([
'class' => 'k-test',
'style' => null,
'foo' => 'bar',
], $component->props());
}

/**
* @covers ::__call
*/
Expand Down

0 comments on commit 76327b8

Please sign in to comment.