Skip to content

Commit db9ec28

Browse files
authored
Merge pull request #100 from Gustavinho/refactor/use-card-item-as-blade-components
Refactor/use card item as blade components
2 parents dc73432 + ee3c1cc commit db9ec28

File tree

6 files changed

+55
-25
lines changed

6 files changed

+55
-25
lines changed

doc/grid-view.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,35 @@ These are the fields by default but you can add more if you want to customize yo
6262

6363
## Customizing card data
6464

65-
The grid view has a card component by default with some data but you can either create your own card and use as much data as you need in the `card` mthod and just use your own card implementation, you just need to specify a blade file.
65+
The grid view has a card component by default with some data, however, you can create your own card component and use as much data as you need in the `card` method, you just need to specify a blade file with your Grid View and return the data that you need in the `card` method.
6666

6767
```php
6868
public $cardComponent = 'components.my-card';
69+
70+
public function card($model) {
71+
return [
72+
'name' => $model->name,
73+
'email' => $model->email,
74+
'model' => $model
75+
];
76+
}
77+
```
78+
79+
All the data returned in the `card` method will be received as a prop in your blade component alog with these other default props that you can use based on your needs.
80+
81+
Name|Description|Type|Value
82+
--|--|--|--|
83+
model|Model instance for this card|||
84+
actions|Actions defined in this view class|Array
85+
hasDefaultAction|Checks if the Grid View has defined a `onCardClick` method|Boolean|true,false
86+
87+
With all this data you can build your own card component as you need, remember to include the `actions` component.
88+
89+
```html
90+
<x-lv-actions :actions="$actions" :model="$model" />
6991
```
7092

93+
7194
## Default card item action
7295
You can define a default action that will be fired clicking on the card image or the card title, this action gets the model instance that fired it.
7396

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@props(['actions', 'model'])
2+
3+
<x-lv-drop-down>
4+
<x-slot name="trigger">
5+
<x-lv-icon-button icon="more-horizontal" size="sm"/>
6+
</x-slot>
7+
8+
<x-lv-actions.icon-and-title :actions="$actions" :model="$model" />
9+
</x-lv-drop-down>

resources/views/components/card.blade.php renamed to resources/views/grid-view/grid-view-item.blade.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1+
@props([
2+
'image',
3+
'title',
4+
'subtitle',
5+
'description',
6+
'withBackground',
7+
'model',
8+
'actions',
9+
'hasDefaultAction'
10+
])
11+
112
<div class="{{ $withBackground ? 'rounded-md shadow-md' : '' }}">
213
@if ($hasDefaultAction)
314
<a href="#!" wire:click.prevent="onCardClick({{ $model->id }})">
4-
<img
5-
src="{{ $image }}"
6-
alt="{{ $image }}"
7-
class="hover:shadow-lg cursor-pointer rounded-md h-48 w-full object-cover {{ $withBackground ? 'rounded-b-none' : '' }}">
15+
<img src="{{ $image }}" alt="{{ $image }}" class="hover:shadow-lg cursor-pointer rounded-md h-48 w-full object-cover {{ $withBackground ? 'rounded-b-none' : '' }}">
816
</a>
917
@else
10-
<img
11-
src="{{ $image }}"
12-
alt="{{ $image }}"
13-
class="rounded-md h-48 w-full object-cover {{ $withBackground ? 'rounded-b-none' : '' }}">
18+
<img src="{{ $image }}" alt="{{ $image }}" class="rounded-md h-48 w-full object-cover {{ $withBackground ? 'rounded-b-none' : '' }}">
1419
@endif
1520

1621
<div class="pt-4 {{ $withBackground ? 'bg-white rounded-b-md p-4' : '' }}">
@@ -38,7 +43,6 @@ class="rounded-md h-48 w-full object-cover {{ $withBackground ? 'rounded-b-none'
3843
<x-lv-icon-button icon="more-horizontal" size="sm"/>
3944
</x-slot>
4045

41-
<x-lv-actions.icon-and-title :actions="$actions" :model="$model" />
4246
</x-lv-drop-down>
4347
</div>
4448
</div>

resources/views/grid-view/grid-view.blade.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55
66
You can customize all the html and css classes but YOU MUST KEEP THE BLADE AND LIVEWIERE DIRECTIVES
77
8-
UI components used:
9-
- table-view.filters
10-
- components.alert
11-
- components.card
12-
- components.paginator --}}
8+
--}}
139

1410
<x-lv-layout>
1511
{{-- Search input and filters --}}
@@ -19,16 +15,14 @@
1915

2016
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-{{ $maxCols }} gap-8 md:gap-8">
2117
@foreach ($items as $item)
22-
@component($cardComponent, array_merge(
23-
$view->card($item),
24-
[
18+
<x-lv-dynamic-component
19+
:view="$cardComponent"
20+
:data="array_merge($this->card($item), [
2521
'withBackground' => $withBackground,
2622
'model' => $item,
2723
'actions' => $actionsByRow,
28-
'hasDefaultAction' => $this->hasDefaultAction
29-
]
30-
))
31-
@endcomponent
24+
'hasDefaultAction' => $this->hasDefaultAction])"
25+
/>
3226
@endforeach
3327
</div>
3428

src/Views/GridView.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class GridView extends DataView
77
/** Component name */
88
protected $view = 'grid-view.grid-view';
99

10-
public $cardComponent = 'laravel-views::components.card';
10+
public $cardComponent = 'laravel-views::grid-view.grid-view-item';
1111

1212
/** Add a white background on each card */
1313
public $withBackground = false;

stubs/grid-view.stub

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class DummyClass extends GridView
2020
/**
2121
* Sets the data to every card on the view
2222
*
23-
* @param $item Current model for each card
23+
* @param $model Current model for each card
2424
*/
25-
public function card($item)
25+
public function card($model)
2626
{
2727
return [
2828
'image' => '',

0 commit comments

Comments
 (0)