Skip to content

Commit

Permalink
feat: hasSlug allows setting the tabs hash key
Browse files Browse the repository at this point in the history
- added hasSlug function to Tabs panel
- HasTabs mixin checks for non-null slug and uses it instead of name
- adds preservedName variable to retain the name after the parent panel is created
- HasTabs Trait gets slug on panel recreation
- fixes naming issue for mix.js conflicting with "mix" command in some environments
-field.js rebuilt with changes
  • Loading branch information
ianrobertsFF committed May 11, 2022
1 parent 81068d9 commit 7412afd
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dist/js/field.js

Large diffs are not rendered by default.

File renamed without changes.
4 changes: 2 additions & 2 deletions resources/js/mixins/HasTabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default {

const tabs = this.tabs = this.setTabs();
const routeTabs = parseLocationHash();
const currentTabSlug = routeTabs[this.panel.name];
const currentTabSlug = routeTabs[this.panel.slug ?? this.panel.name];

if (tabs[currentTabSlug]) {
this.handleTabClick(tabs[currentTabSlug])
Expand Down Expand Up @@ -202,7 +202,7 @@ export default {
*/
setLocationHash() {
const routeTabs = parseLocationHash()
routeTabs[this.panel.name] = this.selectedTab.slug
routeTabs[this.panel.slug ?? this.panel.name] = this.selectedTab.slug
updateLocationHash(routeTabs)
},

Expand Down
21 changes: 21 additions & 0 deletions src/Tabs.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Http\Resources\MergeValue;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Laravel\Nova\Contracts\ListableField;
use Laravel\Nova\Panel;
use Laravel\Nova\ResourceToolElement;
Expand Down Expand Up @@ -39,6 +40,10 @@ class Tabs extends Panel

private $tabsCount = 0;

public $slug = null;

private $preservedName;

/**
* Create a new panel instance.
*
Expand All @@ -49,11 +54,26 @@ class Tabs extends Panel
public function __construct($name, $fields = [])
{
$this->name = $name;
$this->preservedName = $name;
$this->withComponent('tabs');

parent::__construct($this->prepareFields($fields));
}


/**
* Set the tabs slug.
*
* @param string|boolean $slug
* @return $this
*/
public function withSlug($slug) {

$this->slug = is_bool($slug) ? $slug = Str::slug($this->preservedName, '_') : $slug;

return $this;
}

/**
* Prepare the given fields.
*
Expand Down Expand Up @@ -217,6 +237,7 @@ public function jsonSerialize(): array
$result = array_merge(parent::jsonSerialize(), [
'defaultSearch' => $this->defaultSearch,
'showTitle' => $this->showTitle,
'slug' => $this->slug
]);

return $result;
Expand Down
1 change: 1 addition & 0 deletions src/Traits/HasTabs.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ protected function resolvePanelsFromFields(NovaRequest $request, FieldCollection
$panel->name = $panel->meta['fields'][0]->panel;
$panel->showTitle = $panel->meta['fields'][0]->assignedPanel->showTitle;
$panel->showToolbar = $panel->meta['fields'][0]->assignedPanel->showToolbar;
$panel->slug = $panel->meta['fields'][0]->assignedPanel->slug;
}

return $panel;
Expand Down
2 changes: 1 addition & 1 deletion webpack.mix.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ let tailwindcss = require('tailwindcss')
let path = require('path')
let postcssImport = require('postcss-import')

require('./mix')
require('./nova.mix.js')

mix
.setPublicPath('dist')
Expand Down

0 comments on commit 7412afd

Please sign in to comment.