Skip to content

Commit af4e6a8

Browse files
committed
Fix rendering of hidden columns
1 parent 126ca66 commit af4e6a8

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

packages/ra-ui-materialui/src/list/datatable/ColumnsButton.spec.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import * as React from 'react';
22
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
3-
import { Basic, FewColumns, LabelTypes } from './ColumnsButton.stories';
3+
import {
4+
Basic,
5+
FewColumns,
6+
LabelTypes,
7+
NoSource,
8+
} from './ColumnsButton.stories';
49

510
describe('ColumnsButton', () => {
611
it('should render one row per column unless they are hidden', async () => {
@@ -56,4 +61,11 @@ describe('ColumnsButton', () => {
5661
).toHaveLength(8);
5762
});
5863
});
64+
it('should work with columns that have no source', async () => {
65+
render(<NoSource />);
66+
await screen.findByText('c0 value');
67+
fireEvent.click(await screen.findByText('ra.action.select_columns'));
68+
fireEvent.click(await screen.findByLabelText('c_0'));
69+
expect(screen.queryByText('c0 value')).toBeNull();
70+
});
5971
});

packages/ra-ui-materialui/src/list/datatable/ColumnsButton.stories.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,15 @@ export const LabelTypes = () => (
101101
</DataTable>
102102
</Wrapper>
103103
);
104+
105+
export const NoSource = () => (
106+
<Wrapper aside={<ColumnsButton />} actions={null}>
107+
<DataTable bulkActionButtons={false}>
108+
<DataTable.Col label="c_0">c0 value</DataTable.Col>
109+
<DataTable.Col label="c_1">c1 value</DataTable.Col>
110+
<DataTable.Col label="c_2">c2 value</DataTable.Col>
111+
<DataTable.Col label="c_3">c3 value</DataTable.Col>
112+
<DataTable.Col label="c_4">c4 value</DataTable.Col>
113+
</DataTable>
114+
</Wrapper>
115+
);

packages/ra-ui-materialui/src/list/datatable/ColumnsSelectorItem.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,11 @@ export const ColumnsSelectorItem = ({
8484
onToggle={() =>
8585
isColumnHidden
8686
? setHiddenColumns(
87-
hiddenColumns.filter(column => column !== source!)
87+
hiddenColumns.filter(
88+
column => column !== sourceOrLabel!
89+
)
8890
)
89-
: setHiddenColumns([...hiddenColumns, source!])
91+
: setHiddenColumns([...hiddenColumns, sourceOrLabel!])
9092
}
9193
onMove={handleMove}
9294
/>

packages/ra-ui-materialui/src/list/datatable/DataTableCell.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ export const DataTableCell = React.memo(
3939
defaultHiddenColumns
4040
);
4141
const record = useRecordContext();
42-
const isColumnHidden = hiddenColumns.includes(source!);
42+
const isColumnHidden = hiddenColumns.includes(
43+
source ?? (label as string)
44+
);
4345
if (isColumnHidden) return null;
4446
if (!render && !field && !children && !source) {
4547
throw new Error(

packages/ra-ui-materialui/src/list/datatable/DataTableHeadCell.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ export const DataTableHeadCell = React.memo(
6060
const resource = useResourceContext();
6161
const translate = useTranslate();
6262
const translateLabel = useTranslateLabel();
63-
const isColumnHidden = hiddenColumns.includes(source!);
63+
const isColumnHidden = hiddenColumns.includes(
64+
source ?? (label as string)
65+
);
6466
if (isColumnHidden) return null;
6567
const nextSortOrder =
6668
sort && sort.field === source

0 commit comments

Comments
 (0)