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

Refactor EuiTabbedContent to track its selected tab by name #931

Merged
Merged
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
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();
});
});
});