Skip to content
This repository was archived by the owner on May 2, 2023. It is now read-only.

Commit 7841939

Browse files
committed
CHG: Use objects instead of arrays
1 parent aff4054 commit 7841939

File tree

4 files changed

+92
-54
lines changed

4 files changed

+92
-54
lines changed

readme.md

Lines changed: 19 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,13 @@ This package is ment to be used **INSTEAD** of overwriting the `custom-index-hea
66

77
## Usage
88

9-
Let's say you want to add a custom button to the `toolbar` of all `index` views. Just create a vue component for it, as you would do if you use the `custom-index-header` (see section "Create custom component" if you don't know how to). Let's call it `my-index-toolbar-btn` Now the only thing you have to do is register it to your `\App\Ņova\Resource` class, within a new method called `customIndexToolbarComponents`:
9+
Let's say you want to add a custom button to the `toolbar` of all `index` views. Just create a vue component for it, as you would do if you use the `custom-index-header` (see section "Create custom component" if you don't know how to). Let's call it `my-index-toolbar-btn`. Now the only thing you have to do is register it to your `\App\Ņova\Resource` class, within a new method called `customIndexToolbarComponents`, which returns a `\Bernhardh\NovaDynamicViews\CustomComponents` object:
1010

1111
```php
1212
public function customIndexToolbarComponents()
1313
{
14-
return [
15-
'items' => [
16-
[
17-
'name' => 'my-index-toolbar-btn',
18-
],
19-
]
20-
];
14+
return CustomComponents::make()
15+
->addItem('my-index-toolbar-btn');
2116
}
2217
```
2318

@@ -30,32 +25,22 @@ If you want to add extra data (for example a label) to your component (without e
3025
```php
3126
public function customIndexToolbarComponents()
3227
{
33-
return [
34-
'items' => [
35-
[
36-
'name' => 'my-index-toolbar-btn',
37-
'meta' => [
38-
'label' => 'My label'
39-
]
40-
],
41-
]
42-
];
28+
return CustomComponents::make()
29+
->addItem('my-index-toolbar-btn', [
30+
'label' => 'My label'
31+
]);
4332
}
4433
```
4534

4635
### Add (tailwind) class to the container
4736

48-
If you want to add additional css classes to the container div of a section (for example add `flex w-full justify-end items-center mx-3` to the `customIndexToolbarComponents` section), add the `class` key:
37+
If you want to add additional css classes to the container div of a section (for example add `flex w-full justify-end items-center mx-3` to the `customIndexToolbarComponents` section), add the `class` in the `make` function (or use the `setClass` method):
4938

5039
```php
5140
public function customIndexToolbarComponents()
5241
{
53-
return [
54-
'class' => 'flex w-full justify-end items-center mx-3',
55-
'items' => [
56-
...
57-
]
58-
];
42+
return CustomComponents::make('flex w-full justify-end items-center mx-3')
43+
->addItem('my-index-toolbar-btn');
5944
}
6045
```
6146

@@ -72,23 +57,13 @@ class Resource extends \Laravel\Nova\Resource {
7257
*/
7358
public function customIndexToolbarComponents()
7459
{
75-
return [
76-
'class' => 'flex w-full justify-end items-center mx-3',
77-
'items' => [
78-
[
79-
'name' => 'my-index-toolbar-btn',
80-
'meta' => [
81-
'label' => 'My first btn'
82-
]
83-
],
84-
[
85-
'name' => 'my-index-toolbar-btn',
86-
'meta' => [
87-
'label' => 'My second btn'
88-
]
89-
],
90-
]
91-
];
60+
return CustomComponents::make('flex w-full justify-end items-center mx-3')
61+
->addItem('my-index-toolbar-btn', [
62+
'title' => 'My first btn'
63+
])
64+
->addItem('my-index-toolbar-btn', [
65+
'title' => 'My second btn'
66+
]);
9267
}
9368

9469
/**
@@ -98,13 +73,8 @@ class Resource extends \Laravel\Nova\Resource {
9873
*/
9974
public function customDetailHeaderComponents()
10075
{
101-
return [
102-
'items' => [
103-
[
104-
'name' => 'my-other-component'
105-
]
106-
]
107-
];
76+
return CustomComponents::make()
77+
->addItem('my-other-component');
10878
}
10979
}
11080
```

routes/api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
if(method_exists($resource, $method)) {
2424
$data = $resource->$method();
25-
if($data && is_array($data)) {
25+
if($data) {
2626
return $data;
2727
}
2828
}

src/CustomComponents.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace Bernhardh\NovaDynamicViews;
4+
5+
class CustomComponents implements \JsonSerializable
6+
{
7+
/**
8+
* @var string
9+
*/
10+
protected $class;
11+
/**
12+
* @var array
13+
*/
14+
protected $items = [];
15+
16+
17+
/**
18+
* @param string $class
19+
*
20+
* @return static
21+
*/
22+
public static function make($class = '')
23+
{
24+
$obj = new static();
25+
$obj->class = $class;
26+
27+
return $obj;
28+
}
29+
30+
31+
/**
32+
* @param string $name
33+
* @param array $meta
34+
*
35+
* @return $this
36+
*/
37+
public function addItem($name, $meta = [])
38+
{
39+
$item = ['name'=> $name];
40+
if($meta) {
41+
$item['meta'] = $meta;
42+
}
43+
44+
$this->items[] = $item;
45+
46+
return $this;
47+
}
48+
49+
50+
/**
51+
* @param string $class
52+
*
53+
* @return CustomComponents
54+
*/
55+
public function setClass(string $class) {
56+
$this->class = $class;
57+
58+
return $this;
59+
}
60+
61+
62+
/**
63+
* @return array|mixed
64+
*/
65+
public function jsonSerialize()
66+
{
67+
return array_filter([
68+
'class' => $this->class,
69+
'items' => $this->items
70+
]);
71+
}
72+
}

src/ToolServiceProvider.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ public function boot()
2222
$this->app->booted(function () {
2323
$this->routes();
2424
});
25-
26-
Nova::serving(function (ServingNova $event) {
27-
//
28-
});
2925
}
3026

3127
/**

0 commit comments

Comments
 (0)