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> ignores children without label or source #8376

Merged
merged 5 commits into from
Nov 10, 2022
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
3 changes: 3 additions & 0 deletions packages/ra-core/src/i18n/TranslationMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ export interface TranslationMessages extends StringMap {
configurable?: {
customize: string;
configureMode: string;
Datagrid: {
unlabeled: string;
};
inspector: {
title: string;
content: string;
Expand Down
3 changes: 3 additions & 0 deletions packages/ra-language-english/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ const englishMessages: TranslationMessages = {
configurable: {
customize: 'Customize',
configureMode: 'Configure this page',
Datagrid: {
unlabeled: 'Unlabeled column #%{column}',
},
inspector: {
title: 'Inspector',
content: 'Hover the application UI elements to configure them',
Expand Down
3 changes: 3 additions & 0 deletions packages/ra-language-french/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ const frenchMessages: TranslationMessages = {
configurable: {
customize: 'Personnaliser',
configureMode: 'Configurer cette page',
Datagrid: {
unlabeled: 'Colonne sans label #%{column}',
},
inspector: {
title: 'Inspecteur',
content: 'Sélectionner un composant pour le configurer',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { Box } from '@mui/material';
import { DatagridConfigurable } from './DatagridConfigurable';
import { Inspector, InspectorButton } from '../../preferences';
import { TextField } from '../../field';
import { EditButton } from '../../button';
import { createTheme, ThemeProvider } from '@mui/material/styles';

export default { title: 'ra-ui-materialui/list/DatagridConfigurable' };

Expand Down Expand Up @@ -39,28 +41,33 @@ const data = [
const AuthorField = () => <TextField source="author" />;
AuthorField.defaultProps = { label: 'Author' };

const theme = createTheme();

export const Basic = () => (
<PreferencesEditorContextProvider>
<MemoryRouter>
<Inspector />
<Box display="flex" justifyContent="flex-end">
<InspectorButton />
</Box>
<Box p={2}>
<DatagridConfigurable
resource="books1"
data={data}
sort={{ field: 'title', order: 'ASC' }}
bulkActionButtons={false}
>
<TextField source="id" />
<TextField source="title" label="Original title" />
<TextField source="author" />
<TextField source="year" />
</DatagridConfigurable>
</Box>
</MemoryRouter>
</PreferencesEditorContextProvider>
<ThemeProvider theme={theme}>
<PreferencesEditorContextProvider>
<MemoryRouter>
<Inspector />
<Box display="flex" justifyContent="flex-end">
<InspectorButton />
</Box>
<Box p={2}>
<DatagridConfigurable
resource="books1"
data={data}
sort={{ field: 'title', order: 'ASC' }}
bulkActionButtons={false}
>
<TextField source="id" />
<TextField source="title" label="Original title" />
<TextField source="author" />
<TextField source="year" />
<EditButton />
</DatagridConfigurable>
</Box>
</MemoryRouter>
</PreferencesEditorContextProvider>
</ThemeProvider>
);

export const Omit = () => (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import * as React from 'react';
import { useResourceContext, usePreference, useStore } from 'ra-core';
import {
useResourceContext,
usePreference,
useStore,
useTranslate,
} from 'ra-core';

import { Configurable } from '../../preferences';
import { Datagrid, DatagridProps } from './Datagrid';
Expand Down Expand Up @@ -39,6 +44,8 @@ export const DatagridConfigurable = ({
const resource = useResourceContext(props);
const finalPreferenceKey = preferenceKey || `${resource}.datagrid`;

const translate = useTranslate();

const [availableColumns, setAvailableColumns] = useStore<
ConfigurableDatagridColumn[]
>(`preferences.${finalPreferenceKey}.availableColumns`, []);
Expand All @@ -52,12 +59,20 @@ 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) &&
(child.props.source || child.props.label)
React.isValidElement(child)
? {
index: String(index),
source: child.props.source,
label: child.props.label,
label:
child.props.source || child.props.label
? child.props.label
: translate(
'ra.configurable.Datagrid.unlabeled',
{
column: index,
_: `Unlabeled column #%{column}`,
}
),
}
: null
).filter(column => column != null);
Expand Down Expand Up @@ -114,7 +129,7 @@ export interface DatagridConfigurableProps extends DatagridProps {

export interface ConfigurableDatagridColumn {
index: string;
source: string;
source?: string;
label?: string;
}

Expand Down