From 1adf95eceac21186e56af08483b6c541e9aa3a18 Mon Sep 17 00:00:00 2001 From: elijahworkz <34365440+elijahworkz@users.noreply.github.com> Date: Tue, 2 Feb 2021 22:04:25 +0300 Subject: [PATCH] fix panels for Tabs in create and update views. fixes #85 (#139) * fix panels for Tabs in create and update views * refactor assignTabPanels * removed snake case * added PHPDoc --- src/TabsOnEdit.php | 51 +++++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/src/TabsOnEdit.php b/src/TabsOnEdit.php index 4d0f746..012ffd7 100644 --- a/src/TabsOnEdit.php +++ b/src/TabsOnEdit.php @@ -20,15 +20,7 @@ trait TabsOnEdit */ public function creationFields(NovaRequest $request) { - return new TabsFieldCollection( - [ - 'Tabs' => [ - 'component' => 'tabs', - 'fields' => $this->removeNonCreationFields($request, $this->resolveFields($request)), - 'panel' => Panel::defaultNameForCreate($request->newResource()), - ], - ] - ); + return $this->assignTabPanels($this->removeNonCreationFields($request, $this->resolveFields($request))); } /** @@ -140,15 +132,7 @@ public static function rulesForUpdate(NovaRequest $request, $resource = null) */ public function updateFields(NovaRequest $request) { - return new TabsFieldCollection( - [ - 'Tabs' => [ - 'component' => 'tabs', - 'fields' => $this->removeNonUpdateFields($request, $this->resolveFields($request)), - 'panel' => Panel::defaultNameForUpdate($this->resolveResource($request)), - ], - ] - ); + return $this->assignTabPanels($this->removeNonUpdateFields($request, $this->resolveFields($request))); } /** @@ -161,7 +145,8 @@ public function updateFields(NovaRequest $request) protected function assignToPanels($label, FieldCollection $fields) { return $fields->map(function ($field) use ($label) { - if (!\is_array($field) && !$field->panel) { + if (!is_array($field) && !$field->panel) { + $field->panel = $label; } @@ -181,4 +166,32 @@ private function resolveResource(NovaRequest $request) return $request->newResource(); } + + /** + * Assigns fields to proper tab components + * @param \Laravel\Nova\Fields\FieldCollection $Fields + * @return \Laravel\Nova\Fields\FieldCollection + */ + private function assignTabPanels(FieldCollection $fields) + { + $tabPanels = []; + + $nonTabFields = $fields->filter(function ($field, $key) use (&$tabPanels) { + $isTabField = isset($field->meta['tab']); + if ($isTabField) { + if (! isset($tabPanels[$field->panel])) { + $newPanel = [ + 'component' => 'tabs', + 'panel' => $field->panel, + 'fields' => [] + ]; + $tabPanels[$field->panel] = $newPanel; + } + $tabPanels[$field->panel]['fields'][] = $field; + } + return !$isTabField; + }); + + return $nonTabFields->concat($tabPanels); + } }