From 307af537bbbed277c0adc451e4f846d4acc635ae Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Lebleu?= Date: Tue, 7 Mar 2017 18:11:24 +0100 Subject: [PATCH] Allow the target tab's id to be specified through data-tabs-target --- docs/pages/tabs.md | 4 ++-- js/foundation.tabs.js | 10 +++++----- test/javascript/components/tabs.js | 11 +++++++++-- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/docs/pages/tabs.md b/docs/pages/tabs.md index 1f9ddd0a0..d18022c67 100644 --- a/docs/pages/tabs.md +++ b/docs/pages/tabs.md @@ -7,12 +7,12 @@ js: js/foundation.tabs.js ## Basics -There are two pieces to a tabbed interface: the tabs themselves, and the content for each tab. The tabs are an element with the class `.tabs`, and each item has the class `.tabs-title`. Each tab contains a link to a tab. The `href` of each link should match the ID of a tab. +There are two pieces to a tabbed interface: the tabs themselves, and the content for each tab. The tabs are an element with the class `.tabs`, and each item has the class `.tabs-title`. Each tab contains a link to a tab. The `href` of each link should match the ID of a tab. Alternatively, the ID can be specified with the attribute `data-tabs-target`. ```html ``` diff --git a/js/foundation.tabs.js b/js/foundation.tabs.js index b27ec8158..bac091a11 100644 --- a/js/foundation.tabs.js +++ b/js/foundation.tabs.js @@ -50,7 +50,7 @@ class Tabs { var $elem = $(this), $link = $elem.find('a'), isActive = $elem.hasClass(`${_this.options.linkActiveClass}`), - hash = $link[0].hash.slice(1), + hash = $link.attr('data-tabs-target') || $link[0].hash.slice(1), linkId = $link[0].id ? $link[0].id : `${hash}-label`, $tabContent = $(`#${hash}`); @@ -234,8 +234,8 @@ class Tabs { var $oldTab = this.$element. find(`.${this.options.linkClass}.${this.options.linkActiveClass}`), $tabLink = $target.find('[role="tab"]'), - hash = $tabLink[0].hash, - $targetContent = this.$tabContent.find(hash); + hash = $tabLink.attr('data-tabs-target') || $tabLink[0].hash.slice(1), + $targetContent = this.$tabContent.find(`#${hash}`); //close old tab this._collapseTab($oldTab); @@ -271,8 +271,8 @@ class Tabs { */ _openTab($target) { var $tabLink = $target.find('[role="tab"]'), - hash = $tabLink[0].hash, - $targetContent = this.$tabContent.find(hash); + hash = $tabLink.attr('data-tabs-target') || $tabLink[0].hash.slice(1), + $targetContent = this.$tabContent.find(`#${hash}`); $target.addClass(`${this.options.linkActiveClass}`); diff --git a/test/javascript/components/tabs.js b/test/javascript/components/tabs.js index e91c44cad..3aeaef986 100644 --- a/test/javascript/components/tabs.js +++ b/test/javascript/components/tabs.js @@ -6,7 +6,7 @@ describe('Tabs', function() {
@@ -78,6 +78,13 @@ describe('Tabs', function() { plugin.selectTab('#panel2'); $html.find('#panel2').should.be.visible; }); + it('opens the selected tab with data-tabs-target attribute', function() { + $html = $(template).appendTo('body'); + plugin = new Foundation.Tabs($html.find('[data-tabs]'), {}); + + plugin.selectTab('#/panel3'); + $html.find('#panel3').should.be.visible; + }); }); describe('_handleTabChange()', function() { @@ -131,4 +138,4 @@ describe('Tabs', function() { }); }); -}); \ No newline at end of file +}); -- 2.47.2