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 EditGuesser is broken #5728

Merged
merged 1 commit into from
Jan 4, 2021
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
4 changes: 2 additions & 2 deletions packages/ra-core/src/core/useResourceContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ResourceContext, ResourceContextValue } from './ResourceContext';
export const useResourceContext = <
ResourceInformationsType extends Partial<{ resource: string }>
>(
props: ResourceInformationsType
props?: ResourceInformationsType
): ResourceContextValue => {
const context = useContext(ResourceContext);

Expand All @@ -29,7 +29,7 @@ export const useResourceContext = <
// );
}
// Ignored because resource is often optional (as it is injected) in components which passes the props to this hook
return props.resource;
return props && props.resource;
}

return context;
Expand Down
17 changes: 13 additions & 4 deletions packages/ra-ui-materialui/src/detail/EditGuesser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ import { useEffect, useState } from 'react';
import inflection from 'inflection';
import {
useEditController,
EditContextProvider,
InferredElement,
useResourceContext,
useEditContext,
getElementsFromRecords,
} from 'ra-core';

import { EditView } from './EditView';
import editFieldTypes from './editFieldTypes';

const EditViewGuesser = props => {
const { record, resource } = props;
const resource = useResourceContext();
const { record } = useEditContext();
const [inferredChild, setInferredChild] = useState(null);
useEffect(() => {
if (record && !inferredChild) {
Expand Down Expand Up @@ -47,8 +51,13 @@ ${inferredChild.getRepresentation()}

EditViewGuesser.propTypes = EditView.propTypes;

const EditGuesser = props => (
<EditViewGuesser {...props} {...useEditController(props)} />
);
const EditGuesser = props => {
const controllerProps = useEditController(props);
return (
<EditContextProvider value={controllerProps}>
<EditViewGuesser {...props} />
</EditContextProvider>
);
};

export default EditGuesser;