Giter VIP home page Giter VIP logo

nova-action-buttons's Introduction

Nova Action Buttons

Latest Version on Packagist Licence Total Downloads

This Laravel Nova package allows you to execute an action directly on your resource table view.

Requirements

  • php: >=8.0
  • laravel/nova: ^4.1

Installation

Install the package in a Laravel Nova project via Composer:

composer require pavloniym/nova-action-buttons

Usage

Single button

Nova Action Buttons
You can add single button to execute action from index row

use Pavloniym\ActionButtons\ActionButton;

public function fields(Request $request)
{
    return [
      
        // ... Nova default fields
      
        ActionButton::make('') // Name in resource table column
            ->icon('<svg></svg>') // Svg icon (optional)
            ->title('Refresh') // Title (optional)
            ->styles([]) // Custom css styles (optional)
            ->classes([]) // Custom css classes (optional)
            ->action(new RefreshAction, $this->resource->id) // Provide action instance and resource id
            ->asToolbarButton(), // Display as row toolbar button (optional)
      
        // ... Nova default fields
    ];
}

Collection of buttons

Nova Action Buttons
You can add collection of buttons to index row

use Pavloniym\ActionButtons\ActionButton;

public function fields(Request $request)
{
    return [
      
        // ... Nova default fields
      
        ActionButtons::make()->collection([
            ActionButton::make('')->action(),
            ActionButton::make('')->action(),
            ActionButton::make('')->action(),
        ])
      
        // ... Nova default fields
    ];
}

This fields (both ActionButton and ActionButtons) are available on index and detail (Thanks to @CosminBd) views

Caveats

  • Currently, in order to use this field, you still have to declare the action in your resource actions() method.
  • Tested only on confirm-action-modal action
  • You should provide action instance in action() method of button.
  • If you have action fields that are depends on resource instance -> you should inject resource in action constructor, because Nova doesn't provide NovaRequest instance to fields method on index row
class RefreshAction extends Action 
{

    private Torrent $torrent

    /**
     * @param Torrent $torrent
     */
    public function __construct(Torrent $torrent)
    {
        $this->torrent = $torrent;
    }


    
    /**
     * Get the fields available on the action.
     *
     * @param NovaRequest $request
     * @return array|null
     */
    public function fields(NovaRequest $request): ?array
    {
   
        // $request is empty if action is called from index row (or inline)
        // so use instance injected to action constructor
        $torrent = (fn(): ?Torrent => $request?->selectedResources()?->first())();
        $torrent = $torrent ?? $this->torrent;

        if ($torrent) {
            return [
                File::make('File')->creationRules(['required'])
            ];
        }

        return null;
    }

}

To hide the action either on Index or Detail, you can add the methods in the action declaration as per:

ActionButton::make('My action')
    ->action((new RefreshAction)->onlyOnDetail(), $this->resource?->id)
    ->icon('')
    ->asToolbarButton(),

This is available for both action buttons and action button groups, and it works in individual actions which are part of the action group.


To run actions without confirmation, you can add the $withoutConfirmation = true property to the Laravel Nova action or provide it as a method when you declare the action button

ActionButton::make('My action')
    ->action((new RefreshAction)->withoutConfirmation(), $this->resource?->id)
    ->icon('')
    ->asToolbarButton(),

License

This project is open-sourced software licensed under the MIT license.

nova-action-buttons's People

Contributors

cosminbd avatar pavloniym avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.