Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for Nova 2.0.10 #2

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion dist/css/field.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions resources/sass/field.scss
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
.tabs-component {
margin: 1em 1.5em;
margin:25px;
padding-top:25px;
padding-bottom:25px;
}

.px-6 .tabs-component {
margin-left: 0;
margin-right: 0;
margin: 0px;
padding-top: 10px;
padding-bottom: 10px;
}

.tabs-component-tabs {
Expand Down
13 changes: 9 additions & 4 deletions src/NovaTab.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Laravel\Nova\Fields\Field;
use Laravel\Nova\ResourceTool;
use Illuminate\Http\Resources\MergeValue;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Panel;

class NovaTab extends MergeValue implements \JsonSerializable
{
Expand Down Expand Up @@ -49,9 +51,11 @@ class NovaTab extends MergeValue implements \JsonSerializable
public function __construct($name, $fields = [], $html = null, $errorCallback = null)
{
$this->name = $name;
$this->hasErrorCallback = $errorCallback;

parent::__construct($this->prepareFields($fields, $html));
$this->hasErrorCallback = $errorCallback;
if (app(NovaRequest::class)->isCreateOrAttachRequest()) {
$this->panel = Panel::defaultNameForCreate(app(NovaRequest::class)->newResource());
}
parent::__construct($this->prepareFields($fields, $html));
}

/**
Expand Down Expand Up @@ -82,7 +86,8 @@ protected function prepareFields($fields, $html)

return collect(is_callable($fields) ? $fields() : $fields)
->each(function ($field) use ($html, $hasError) {
if ($field instanceof Field || $field instanceof ResourceTool) {
if ($field instanceof Field || $field instanceof ResourceTool) {
$field->panel = $this->panel;
$field->withMeta([
'tab' => $this->name,
'tabHTML' => $html,
Expand Down
34 changes: 34 additions & 0 deletions src/NovaTabs.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace R64\NovaTab;

use Laravel\Nova\Fields\Field;
use Laravel\Nova\Panel;
use Laravel\Nova\Http\Requests\NovaRequest;

class NovaTabs extends Field
{
Expand All @@ -12,4 +14,36 @@ class NovaTabs extends Field
* @var string
*/
public $component = 'nova-tabs';

/**
* Prepare the field for JSON serialization.
*
* @return array
*/
public function jsonSerialize()
{
if (empty($this->meta()['fields'][0]->panel)) {
if ($id = app(NovaRequest::class)->resourceId) {
$this->panel = Panel::defaultNameForUpdate(app(NovaRequest::class)->findResourceOrFail($id));
} else {
$this->panel = Panel::defaultNameForUpdate(app(NovaRequest::class)->newResource());
}
} else {
$this->panel = $this->meta()['fields'][0]->panel;
}

return array_merge([
'component' => $this->component(),
'prefixComponent' => true,
'indexName' => $this->name,
'name' => $this->name,
'attribute' => $this->attribute,
'value' => $this->value,
'panel' => $this->panel,
'sortable' => $this->sortable,
'nullable' => $this->nullable,
'readonly' => $this->isReadonly(app(NovaRequest::class)),
'textAlign' => $this->textAlign,
], $this->meta());
}
}
29 changes: 24 additions & 5 deletions src/Tabs.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Laravel\Nova\Panel;
use Illuminate\Support\Collection;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Fields\FieldCollection;

trait Tabs
{
Expand All @@ -24,9 +25,9 @@ public function updateFields(NovaRequest $request)
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return array
*/
public function serializeForDetail(NovaRequest $request)
public function serializeForDetail(NovaRequest $request, \Laravel\Nova\Resource $resource)
{
$detailFields = parent::serializeForDetail($request);
$detailFields = parent::serializeForDetail($request, $resource);
$detailFields['fields'] = $this->availableTabs($request, $detailFields['fields']);
return $detailFields;
}
Expand Down Expand Up @@ -62,7 +63,7 @@ public function availableTabs(NovaRequest $request, $fields)
$fields = $fields->all();
}
$this->assignFieldsToTabs($request, $fields);
return collect([
return FieldCollection::make([
(NovaTabs::make('tabs'))
->withMeta(['fields' => array_values($fields)])
]);
Expand All @@ -73,14 +74,32 @@ public function availableTabs(NovaRequest $request, $fields)
protected function assignFieldsToTabs(NovaRequest $request, $fields)
{
foreach ($fields as $field) {
$name = $field->meta['tab'] ?? Panel::defaultNameFor($request->newResource());
$name = $field->meta['tab'] ?? Panel::defaultNameForCreate($request->newResource());
$field->meta['tab'] = [
'name' => $name,
'html' => $field->meta['tabHTML'] ?? $name,
'error' => $field->meta['hasError']
'error' => array_key_exists('hasError', $field->meta) ? $field->meta['hasError'] : ''
];
}

return $fields;
}

/**
* Assign the fields with the given panels to their parent panel.
*
* @param string $label
* @param \Laravel\Nova\Fields\FieldCollection $fields
* @return \Laravel\Nova\Fields\FieldCollection
*/
protected function assignToPanels($label, FieldCollection $fields)
{
return $fields->map(function ($field) use ($label) {
if (! $field->panel) {
$field->panel = $label;
}

return $field;
});
}
}