Skip to content

Commit f2e0c47

Browse files
authored
Merge pull request #141 from fleetbase/dev-v0.3.40
Fix tab navigation active state persistence
2 parents 762af30 + 3bf9bee commit f2e0c47

3 files changed

Lines changed: 60 additions & 3 deletions

File tree

addon/components/tab-navigation.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,11 @@ export default class TabNavigationComponent extends Component {
139139
}
140140

141141
@action argsDidChange() {
142-
const nextActiveTabId = this.args.activeTabId || (this.args.tabs?.[0]?.id ?? null);
142+
const tabs = this.args.tabs ?? [];
143+
const hasControlledActiveTab = this.args.activeTabId !== undefined;
144+
const currentActiveTabExists = tabs.some((tab) => tab.id === this._activeTabId);
145+
const nextActiveTabId = hasControlledActiveTab ? this.args.activeTabId : currentActiveTabExists ? this._activeTabId : (tabs[0]?.id ?? null);
146+
143147
if (nextActiveTabId !== this._activeTabId) {
144148
this._activeTabId = nextActiveTabId;
145149
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@fleetbase/ember-ui",
3-
"version": "0.3.39",
3+
"version": "0.3.40",
44
"description": "Fleetbase UI provides all the interface components, helpers, services and utilities for building a Fleetbase extension into the Console.",
55
"keywords": [
66
"fleetbase-ui",

tests/integration/components/tab-navigation-test.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { module, test } from 'qunit';
22
import { setupRenderingTest } from 'dummy/tests/helpers';
3-
import { click, render } from '@ember/test-helpers';
3+
import { click, render, settled } from '@ember/test-helpers';
44
import { hbs } from 'ember-cli-htmlbars';
55

66
module('Integration | Component | tab-navigation', function (hooks) {
@@ -98,6 +98,59 @@ module('Integration | Component | tab-navigation', function (hooks) {
9898
assert.dom('[data-tab-navigation-more]').doesNotExist();
9999
});
100100

101+
test('it preserves uncontrolled active tab when tabs are refreshed with the same ids', async function (assert) {
102+
this.set('tabs', [
103+
{ id: 'overview', label: 'Overview' },
104+
{ id: 'positions', label: 'Positions' },
105+
{ id: 'devices', label: 'Devices' },
106+
]);
107+
108+
await render(hbs`
109+
<TabNavigation @tabs={{this.tabs}} as |activeTab|>
110+
{{activeTab.label}}
111+
</TabNavigation>
112+
`);
113+
114+
await click('[role="tab"][data-tab-id="positions"]');
115+
assert.dom('.tab-content').hasText('Positions');
116+
117+
this.set('tabs', [
118+
{ id: 'overview', label: 'Overview refreshed' },
119+
{ id: 'positions', label: 'Positions refreshed' },
120+
{ id: 'devices', label: 'Devices refreshed' },
121+
]);
122+
await settled();
123+
124+
assert.dom('.tab-content').hasText('Positions refreshed');
125+
assert.dom('[role="tab"][data-tab-id="positions"]').hasClass('tab-item--active');
126+
});
127+
128+
test('it falls back to the first tab when uncontrolled active tab is removed', async function (assert) {
129+
this.set('tabs', [
130+
{ id: 'overview', label: 'Overview' },
131+
{ id: 'positions', label: 'Positions' },
132+
{ id: 'devices', label: 'Devices' },
133+
]);
134+
135+
await render(hbs`
136+
<TabNavigation @tabs={{this.tabs}} as |activeTab|>
137+
{{activeTab.label}}
138+
</TabNavigation>
139+
`);
140+
141+
await click('[role="tab"][data-tab-id="positions"]');
142+
assert.dom('.tab-content').hasText('Positions');
143+
144+
this.set('tabs', [
145+
{ id: 'overview', label: 'Overview refreshed' },
146+
{ id: 'devices', label: 'Devices refreshed' },
147+
]);
148+
await settled();
149+
150+
assert.dom('.tab-content').hasText('Overview refreshed');
151+
assert.dom('[role="tab"][data-tab-id="overview"]').hasClass('tab-item--active');
152+
});
153+
101154
test('it moves overflowing array tabs into a More menu', async function (assert) {
102155
availableWidth = 142;
103156
this.set('tabs', [

0 commit comments

Comments
 (0)