filament-kanban

repository·main·Indexed 19 days ago

https://github.com/mokhosh/filament-kanban

A Filament plugin for adding interactive Kanban boards to Filament panels. It supports drag-and-drop reordering, status updates, and customizable edit modals. Developers can scaffold boards using the `make:kanban` command and configure them by extending the `KanbanBoard` class, specifying the Eloquent model and a status Enum (optionally using the `IsKanbanStatus` trait). The plugin allows for custom record retrieval, drag-and-drop logic, and view overrides.

Tokens
2.8K
Snippets
12
Records
12
Agent score
65%

What's inside filament-kanban

  1. How to use the IsKanbanStatus trait in Enums

    main

    It is recommended to use a string-backed Enum for statuses. By using the IsKanbanStatus trait, you can easily transform your enum cases for the Kanban board using the statuses() method.

    use Mokhoshilament-kanban\
    Concerns\IsKanbanStatus;
    
    enum UserStatus: string
    {
        use IsKanbanStatus;
    
        case User = 'User';
        case Admin = 'Admin';
    }

    Customizing Enum behavior:

    • Filter cases: Override kanbanCases(): array to return a subset of cases to show on the board.
    • Custom titles: Override getTitle(): string to change how the status title is retrieved (e.g., using translations).
    • Note: The trait uses the enum value for the status id and the title by default.
    use Mokhosh\FilamentKanban\Concerns\IsKanbanStatus;
    
    enum UserStatus: string
    {
        use IsKanbanStatus;
    
        case User = 'User';
        case Admin = 'Admin';
    }
  2. Configure basic Kanban board properties

    main

    To make a Kanban board functional, you must override the $model and $statusEnum properties in your generated class.

    • $model: The Eloquent model class used to load records.
    • $statusEnum: The string-backed Enum class defining the board's statuses.
    protected static string $model = User::class;
    protected static string $statusEnum = UserStatus::class;
  3. Implement a Kanban board by extending KanbanBoard

    main

    To create a kanban board in Filament, you must create a new class that extends Mokhosh\FilamentKanban\Pages\KanbanBoard. You are required to define the model being used and the Enum that represents the statuses. You can also customize which attributes are used for the record title and the status field.

    Required properties to define in your class:

    • static string $model: The Eloquent model class name.
    • static string $statusEnum: The Enum class name used for statuses.

    Optional properties:

    • static string $recordTitleAttribute: The attribute on the model used for the title (defaults to 'title').
    • static string $recordStatusAttribute: The attribute on the model used for the status (defaults to 'status').
    namespace App\Filament\Pages;
    
    use App\Models\Task;
    use App\Enums\TaskStatus;
    use Mokhosh\FilamentKanban\Pages\KanbanBoard;
    
    class TaskKanbanBoard extends KanbanBoard
    {
        protected static string $model = Task::class;
        protected static string $statusEnum = TaskStatus::class;
    
        // Optional customizations
        protected static string $recordTitleAttribute = 'name';
        protected static string $recordStatusAttribute = 'state';
    }
  4. Register the Filament Kanban plugin in a Panel

    main

    To use Kanban boards in your Filament application, you must register the FilamentKanbanPlugin within your Filament Panel provider using the plugin() method. You can instantiate the plugin using the static make() method.

    use Mokhosh\FilamentKanban\FilamentKanbanPlugin;
    use Filament\Panel;
    
    public function panel(Panel $panel): Panel
    {
        return $panel
            // ...
            ->plugins([
                FilamentKanbanPlugin::make(),
            ]);
    }
  5. Customize Kanban board attributes and views

    main

    Model Attribute Mapping

    If your database columns differ from the defaults, override these properties:

    • $recordTitleAttribute: The column used for the card title (default: 'title').
    • $recordStatusAttribute: The column used for the status (default: 'status').
    • $navigationIcon: The Heroicon used in the Filament navigation.

    Customizing Views

    To change the look of the board, you can either publish all views globally or override specific views per board class:

    Global Publish:

    php artisan vendor:publish --tag="filament-kanban-views"

    Per-Board Overrides:

    • $view: The main board view.
    • $headerView: The header view.
    • $recordView: The individual card view.
    • $statusView: The column/status view.
    • $scriptsView: The scripts view.
    protected static string $recordTitleAttribute = 'title';
    protected static string $recordStatusAttribute = 'status';
    protected static ?string $navigationIcon = 'heroicon-o-document-text';
    
    protected static string $view = 'filament-kanban::kanban-board';
    protected static string $recordView = 'filament-kanban::kanban-record';
  6. Configure the Edit Modal

    main

    The edit modal is enabled by default. You can customize its behavior, appearance, and logic.

    Disabling the Modal

    Set $disableEditModal to true to prevent records from opening an edit view when clicked.

    Form Schema

    Override getEditModalFormSchema(int|string|null $recordId) to define the Filament form fields used in the modal.

    Submit Action

    Override editRecord(int|string $recordId, array $data, array $state) to define how the form data is saved to the model. $data contains the form inputs, and $state contains the full record data.

    Appearance

    Use the following properties to customize the modal:

    • $editModalTitle: The title of the modal.
    • $editModalWidth: The width (e.g., '2xl').
    • $editModalSaveButtonLabel: Label for the save button.
    • $editModalCancelButtonLabel: Label for the cancel button.
    • $editModalSlideOver: Set to true to use a slide-over instead of a centered modal.
    public bool $disableEditModal = false;
    
    protected function getEditModalFormSchema(int|string|null $recordId): array
    {
        return [
            TextInput::make('title'),
        ];
    }
    
    protected function editRecord(int|string $recordId, array $data, array $state): void
    {
        User::find($recordId)->update([
            'phone' => $data['phone']
        ]);
    }
    
    protected string $editModalTitle = 'Edit Record';
    protected string $editModalWidth = '2xl';
    protected bool $editModalSlideOver = true;
  7. Customize record retrieval and drag-and-drop behavior

    main

    You can override several methods to control how data is fetched and how the board reacts to user interactions.

    Data Retrieval

    • records(): Override this to return a Collection of records. This allows for custom filtering (e.g., only showing active users).
    • statuses(): If you are not using an Enum, override this to return a Collection of arrays containing id and title.

    Drag and Drop Logic

    • onStatusChanged(int|string $recordId, string $status, array $fromOrderedIds, array $toOrderedIds): Define what happens when a record is moved to a different status column.
    • onSortChanged(int|string $recordId, string $status, array $orderedIds): Define what happens when a record is reordered within the same status column.
    protected function records(): Collection
    {
        return User::where('role', 'admin')->get();
    }
    
    public function onStatusChanged(int|string $recordId, string $status, array $fromOrderedIds, array $toOrderedIds): void
    {
        User::find($recordId)->update(['status' => $status]);
        User::setNewOrder($toOrderedIds);
    }
    
    public function onSortChanged(int|string $recordId, string $status, array $orderedIds): void
    {
        User::setNewOrder($orderedIds);
    }
  8. Retrieve the Filament Kanban plugin instance

    main

    If you need to access the plugin instance at runtime, you can use the get() method. This method uses the Filament helper function to retrieve the plugin registered under the ID filament-kanban.

    use Mokhosh\FilamentKanban\FilamentKanbanPlugin;
    
    $plugin = FilamentKanbanPlugin::get();
  9. Configure Kanban board model and status attributes

    main

    When extending KanbanBoard, you can control how the board interacts with your Eloquent models using these static properties:

    PropertyTypeDefaultDescription
    $modelstringRequiredThe fully qualified class name of the Eloquent model.
    $statusEnumstringRequiredThe fully qualified class name of the Enum used for statuses. This Enum must implement a statuses() method returning a collection of status arrays.
    $recordTitleAttributestring'title'The database column/attribute on the model used to display the record's title.
    $recordStatusAttributestring'status'The database column/attribute on the model that stores the status value.
    protected static string $model = Task::class;
    protected static string $statusEnum = TaskStatus::class;
    protected static string $recordTitleAttribute = 'title';
    protected static string $recordStatusAttribute = 'status';