Skip to content

Commit

Permalink
[6.x] Fix Button::can method (#1673)
Browse files Browse the repository at this point in the history
* Fix Button::can method

* phpstan fixes

* Allow Button::can type bool

* Allow Button::can type bool
  • Loading branch information
luanfreitasdev authored Sep 8, 2024
1 parent 90d667d commit 25cbe18
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 24 deletions.
2 changes: 1 addition & 1 deletion dist/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"/powergrid.js": "/powergrid.js?id=05c5f62ec274e8fcf8d83856987538fe",
"/powergrid.js": "/powergrid.js?id=434af38e193e4b54d647faede6b8b74f",
"/bootstrap5.css": "/bootstrap5.css?id=a27af22343149104b2aa3283d8fd502b",
"/tailwind.css": "/tailwind.css?id=924477e2afcb2cb56aa392e266ee56ca"
}
2 changes: 1 addition & 1 deletion dist/powergrid.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions resources/js/components/pg-render-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ export default (params) => ({
},

shouldHideAction(action) {

if (action.can === false) {
return true;
}

let hideAction = false;
if (action.rules && Object.values(action.rules).length > 0) {
Object.values(action.rules).forEach((ruleObj) => {
Expand Down
2 changes: 1 addition & 1 deletion src/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ final class Button implements Wireable

public ?\Closure $hideWhen = null;

public ?\Closure $can = null;
public bool | \Closure $can = true;

public function __construct(public string $action)
{
Expand Down
20 changes: 10 additions & 10 deletions src/Concerns/HasActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,16 @@ public function storeActionsHeaderInJSWindow(): void
}

$actionsHtml = collect($this->header())
->transform(function (Button $action) {
return [
'slot' => $action->slot,
'tag' => $action->tag,
'icon' => $action->icon,
'iconAttributes' => $action->iconAttributes,
'attributes' => $action->attributes,
'rules' => [],
];
});
->transform(function (Button $action) {
return [
'slot' => $action->slot,
'tag' => $action->tag,
'icon' => $action->icon,
'iconAttributes' => $action->iconAttributes,
'attributes' => $action->attributes,
'rules' => [],
];
});

$actionsHtml = json_encode($actionsHtml);

Expand Down
28 changes: 18 additions & 10 deletions src/DataSource/Processors/DataSourceBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,24 @@ private static function processRows(BaseCollection $results, PowerGridComponent
if ($renderActions && !$hasCookieActionsForRow) {
try {
$actions = collect($component->actions((object) $row)) // @phpstan-ignore-line
->transform(function (Button|array $action) use ($row, $component) {
return [
'slot' => data_get($action, 'slot'),
'tag' => data_get($action, 'tag'),
'icon' => data_get($action, 'icon'),
'iconAttributes' => data_get($action, 'iconAttributes'),
'attributes' => data_get($action, 'attributes'),
'rules' => $component->resolveActionRules($row),
];
});
->transform(function (Button|array $action) use ($row, $component) {
/** @var bool|\Closure $can */
$can = data_get($action, 'can');

if ($can instanceof \Closure) {
$allowed = $can($row);
}

return [
'can' => $allowed ?? $can,
'slot' => data_get($action, 'slot'),
'tag' => data_get($action, 'tag'),
'icon' => data_get($action, 'icon'),
'iconAttributes' => data_get($action, 'iconAttributes'),
'attributes' => data_get($action, 'attributes'),
'rules' => $component->resolveActionRules($row),
];
});

static::$actionsHtml[$rowId] = $actions->toArray();
} catch (\ArgumentCountError $exception) {
Expand Down
2 changes: 1 addition & 1 deletion src/Providers/Macros.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public static function actions(): void
return $this;
});

Button::macro('can', function (\Closure $closure) {
Button::macro('can', function (bool|\Closure $closure) {
$this->can = $closure;

return $this;
Expand Down

0 comments on commit 25cbe18

Please sign in to comment.