Skip to content

Commit

Permalink
Add option to set render condition callback on action (#597)
Browse files Browse the repository at this point in the history
  • Loading branch information
jankonas authored and paveljanda committed Jan 10, 2018
1 parent 88d4bcb commit 50e5b81
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 29 deletions.
5 changes: 5 additions & 0 deletions src/Column/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Action extends Column
use Traits\TButtonTryAddIcon;
use Traits\TButtonText;
use Traits\TLink;
use Traits\TRenderCondition;

/**
* @var string
Expand Down Expand Up @@ -109,6 +110,10 @@ public function __construct(DataGrid $grid, $href, $name, $params)
*/
public function render(Row $row)
{
if (!$this->shouldBeRendered($row)) {
return null;
}

try {
// Renderer function may be used
return $this->useRenderer($row);
Expand Down
30 changes: 1 addition & 29 deletions src/Column/ItemDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ItemDetail
use Traits\TButtonClass;
use Traits\TButtonTitle;
use Traits\TButtonText;
use Traits\TRenderCondition;

/**
* (renderer | template | block)
Expand Down Expand Up @@ -60,11 +61,6 @@ class ItemDetail
*/
protected $template_parameters = [];

/**
* @var callable
*/
protected $render_condition_callback;


/**
* @param DataGrid $grid
Expand Down Expand Up @@ -239,28 +235,4 @@ public function getTemplateVariables()
{
return $this->template_parameters;
}


/**
* @param callable $condition
* @return static
*/
public function setRenderCondition(callable $condition)
{
$this->render_condition_callback = $condition;

return $this;
}


/**
* @param Row $row
* @return bool
*/
public function shouldBeRendered(Row $row)
{
$condition = $this->render_condition_callback;

return $condition ? $condition($row->getItem()) : true;
}
}
45 changes: 45 additions & 0 deletions src/Traits/TRenderCondition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* @copyright Copyright (c) 2015 ublaboo <ublaboo@paveljanda.com>
* @author Pavel Janda <me@paveljanda.com>
* @package Ublaboo
*/

namespace Ublaboo\DataGrid\Traits;

use Ublaboo\DataGrid\Row;

trait TRenderCondition
{

/**
* @var callable|null
*/
protected $render_condition_callback;


/**
* @param callable $condition
* @return static
*/
public function setRenderCondition(callable $condition)
{
$this->render_condition_callback = $condition;

return $this;
}


/**
* @param Row $row
* @return bool
*/
public function shouldBeRendered(Row $row)
{
$condition = $this->render_condition_callback;

return $condition ? $condition($row->getItem()) : true;
}

}
16 changes: 16 additions & 0 deletions tests/Cases/ColumnActionTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,22 @@ final class ColumnActionTest extends TestCase
);
}


public function testActionRenderCondition()
{
$action = $this->grid->addAction('action1', 'Do', 'doStuff!')->setRenderCondition(function () {
return true;
});

Assert::same('<a href="doStuff!?id=1" class="btn btn-xs btn-default">Do</a>', $this->render($action));

$action = $this->grid->addAction('action2', 'Do', 'doStuff!')->setRenderCondition(function () {
return false;
});

Assert::same('', $this->render($action));
}

}


Expand Down

0 comments on commit 50e5b81

Please sign in to comment.