Skip to content

Commit

Permalink
Refactor EuiTabbedContent to track its selected tab by name (#931)
Browse files Browse the repository at this point in the history
* Refactor EuiTabbedContent to track its selected tab by id so content can be updated

* changelog
  • Loading branch information
chandlerprall authored Jun 18, 2018
1 parent f42798f commit 53155b7
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 4 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## [`master`](https://github.com/elastic/eui/tree/master)

No public interface changes since `0.0.53`.
**Bug fixes**

- `EuiTabbedContent` now updates dynamic tab content when used as an uncontrolled component ([#931](https://github.com/elastic/eui/pull/931))

## [`0.0.53`](https://github.com/elastic/eui/tree/v0.0.53)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,101 @@ exports[`EuiTabbedContent behavior when selected tab state isn't controlled by t
</div>
`;

exports[`EuiTabbedContent behavior when uncontrolled, the selected tab should update if it receives new content 1`] = `
<EuiTabbedContent
tabs={
Array [
Object {
"content": <p>
Elasticsearch content
</p>,
"id": "es",
"name": "Elasticsearch",
},
Object {
"content": <p>
updated Kibana content
</p>,
"data-test-subj": "kibanaTab",
"id": "kibana",
"name": "Kibana",
},
]
}
>
<div>
<EuiTabs>
<div
className="euiTabs"
role="tablist"
>
<EuiTab
aria-controls="42-es"
disabled={false}
id="es"
isSelected={false}
key="es"
onClick={[Function]}
>
<button
aria-controls="42-es"
aria-selected={false}
className="euiTab"
disabled={false}
id="es"
onClick={[Function]}
role="tab"
type="button"
>
<span
className="euiTab__content"
>
Elasticsearch
</span>
</button>
</EuiTab>
<EuiTab
aria-controls="42-kibana"
data-test-subj="kibanaTab"
disabled={false}
id="kibana"
isSelected={true}
key="kibana"
onClick={[Function]}
>
<button
aria-controls="42-kibana"
aria-selected={true}
className="euiTab euiTab-isSelected"
data-test-subj="kibanaTab"
disabled={false}
id="kibana"
onClick={[Function]}
role="tab"
type="button"
>
<span
className="euiTab__content"
>
Kibana
</span>
</button>
</EuiTab>
</div>
</EuiTabs>
<div
aria-labelledby="kibana"
id="42-kibana"
role="tabpanel"
>
<p>
updated Kibana content
</p>
</div>
</div>
</EuiTabbedContent>
`;

exports[`EuiTabbedContent is rendered with required props and tabs 1`] = `
<div
aria-label="aria-label"
Expand Down
8 changes: 5 additions & 3 deletions src/components/tabs/tabbed_content/tabbed_content.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class EuiTabbedContent extends Component {
// Only track selection state if it's not controlled externally.
if (!selectedTab) {
this.state = {
selectedTab: initialSelectedTab || tabs[0],
selectedTabId: (initialSelectedTab && initialSelectedTab.id) || tabs[0].id,
};
}
}
Expand All @@ -60,7 +60,7 @@ export class EuiTabbedContent extends Component {

// Only track selection state if it's not controlled externally.
if (!externalSelectedTab) {
this.setState({ selectedTab })
this.setState({ selectedTabId: selectedTab.id })
}
};

Expand All @@ -76,7 +76,9 @@ export class EuiTabbedContent extends Component {
} = this.props;

// Allow the consumer to control tab selection.
const selectedTab = externalSelectedTab || this.state.selectedTab
const selectedTab = externalSelectedTab || tabs.find(
tab => tab.id === this.state.selectedTabId
);

const {
content: selectedTabContent,
Expand Down
24 changes: 24 additions & 0 deletions src/components/tabs/tabbed_content/tabbed_content.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,29 @@ describe('EuiTabbedContent', () => {
const component = render(<EuiTabbedContent tabs={tabs} />);
expect(component).toMatchSnapshot();
});

test('when uncontrolled, the selected tab should update if it receives new content', () => {
const tabs = [
elasticsearchTab,
{
...kibanaTab
}
];
const component = mount(<EuiTabbedContent tabs={tabs}/>);

component.find('EuiTab[id="kibana"] button').first().simulate('click');

component.setProps({
tabs: [
elasticsearchTab,
{
...kibanaTab,
content: <p>updated Kibana content</p>
}
]
});

expect(component).toMatchSnapshot();
});
});
});

0 comments on commit 53155b7

Please sign in to comment.