Skip to content

Commit

Permalink
add callback to customize row html attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrombez committed Nov 16, 2023
1 parent e09efc4 commit a32095a
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 18 deletions.
46 changes: 46 additions & 0 deletions src/Grid/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Grid
private array $columns;
private array $batchActions;
private string $theme;
private $rowAttributesCallback = null;

private PaginationInterface $pagination;

Expand Down Expand Up @@ -51,4 +52,49 @@ public function getTheme(): string
{
return $this->theme;
}

public function getRowAttributes($item, bool $keepAsArray = false): null|array|string
{
if (!is_callable($this->rowAttributesCallback)) {
return null;
}

$callback = $this->rowAttributesCallback;
$attributes = $callback($item);

if (!is_array($attributes)) {
return null;
}

if ($keepAsArray) {
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),
};
},
''
);
}
}
50 changes: 32 additions & 18 deletions src/Grid/GridBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class GridBuilder
private ?Request $request;
private ?FormInterface $filtersForm;
private ?int $itemsPerPage;
private $rowAttributesCallback = null;

/**
* @var array|Column[]
Expand Down Expand Up @@ -177,24 +178,6 @@ private function applyFilters()
}
}

public function getGrid(): Grid
{
if ($this->grid === null) {
$this->applySort();
$this->applyFilters();

$pagination = $this->paginator->paginate(
$this->queryBuilder->getQuery(),
$this->request->query->getInt('page', 1),
$this->itemsPerPage
);

$this->grid = new Grid($this->columns, $pagination, $this->theme, $this->batchActions);
}

return $this->grid;
}

public function addBatchAction(string $id, string $label, string $url): self
{
$this->batchActions[] = [
Expand All @@ -212,4 +195,35 @@ public function setItemsPerPage(?int $itemsPerPage): self

return $this;
}

public function setRowAttributesCallback(callable $callback): self
{
$this->rowAttributesCallback = $callback;

return $this;
}

public function getGrid(): Grid
{
if ($this->grid === null) {
$this->applySort();
$this->applyFilters();

$pagination = $this->paginator->paginate(
$this->queryBuilder->getQuery(),
$this->request->query->getInt('page', 1),
$this->itemsPerPage
);

$this->grid = new Grid(
$this->columns,
$pagination,
$this->theme,
$this->batchActions,
$this->rowAttributesCallback
);
}

return $this->grid;
}
}

0 comments on commit a32095a

Please sign in to comment.