Skip to content

Commit ee64c5e

Browse files
committed
UP
1 parent d3b3a4f commit ee64c5e

File tree

5 files changed

+205
-98
lines changed

5 files changed

+205
-98
lines changed

src/Adapters/DefaultKanbanAdapter.php

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
namespace Relaticle\Flowforge\Adapters;
66

77
use DB;
8+
use Filament\Forms\Components\DatePicker;
9+
use Filament\Forms\Components\Hidden;
10+
use Filament\Forms\Components\Section;
11+
use Filament\Forms\Components\Select;
12+
use Filament\Forms\Components\Textarea;
13+
use Filament\Forms\Components\TextInput;
14+
use Filament\Forms\Form;
815
use Illuminate\Database\Eloquent\Model;
916
use Illuminate\Support\Collection;
1017
use Illuminate\Support\Str;
@@ -62,6 +69,14 @@ class DefaultKanbanAdapter implements IKanbanAdapter, Wireable
6269
*/
6370
protected string $modelClass;
6471

72+
73+
/**
74+
* The create form callable for the model.
75+
*
76+
* @var callable|null
77+
*/
78+
protected mixed $createFormCallable = null;
79+
6580
/**
6681
* The singular label for the model.
6782
*
@@ -97,6 +112,7 @@ public function __construct(
97112
?string $descriptionAttribute = null,
98113
array $cardAttributes = [],
99114
?string $orderField = null,
115+
?callable $createFormCallable = null,
100116
?string $recordLabel = null,
101117
?string $pluralRecordLabel = null
102118
)
@@ -109,6 +125,8 @@ public function __construct(
109125
$this->cardAttributes = $cardAttributes;
110126
$this->orderField = $orderField;
111127

128+
$this->createFormCallable = $createFormCallable;
129+
112130
// Set model labels with defaults
113131
$this->recordLabel = $recordLabel ?? Str::singular(class_basename($modelClass));
114132
$this->pluralRecordLabel = $pluralRecordLabel ?? Str::singular(class_basename($modelClass));
@@ -307,6 +325,116 @@ public function updateColumnCards(string|int $columnId, array $cards): bool
307325
});
308326
}
309327

328+
/**
329+
* Get the form class for creating cards.
330+
*
331+
* @param Form $form
332+
* @return Form
333+
*/
334+
public function getEditForm(Form $form): Form
335+
{
336+
return $form
337+
->statePath('editFormData')
338+
->schema([
339+
Select::make($this->getStatusField())
340+
->label(__('Status'))
341+
->options($this->getStatusValues())
342+
->required(),
343+
344+
TextInput::make('title')
345+
->label(__('Title'))
346+
->required()
347+
->maxLength(255)
348+
->placeholder(__('Enter :recordLabel title', ['recordLabel' => strtolower($this->config['recordLabel'] ?? 'card')]))
349+
->columnSpanFull(),
350+
351+
Textarea::make('description')
352+
->label(__('Description'))
353+
->placeholder(__('Enter :recordLabel description', ['recordLabel' => strtolower($this->config['recordLabel'] ?? 'card')]))
354+
->columnSpanFull(),
355+
356+
$this->getCardAttributesFields(),
357+
]);
358+
}
359+
360+
/**
361+
* Get the form class for creating cards.
362+
*
363+
* @param Form $form
364+
* @param mixed $activeColumn
365+
* @return Form
366+
*/
367+
public function getCreateForm(Form $form, mixed $activeColumn): Form {
368+
if($this->createFormCallable) {
369+
return call_user_func($this->createFormCallable, $form, $activeColumn);
370+
}
371+
372+
return $form
373+
->statePath('createFormData')
374+
->schema([
375+
Hidden::make($this->getStatusField())
376+
->default(fn () => $activeColumn),
377+
378+
TextInput::make('title')
379+
->label(__('Title'))
380+
->required()
381+
->maxLength(255)
382+
->placeholder(__('Enter :recordLabel title', ['recordLabel' => strtolower($this->config['recordLabel'] ?? 'card')]))
383+
->columnSpanFull(),
384+
385+
Textarea::make('description')
386+
->label(__('Description'))
387+
->placeholder(__('Enter :recordLabel description', ['recordLabel' => strtolower($this->config['recordLabel'] ?? 'card')]))
388+
->columnSpanFull(),
389+
390+
$this->getCardAttributesFields(),
391+
]);
392+
}
393+
394+
/**
395+
* Generate form fields for card attributes.
396+
*
397+
* @return Section|null
398+
*/
399+
protected function getCardAttributesFields(): ?Section
400+
{
401+
$cardAttributes = $this->getCardAttributes();
402+
403+
if (empty($cardAttributes)) {
404+
return null;
405+
}
406+
407+
$fields = [];
408+
409+
foreach ($cardAttributes as $attribute => $label) {
410+
// Determine field type based on attribute name
411+
if (str_contains($attribute, 'date')) {
412+
$fields[] = DatePicker::make($attribute)
413+
->label($label);
414+
} elseif (str_contains($attribute, 'priority')) {
415+
$fields[] = Select::make($attribute)
416+
->label($label)
417+
->options([
418+
'Low' => __('Low'),
419+
'Medium' => __('Medium'),
420+
'High' => __('High'),
421+
]);
422+
} else {
423+
$fields[] = TextInput::make($attribute)
424+
->label($label)
425+
->maxLength(255);
426+
}
427+
}
428+
429+
if (empty($fields)) {
430+
return null;
431+
}
432+
433+
return Section::make(__('Additional Details'))
434+
->schema($fields)
435+
->columns(2);
436+
}
437+
310438
/**
311439
* Create a new card with the given attributes.
312440
*
@@ -411,6 +539,7 @@ public function toLivewire(): array
411539
'descriptionAttribute' => $this->getDescriptionAttribute(),
412540
'cardAttributes' => $this->getCardAttributes(),
413541
'orderField' => $this->getOrderField(),
542+
'createFormCallable' => $this->createFormCallable,
414543
'recordLabel' => $this->getRecordLabel(),
415544
'pluralRecordLabel' => $this->getPluralRecordLabel(),
416545
];
@@ -432,6 +561,7 @@ public static function fromLivewire($value)
432561
$value['descriptionAttribute'] ?? null,
433562
$value['cardAttributes'] ?? [],
434563
$value['orderField'] ?? null,
564+
$value['createFormCallable'] ?? null,
435565
$value['recordLabel'] ?? null,
436566
$value['pluralRecordLabel'] ?? null
437567
);

src/Contracts/IKanbanAdapter.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Relaticle\Flowforge\Contracts;
44

5+
use Filament\Forms\Form;
56
use Illuminate\Database\Eloquent\Model;
67
use Illuminate\Support\Collection;
78

@@ -56,7 +57,7 @@ public function getItems(): Collection;
5657
* @return Collection
5758
*/
5859
public function getItemsForStatus(string $status, int $limit = 10): Collection;
59-
60+
6061
/**
6162
* Get the total count of items for a specific status.
6263
*
@@ -95,6 +96,24 @@ public function getTitleAttribute(): string;
9596
*/
9697
public function getDescriptionAttribute(): ?string;
9798

99+
100+
/**
101+
* Get the form class for creating cards.
102+
*
103+
* @param Form $form
104+
* @return Form
105+
*/
106+
public function getCreateForm(Form $form, mixed $activeColumn): Form;
107+
108+
/**
109+
* Get the form class for creating cards.
110+
*
111+
* @param Form $form
112+
* @return Form
113+
*/
114+
public function getEditForm(Form $form): Form;
115+
116+
98117
/**
99118
* Create a new card with the given attributes.
100119
*
@@ -103,6 +122,7 @@ public function getDescriptionAttribute(): ?string;
103122
*/
104123
public function createCard(array $attributes): ?Model;
105124

125+
106126
/**
107127
* Update an existing card with the given attributes.
108128
*

src/Filament/Resources/Pages/KanbanBoard.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class KanbanBoard extends Page
4040
*/
4141
protected ?string $orderField = null;
4242

43+
/**
44+
* @var callable|null
45+
*/
46+
protected mixed $createFormCallback = null;
47+
4348
/**
4449
* @var IKanbanAdapter|null
4550
*/
@@ -143,6 +148,13 @@ public function orderField(string $field): static
143148
return $this;
144149
}
145150

151+
public function createForm(callable $callback): static
152+
{
153+
$this->createFormCallback = $callback;
154+
155+
return $this;
156+
}
157+
146158
/**
147159
* Set a custom adapter for the Kanban board.
148160
*
@@ -226,6 +238,10 @@ public function getAdapter(): IKanbanAdapter
226238
$instance->setKanbanOrderField($this->orderField);
227239
}
228240

241+
if(method_exists($instance, 'setKanbanCreateFormCallback') && $this->createFormCallback) {
242+
$instance->setKanbanCreateFormCallback($this->createFormCallback);
243+
}
244+
229245
if (method_exists($instance, 'setKanbanRecordLabel') && $this->recordLabel) {
230246
$instance->setKanbanRecordLabel($this->recordLabel);
231247
}

0 commit comments

Comments
 (0)