m-a-k-o / nova-custom-table-card

Simple Nova Card for Custom Tables
MIT License
46 stars 26 forks source link

Add the ability to make classes without new #24

Closed alexrififi closed 3 years ago

alexrififi commented 3 years ago

In some cases, it is more convenient not to wrap the initialization of the class in parentheses for the method call.

Your example before:

// ...
public function cards()
{
    return [
        // ...

        // all the parameters are required except title
        (new \Mako\CustomTableCard\CustomTableCard)
            ->header([
                new \Mako\CustomTableCard\Table\Cell('Order Number'),
                (new \Mako\CustomTableCard\Table\Cell('Price'))->class('text-right'),
            ])
            ->data([
                (new \Mako\CustomTableCard\Table\Row(
                    new \Mako\CustomTableCard\Table\Cell('2018091001'),
                    (new \Mako\CustomTableCard\Table\Cell('20.50'))->class('text-right')->id('price-2')
                ))->viewLink('/resources/orders/1'),
                (new \Mako\CustomTableCard\Table\Row(
                    new \Mako\CustomTableCard\Table\Cell('2018091002'),
                    (new \Mako\CustomTableCard\Table\Cell('201.25'))->class('text-right')->id('price-2')
                )),
            ])
            ->title('Orders')
            ->viewall(['label' => 'View All', 'link' => '/resources/orders']),
    ];
}

Your example after:

// ...
public function cards()
{
    return [
        // ...

        // all the parameters are required except title
        \Mako\CustomTableCard\CustomTableCard::make()
            ->header([
                \Mako\CustomTableCard\Table\Cell::make('Order Number'),
                \Mako\CustomTableCard\Table\Cell::make('Price')->class('text-right'),
            ])
            ->data([
                \Mako\CustomTableCard\Table\Row::make(
                    \Mako\CustomTableCard\Table\Cell::make('2018091001'),
                    \Mako\CustomTableCard\Table\Cell::make('20.50')->class('text-right')->id('price-2')
                )->viewLink('/resources/orders/1'),
                \Mako\CustomTableCard\Table\Row::make(
                    \Mako\CustomTableCard\Table\Cell::make('2018091002'),
                    \Mako\CustomTableCard\Table\Cell::make('201.25')->class('text-right')->id('price-2')
                ),
            ])
            ->title('Orders')
            ->viewall(['label' => 'View All', 'link' => '/resources/orders']),
    ];
}

Example with use (the code is cleaner):

use Mako\CustomTableCard\CustomTableCard;
use Mako\CustomTableCard\Table\Cell;
use Mako\CustomTableCard\Table\Row;

// ...
public function cards()
{
    return [
        // ...

        // all the parameters are required except title
        CustomTableCard::make()
            ->header([
                Cell::make('Order Number'),
                Cell::make('Price')->class('text-right'),
            ])
            ->data([
                Row::make(
                    Cell::make('2018091001'),
                    Cell::make('20.50')->class('text-right')->id('price-2')
                )->viewLink('/resources/orders/1'),
                Row::make(
                    Cell::make('2018091002'),
                    Cell::make('201.25')->class('text-right')->id('price-2')
                ),
            ])
            ->title('Orders')
            ->viewall(['label' => 'View All', 'link' => '/resources/orders']),
    ];
}

The PR is backward compatible, does not break old code. Give the opportunity to use both options

m-a-k-o commented 3 years ago

@medvinator Thank you for your contribution :)

You're pull request has been tagged with 3.1.0 for release.