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(Tabset): reset tabset to current tab when first tab is removed #877

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/components/tabset/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ <h2 id="dynamic-tabset">Dynamic Tabset</h2>
<hx-tabset
ref="tabset"
:current-tab="currentTab"
:tabsize="tabSize"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the advantage of to adding a tab size as an attribute? I wonder if the hx-tabset component could/should/can be aware of how many tabs it has.

Side note: tabCount, might be a more descriptive name as "size" might imply "small, medium, large" etc, related to a component.

@tabchange="onTabchange"
>
<hx-tablist>
Expand Down
5 changes: 4 additions & 1 deletion docs/components/tabset/tabset-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function getDefaultData (force = false) {
{ id: 2 },
{ id: 3 },
],
tabSize: 3,
};

if (force === true) {
Expand All @@ -32,6 +33,8 @@ if (document.getElementById('vue-dynamicTabsetDemo')) {
if (this.autoUpdate) {
this.update();
}

this.tabSize = this.tabs.length;
},
methods: {
addTab: function (dir) {
Expand Down Expand Up @@ -111,7 +114,7 @@ if (document.getElementById('vue-dynamicTabsetDemo')) {
// isn't smart enough to re-indent HTML tags.
snippet: function () {
return Util.snippet(`
<hx-tabset current-tab="${this.currentTab}">
<hx-tabset current-tab="${this.currentTab}" tabsize="${this.tabSize}">
<hx-tablist>
${this._tablist}
</hx-tablist>
Expand Down
16 changes: 16 additions & 0 deletions docs/elements/hx-tabset/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ <h2 id="methods">Methods</h2>
Automatically updates visual state, when changed.
</p>
</dd>
<dt>tabsize {Number}</dt>
<dd>
<p>
Number of tabs.
</p>
</dd>
</dl>
{% endblock %}

Expand All @@ -143,5 +149,15 @@ <h2 id="methods">Methods</h2>
Reflects the <code>current-tab</code> attribute.
</p>
</dd>
<dt>tabSize {Integer}</dt>
<dd>
<p>
Number of tabs.
</p>
<p class="hxSubdued hxSubBody">
<hx-icon type="info-circle"></hx-icon>
Reflects the <code>tabsize</code> attribute.
</p>
</dd>
</dl>
{% endblock %}
23 changes: 22 additions & 1 deletion src/elements/hx-tabset/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ export class HXTabsetElement extends HXElement {

$onConnect () {
this.$upgradeProperty('current-tab');
this.$upgradeProperty('tabsize');
this.$defaultAttribute('id', `tabset-${generateId()}`);
this._setupIds();
this.currentTab = Number(this.getAttribute('current-tab')) || 0;
this.tabSize = Number(this.getAttribute('tabsize')) || this.tabs.length;
this._tablist.addEventListener('keyup', this._onKeyUp);
this._tablist.addEventListener('keydown', preventKeyScroll);
this.addEventListener('hxtabclick', this._onHxtabclick);
Expand All @@ -48,7 +50,7 @@ export class HXTabsetElement extends HXElement {
}

static get $observedAttributes () {
return [ 'current-tab' ];
return [ 'current-tab', 'tabsize' ];
}

$onAttributeChange (attr, oldVal, newVal) {
Expand All @@ -58,6 +60,12 @@ export class HXTabsetElement extends HXElement {
this.$emit('tabchange');
}
}
if (attr === 'tabsize') {
if (this.currentTab === 0 && !isNaN(newVal)) {
this._activateTab(0);
this.$emit('tabchange');
}
}
}

/* ---------- PUBLIC MEMBERS ---------- */
Expand Down Expand Up @@ -88,6 +96,19 @@ export class HXTabsetElement extends HXElement {
this.setAttribute('current-tab', idx);
}

/* ---------- PUBLIC MEMBERS ---------- */

/**
* Number of Tabs.
* @type {Number}
*/
get tabSize () {
return Number(this.getAttribute('tabsize')) || this.tabs.length;
}
set tabSize (len) {
this.setAttribute('tabsize', len);
}

/* ---------- PUBLIC METHODS ---------- */

/**
Expand Down
9 changes: 9 additions & 0 deletions src/elements/hx-tabset/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ describe('<hx-tabset> component tests', () => {

expect(attr).to.be.true;
});

it('should get tabsize on render', async () => {
const fragment = /** @type {HXTabsetElement} */ await fixture(mockup);
const tab = fragment.hasAttribute('tabsize');
const tabSize = fragment.tabSize;

expect(tab).to.be.true;
expect(tabSize).to.equal(3);
});
});

describe(`test ${template} getters and setters`, () => {
Expand Down