Skip to content

Commit 0898d78

Browse files
committed
Add Edit/View/Delete/Group Action for TreeWidget
1 parent 3438757 commit 0898d78

File tree

2 files changed

+152
-1
lines changed

2 files changed

+152
-1
lines changed

src/Concern/HasActions.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,9 @@ protected function findTreeAction(string $name): ?Action
259259
return null;
260260
}
261261

262+
/**
263+
* Action for each record
264+
*/
262265
protected function getTreeActions(): array
263266
{
264267
return [];

src/Widgets/Tree.php

Lines changed: 149 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@
22

33
namespace SolutionForest\FilamentTree\Widgets;
44

5+
use Filament\Forms\Concerns\InteractsWithForms;
6+
use Filament\Forms\Contracts\HasForms;
57
use Filament\Widgets\Widget;
8+
use Illuminate\Database\Eloquent\Model;
9+
use SolutionForest\FilamentTree\Actions\Action;
10+
use SolutionForest\FilamentTree\Actions\DeleteAction;
11+
use SolutionForest\FilamentTree\Actions\EditAction;
12+
use SolutionForest\FilamentTree\Actions\ViewAction;
613
use SolutionForest\FilamentTree\Components\Tree as TreeComponent;
714
use SolutionForest\FilamentTree\Concern;
815
use SolutionForest\FilamentTree\Contract\HasTree;
916

10-
class Tree extends Widget implements HasTree
17+
class Tree extends Widget implements HasTree, HasForms
1118
{
1219
use Concern\InteractWithTree;
20+
use InteractsWithForms;
1321

1422
protected static string $view = 'filament-tree::widgets.tree';
1523

@@ -33,4 +41,144 @@ public function getModel(): string
3341
{
3442
return static::$model ?? class_basename(static::class);
3543
}
44+
45+
protected function getFormModel(): Model | string | null
46+
{
47+
return $this->getModel();
48+
}
49+
50+
protected function getFormSchema(): array
51+
{
52+
return [];
53+
}
54+
55+
protected function getViewFormSchema(): array
56+
{
57+
return [];
58+
}
59+
60+
protected function getEditFormSchema(): array
61+
{
62+
return [];
63+
}
64+
65+
protected function getTreeActions(): array
66+
{
67+
return array_merge(
68+
($this->hasEditAction() ? [$this->getEditAction()] : []),
69+
($this->hasViewAction() ? [$this->getViewAction()] : []),
70+
($this->hasDeleteAction() ? [$this->getDeleteAction()] : []),
71+
);
72+
}
73+
74+
protected function configureTreeAction(Action $action): void
75+
{
76+
match (true) {
77+
$action instanceof DeleteAction => $this->configureDeleteAction($action),
78+
$action instanceof EditAction => $this->configureEditAction($action),
79+
$action instanceof ViewAction => $this->configureViewAction($action),
80+
default => null,
81+
};
82+
}
83+
84+
protected function hasDeleteAction(): bool
85+
{
86+
return false;
87+
}
88+
89+
protected function hasEditAction(): bool
90+
{
91+
return false;
92+
}
93+
94+
protected function hasViewAction(): bool
95+
{
96+
return false;
97+
}
98+
99+
protected function getDeleteAction(): DeleteAction
100+
{
101+
return DeleteAction::make();
102+
}
103+
104+
protected function getEditAction(): EditAction
105+
{
106+
return EditAction::make();
107+
}
108+
109+
protected function getViewAction(): ViewAction
110+
{
111+
return ViewAction::make();
112+
}
113+
114+
protected function configureDeleteAction(DeleteAction $action): DeleteAction
115+
{
116+
$action->tree($this->getCachedTree());
117+
118+
$this->afterConfiguredDeleteAction($action);
119+
120+
return $action;
121+
}
122+
123+
protected function configureEditAction(EditAction $action): EditAction
124+
{
125+
$action->tree($this->getCachedTree());
126+
127+
$schema = $this->getEditFormSchema();
128+
129+
if (empty($schema)) {
130+
$schema = $this->getFormSchema();
131+
}
132+
133+
$action->form($schema);
134+
135+
$action->model($this->getModel());
136+
137+
$this->afterConfiguredEditAction($action);
138+
139+
return $action;
140+
}
141+
142+
protected function configureViewAction(ViewAction $action): ViewAction
143+
{
144+
$action->tree($this->getCachedTree());
145+
146+
$schema = $this->getViewFormSchema();
147+
148+
if (empty($schema)) {
149+
$schema = $this->getFormSchema();
150+
}
151+
152+
$action->form($schema);
153+
154+
$action->model($this->getModel());
155+
156+
$this->afterConfiguredViewAction($action);
157+
158+
return $action;
159+
}
160+
161+
protected function afterConfiguredDeleteAction(DeleteAction $action): DeleteAction
162+
{
163+
return $action;
164+
}
165+
166+
protected function afterConfiguredEditAction(EditAction $action): EditAction
167+
{
168+
return $action;
169+
}
170+
171+
protected function afterConfiguredViewAction(ViewAction $action): ViewAction
172+
{
173+
return $action;
174+
}
175+
176+
protected function callHook(string $hook): void
177+
{
178+
if (! method_exists($this, $hook)) {
179+
return;
180+
}
181+
182+
$this->{$hook}();
183+
}
36184
}

0 commit comments

Comments
 (0)