Skip to content

Commit

Permalink
Create default template for action column automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Dec 5, 2023
1 parent 601909c commit 933cd80
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/Column/ActionColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class ActionColumn implements ColumnInterface
* @param ?callable $urlCreator A callback that creates a button URL using the specified data information.
*
* @psalm-param UrlCreator|null $urlCreator
* @psalm-param array<string,ButtonRenderer> $buttons
* @psalm-param array<string,ButtonRenderer>|null $buttons
* @psalm-param array<string,bool|Closure>|null $visibleButtons
*/
public function __construct(
Expand All @@ -37,7 +37,7 @@ public function __construct(
public readonly ?string $header = null,
public readonly ?string $footer = null,
public readonly mixed $content = null,
public readonly array $buttons = [],
public readonly ?array $buttons = null,
public readonly ?array $visibleButtons = null,
public readonly array $columnAttributes = [],
public readonly array $headerAttributes = [],
Expand Down
23 changes: 20 additions & 3 deletions src/Column/ActionColumnRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class ActionColumnRenderer implements ColumnRendererInterface
*/
public function __construct(
?callable $defaultUrlCreator = null,
private readonly string $defaultTemplate = "{view}\n{update}\n{delete}",
private readonly ?string $defaultTemplate = null,
?array $defaultButtons = null,
) {
$this->defaultUrlCreator = $defaultUrlCreator;
Expand Down Expand Up @@ -100,7 +100,7 @@ public function renderBody(ColumnInterface $column, Cell $cell, DataContext $con
if ($contentSource !== null) {
$content = (string)(is_callable($contentSource) ? $contentSource($context->data, $context) : $contentSource);
} else {
$buttons = empty($column->buttons) ? $this->defaultButtons : $column->buttons;
$buttons = $column->buttons ?? $this->defaultButtons;
$content = preg_replace_callback(
'/{([\w\-\/]+)}/',
function (array $matches) use ($column, $buttons, $context): string {
Expand All @@ -122,7 +122,7 @@ function (array $matches) use ($column, $buttons, $context): string {

return '';
},
$column->template ?? $this->defaultTemplate
$this->getTemplate($column, $buttons),
);
$content = trim($content);
}
Expand Down Expand Up @@ -179,6 +179,23 @@ private function isVisibleButton(
return $visibleValue($data, $key, $index);
}

/**
* @psalm-param array<string,ButtonRenderer> $buttons
*/
private function getTemplate(ActionColumn $column, array $buttons): string
{
if ($column->template !== null) {
return $column->template;
}

$tokens = [];
foreach ($buttons as $name => $_renderer) {
$tokens[] = '{' . $name . '}';
}

return implode("\n", $tokens);
}

/**
* @psalm-assert ActionColumn $column
*/
Expand Down
57 changes: 51 additions & 6 deletions tests/Column/ActionColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,6 @@ public function testContentAttributes(): void
);
}

/**
* @throws InvalidConfigException
* @throws NotFoundException
* @throws NotInstantiableException
* @throws CircularReferenceException
*/
public function testCustomButton(): void
{
Assert::equalsWithoutLE(
Expand Down Expand Up @@ -732,4 +726,55 @@ public function testObjectsWithPrimaryKey(): void
$html
);
}

public function testDefaultTemplateGeneration(): void
{
$dataReader = new IterableDataReader([
['id' => 1],
['id' => 2],
]);

$actionColumn = new ActionColumn(
primaryKey: 'id',
urlCreator: static fn() => '#',
buttons: [
'one' => static fn(string $url) => Html::a('1', $url)->render(),
'two' => static fn(string $url) => Html::a('2', $url)->render(),
]
);

$html = GridView::widget()
->columns($actionColumn)
->dataReader($dataReader)
->render();

$this->assertSame(
<<<HTML
<div>
<table>
<thead>
<tr>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<a href="#">1</a>
<a href="#">2</a>
</td>
</tr>
<tr>
<td>
<a href="#">1</a>
<a href="#">2</a>
</td>
</tr>
</tbody>
</table>
</div>
HTML,
$html
);
}
}

0 comments on commit 933cd80

Please sign in to comment.