Skip to content

Commit

Permalink
Merge pull request #8546 from marmelab/fix-listguesser-show-link
Browse files Browse the repository at this point in the history
Fix `ListGuesser` links to Edit view even though there is only a ShowView
  • Loading branch information
djhi committed Jan 4, 2023
2 parents d412153 + 2e58707 commit a2c4b0f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
19 changes: 19 additions & 0 deletions packages/ra-ui-materialui/src/list/ListGuesser.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import defaultMessages from 'ra-language-english';
import polyglotI18nProvider from 'ra-i18n-polyglot';

import { ListGuesser } from './ListGuesser';
import { ShowGuesser } from '../detail';
import { AdminUI } from '../AdminUI';
import { AdminContext } from '../AdminContext';

Expand Down Expand Up @@ -93,3 +94,21 @@ export const Basic = () => (
</AdminUI>
</AdminContext>
);

export const LinkedShow = () => (
<AdminContext
dataProvider={dataProvider}
i18nProvider={polyglotI18nProvider(() => defaultMessages, 'en')}
>
<AdminUI>
<Resource
name="products"
list={ListGuesser}
show={ShowGuesser}
recordRepresentation="name"
/>
<Resource name="categories" recordRepresentation="name" />
<Resource name="tags" recordRepresentation="name" />
</AdminUI>
</AdminContext>
);
6 changes: 4 additions & 2 deletions packages/ra-ui-materialui/src/list/ListGuesser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
useListContext,
useResourceContext,
RaRecord,
useResourceDefinition,
} from 'ra-core';

import { ListProps } from './List';
Expand Down Expand Up @@ -71,6 +72,7 @@ export const ListGuesser = <RecordType extends RaRecord = any>(
const ListViewGuesser = (props: Omit<ListViewProps, 'children'>) => {
const { data } = useListContext(props);
const resource = useResourceContext();
const { hasEdit, hasShow } = useResourceDefinition(props);
const [child, setChild] = useState(null);

useEffect(() => {
Expand All @@ -85,7 +87,7 @@ const ListViewGuesser = (props: Omit<ListViewProps, 'children'>) => {
);
const inferredChild = new InferredElement(
listFieldTypes.table,
null,
{ hasEdit, hasShow },
inferredElements
);
setChild(inferredChild.getElement());
Expand Down Expand Up @@ -120,7 +122,7 @@ ${inferredChild.getRepresentation()}
);`
);
}
}, [data, child, resource]);
}, [data, child, resource, hasEdit, hasShow]);

return <ListView {...props}>{child}</ListView>;
};
Expand Down
14 changes: 12 additions & 2 deletions packages/ra-ui-materialui/src/list/listFieldTypes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,18 @@ import {

export const listFieldTypes = {
table: {
component: props => <Datagrid rowClick="edit" {...props} />, // eslint-disable-line react/display-name
representation: (_, children) => ` <Datagrid rowClick="edit">
component: props => {
const { hasEdit, hasShow, ...rest } = props;
return (
<Datagrid
rowClick={!hasEdit && hasShow ? 'show' : 'edit'}
{...rest}
/>
);
}, // eslint-disable-line react/display-name
representation: (props, children) => ` <Datagrid rowClick="${
!props.hasEdit && props.hasShow ? 'show' : 'edit'
}">
${children.map(child => ` ${child.getRepresentation()}`).join('\n')}
</Datagrid>`,
},
Expand Down

0 comments on commit a2c4b0f

Please sign in to comment.