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

Fix <DatagridConfigurable> inspector hides the wrong column when using empty children #8929

Merged
merged 1 commit into from
May 22, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Omit,
PreferenceKey,
LabelElement,
NullChildren,
} from './DatagridConfigurable.stories';

describe('<DatagridConfigurable>', () => {
Expand Down Expand Up @@ -49,6 +50,19 @@ describe('<DatagridConfigurable>', () => {
screen.getByLabelText('Title').click();
expect(screen.queryByText('War and Peace')).not.toBeNull();
});
it('accepts null children', async () => {
render(<NullChildren />);
screen.getByLabelText('Configure mode').click();
await screen.findByText('Inspector');
fireEvent.mouseOver(screen.getByText('Leo Tolstoy'));
await screen.getByTitle('ra.configurable.customize').click();
await screen.findByText('Datagrid');
expect(screen.queryByText('War and Peace')).not.toBeNull();
screen.getByLabelText('Original title').click();
expect(screen.queryByText('War and Peace')).toBeNull();
screen.getByLabelText('Original title').click();
expect(screen.queryByText('War and Peace')).not.toBeNull();
});
it('should accept fields with no source', async () => {
render(<Basic />);
screen.getByLabelText('Configure mode').click();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,20 @@ export const LabelElement = () => (
</DatagridConfigurable>
</Wrapper>
);

export const NullChildren = () => (
<Wrapper>
<DatagridConfigurable
resource="books1"
data={data}
sort={{ field: 'title', order: 'ASC' }}
bulkActionButtons={false}
>
{false && <TextField source="id" />}
<TextField source="title" label="Original title" />
<TextField source="author" />
<TextField source="year" />
<EditButton />
</DatagridConfigurable>
</Wrapper>
);
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,23 @@ export const DatagridConfigurable = ({

React.useEffect(() => {
// first render, or the preference have been cleared
const columns = React.Children.map(props.children, (child, index) =>
React.isValidElement(child)
? {
index: String(index),
source: child.props.source,
label:
child.props.label &&
typeof child.props.label === 'string' // this list is serializable, so we can't store ReactElement in it
? child.props.label
: child.props.source
? // force the label to be the source
undefined
: // no source or label, generate a label
translate(
'ra.configurable.Datagrid.unlabeled',
{
column: index,
_: `Unlabeled column #%{column}`,
}
),
}
: null
).filter(column => column != null);
const columns = React.Children.toArray(props.children)
.filter(child => React.isValidElement(child))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I basically made the filter before the map, so that we don't remove columns (which would break the indexing), but instead never create the columns for invalid children.

.map((child: React.ReactElement, index) => ({
index: String(index),
source: child.props.source,
label:
child.props.label && typeof child.props.label === 'string' // this list is serializable, so we can't store ReactElement in it
? child.props.label
: child.props.source
? // force the label to be the source
undefined
: // no source or label, generate a label
translate('ra.configurable.Datagrid.unlabeled', {
column: index,
_: `Unlabeled column #%{column}`,
}),
}));
if (columns.length !== availableColumns.length) {
setAvailableColumns(columns);
setOmit(omit);
Expand Down