Skip to content

Commit

Permalink
Merge pull request #6929 from getkirby/v5/refactor/ui-component-attrs-2
Browse files Browse the repository at this point in the history
UI component class: support non-pre-defined props
  • Loading branch information
bastianallgeier authored Jan 23, 2025
2 parents 2cdf0b3 + 61c04d8 commit c24b672
Show file tree
Hide file tree
Showing 6 changed files with 67 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
20 changes: 20 additions & 0 deletions tests/Panel/Ui/Buttons/ViewButtonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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 ::__construct
*/
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 c24b672

Please sign in to comment.