Skip to content

Commit 351b2fa

Browse files
authored
Merge pull request #8221 from marmelab/fix-datagrid-exapand-all
Fix `Datagrid`'s `ExpandAllButton` conflicts with `expandSingle` prop
2 parents 4a14dbc + ef27308 commit 351b2fa

File tree

5 files changed

+33
-5
lines changed

5 files changed

+33
-5
lines changed

packages/ra-core/src/controller/list/useExpanded.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export const useExpanded = (
6060
*
6161
* @example
6262
*
63-
* const [expanded, toggleExpanded] = useExpandedMultiple('posts', [123, 124, 125]);
63+
* const [expanded, toggleExpanded] = useExpandAll('posts', [123, 124, 125]);
6464
* const expandIcon = expanded ? ExpandLess : ExpandMore;
6565
* const onExpandClick = () => toggleExpanded();
6666
*/

packages/ra-ui-materialui/src/list/datagrid/Datagrid.spec.tsx

+13
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,19 @@ describe('<Datagrid />', () => {
9696
expect(screen.queryByText('No records to show')).toBeTruthy();
9797
});
9898

99+
it('should not allow to expand all rows when `expandSingle` prop is true', () => {
100+
render(
101+
<Wrapper listContext={contextValue}>
102+
<Datagrid expand={<div>Expanded panel</div>} expandSingle>
103+
<TitleField />
104+
</Datagrid>
105+
</Wrapper>
106+
);
107+
expect(screen.queryAllByTestId('ExpandMoreIcon')).toHaveLength(
108+
defaultData.length
109+
);
110+
});
111+
99112
describe('selecting items with the shift key', () => {
100113
it('should call onSelect with the correct ids when the last selection is after the first', () => {
101114
const Test = ({ selectedIds = [] }) => (

packages/ra-ui-materialui/src/list/datagrid/Datagrid.stories.tsx

+11
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ export const Expand = () => (
108108
</Wrapper>
109109
);
110110

111+
export const ExpandSingle = () => (
112+
<Wrapper>
113+
<Datagrid expand={<ExpandPanel />} expandSingle>
114+
<TextField source="id" />
115+
<TextField source="title" />
116+
<TextField source="author" />
117+
<TextField source="year" />
118+
</Datagrid>
119+
</Wrapper>
120+
);
121+
111122
export const Hover = () => (
112123
<Wrapper>
113124
<Datagrid hover={false}>

packages/ra-ui-materialui/src/list/datagrid/DatagridHeader.tsx

+8-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import clsx from 'clsx';
1515
import DatagridHeaderCell from './DatagridHeaderCell';
1616
import { DatagridClasses } from './useDatagridStyles';
1717
import ExpandAllButton from './ExpandAllButton';
18+
import { useDatagridContext } from './useDatagridContext';
1819

1920
/**
2021
* The default Datagrid Header component.
@@ -34,6 +35,7 @@ export const DatagridHeader = (props: DatagridHeaderProps) => {
3435
const { sort, data, onSelect, selectedIds, setSort } = useListContext(
3536
props
3637
);
38+
const { expandSingle } = useDatagridContext();
3739

3840
const updateSortCallback = useCallback(
3941
event => {
@@ -95,10 +97,12 @@ export const DatagridHeader = (props: DatagridHeaderProps) => {
9597
DatagridClasses.expandHeader
9698
)}
9799
>
98-
<ExpandAllButton
99-
resource={resource}
100-
ids={data.map(record => record.id)}
101-
/>
100+
{!expandSingle ? (
101+
<ExpandAllButton
102+
resource={resource}
103+
ids={data.map(record => record.id)}
104+
/>
105+
) : null}
102106
</TableCell>
103107
)}
104108
{hasBulkActions && selectedIds && (

0 commit comments

Comments
 (0)