Skip to content

Commit

Permalink
add "attr" parameter for the actions template
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrombez committed Nov 23, 2023
1 parent 33aa7bd commit 0bdd312
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 28 deletions.
30 changes: 3 additions & 27 deletions src/Grid/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kibatic\DatagridBundle\Grid;

use Kibatic\DatagridBundle\Twig\HtmlExtension;
use Knp\Component\Pager\Pagination\PaginationInterface;

class Grid
Expand All @@ -21,7 +22,7 @@ public function __construct(
PaginationInterface $pagination,
string $theme,
array $batchActions = [],
?callable $rowAttributesCallback = null
?callable $rowAttributesCallback = null,
) {
$this->columns = $columns;
$this->pagination = $pagination;
Expand Down Expand Up @@ -72,31 +73,6 @@ public function getRowAttributes($item, bool $keepAsArray = false): null|array|s
return $attributes;
}

return self::attributesToHtml($attributes);
}

private static function attributesToHtml(array $attributes): string
{
return array_reduce(
array_keys($attributes),
function (string $carry, string $key) use ($attributes) {
$value = $attributes[$key];

if (!\is_scalar($value) && null !== $value) {
throw new \LogicException(sprintf('A "%s" prop was passed when creating the component. No matching "%s" property or mount() argument was found, so we attempted to use this as an HTML attribute. But, the value is not a scalar (it\'s a %s). Did you mean to pass this to your component or is there a typo on its name?', $key, $key, get_debug_type($value)));
}

if (null === $value) {
throw new \Exception('Passing "null" as an attribute value is forbidden');
}

return match ($value) {
true => "{$carry} {$key}",
false => $carry,
default => sprintf('%s %s="%s"', $carry, $key, $value),
};
},
''
);
return HtmlExtension::attributesToHtml($attributes);
}
}
7 changes: 6 additions & 1 deletion src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ services:
autoconfigure: true

Kibatic\DatagridBundle\Grid\GridBuilder:
public: true
public: true

Kibatic\DatagridBundle\Twig\HtmlExtension:
public: true
tags:
- { name: twig.extension }
41 changes: 41 additions & 0 deletions src/Twig/HtmlExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Kibatic\DatagridBundle\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class HtmlExtension extends AbstractExtension
{
public function getFilters(): array
{
return [
new TwigFilter('inline_attr', $this->attributesToHtml(...), ['is_safe' => ['html']]),
];
}

public static function attributesToHtml(array $attributes): string
{
return array_reduce(
array_keys($attributes),
function (string $carry, string $key) use ($attributes) {
$value = $attributes[$key];

if (!\is_scalar($value) && null !== $value) {
throw new \LogicException(sprintf('A "%s" prop was passed when creating the component. No matching "%s" property or mount() argument was found, so we attempted to use this as an HTML attribute. But, the value is not a scalar (it\'s a %s). Did you mean to pass this to your component or is there a typo on its name?', $key, $key, get_debug_type($value)));
}

if (null === $value) {
throw new \Exception('Passing "null" as an attribute value is forbidden');
}

return match ($value) {
true => "{$carry} {$key}",
false => $carry,
default => sprintf('%s %s="%s"', $carry, $key, $value),
};
},
''
);
}
}

0 comments on commit 0bdd312

Please sign in to comment.