Skip to content

Commit e87de36

Browse files
authored
Merge pull request #5312 from WiXSL/fix-optional-first-tab
Fix TabbedShowLayout resolves path incorrectly if first tab is null
2 parents e84c924 + 020ba01 commit e87de36

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

packages/ra-ui-materialui/src/detail/TabbedShowLayout.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,18 @@ const TabbedShowLayout = props => {
8787
...rest
8888
} = props;
8989
const match = useRouteMatch();
90-
9190
const classes = useStyles(props);
91+
const nonNullChildren = Children.toArray(children).filter(
92+
child => child !== null
93+
);
9294

9395
return (
9496
<div className={className} key={version} {...sanitizeRestProps(rest)}>
95-
{cloneElement(tabs, {}, children)}
97+
{cloneElement(tabs, {}, nonNullChildren)}
9698

9799
<Divider />
98100
<div className={classes.content}>
99-
{Children.map(children, (tab, index) =>
101+
{Children.map(nonNullChildren, (tab, index) =>
100102
tab && isValidElement(tab) ? (
101103
<Route
102104
exact
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import * as React from 'react';
2+
import expect from 'expect';
3+
import { cleanup, render } from '@testing-library/react';
4+
5+
import TabbedShowLayout from './TabbedShowLayout';
6+
import Tab from './Tab';
7+
import TextField from '../field/TextField';
8+
import { createMemoryHistory } from 'history';
9+
import { Router } from 'react-router-dom';
10+
11+
describe('<TabbedShowLayout />', () => {
12+
afterEach(cleanup);
13+
14+
const renderWithRouter = children => {
15+
const history = createMemoryHistory();
16+
17+
return {
18+
history,
19+
...render(<Router history={history}>{children}</Router>),
20+
};
21+
};
22+
23+
it('should display the first Tab component and its content', () => {
24+
const { queryByText, history } = renderWithRouter(
25+
<TabbedShowLayout basePath="/" record={{ id: 123 }} resource="foo">
26+
<Tab label="Tab1">
27+
<TextField label="Field On Tab1" source="field1" />
28+
</Tab>
29+
<Tab label="Tab2">
30+
<TextField label="Field On Tab2" source="field2" />
31+
</Tab>
32+
</TabbedShowLayout>
33+
);
34+
35+
expect(queryByText('Tab1')).not.toBeNull();
36+
expect(queryByText('Field On Tab1')).not.toBeNull();
37+
});
38+
39+
it('should display the first valid Tab component and its content', () => {
40+
const { queryByText, history } = renderWithRouter(
41+
<TabbedShowLayout basePath="/" record={{ id: 123 }} resource="foo">
42+
{null}
43+
<Tab label="Tab1">
44+
<TextField label="Field On Tab1" source="field1" />
45+
</Tab>
46+
<Tab label="Tab2">
47+
<TextField label="Field On Tab2" source="field2" />
48+
</Tab>
49+
</TabbedShowLayout>
50+
);
51+
52+
expect(queryByText('Tab1')).not.toBeNull();
53+
expect(queryByText('Field On Tab1')).not.toBeNull();
54+
});
55+
});

0 commit comments

Comments
 (0)