ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
57 lines • 2.04 kB
JavaScript
import * as React from 'react';
import { useCreateController, } from "./useCreateController.js";
import { CreateContextProvider } from "./CreateContextProvider.js";
import { OptionalResourceContextProvider } from "../../core/index.js";
import { useIsAuthPending } from "../../auth/index.js";
/**
* Call useCreateController and put the value in a CreateContext
*
* Base class for <Create> components, without UI.
*
* Accepts any props accepted by useCreateController:
* - id: The record identifier
* - resource: The resource
*
* @example // Custom edit layout
*
* const PostCreate = () => (
* <CreateBase>
* <Grid container>
* <Grid item xs={8}>
* <SimpleForm>
* ...
* </SimpleForm>
* </Grid>
* <Grid item xs={4}>
* Create instructions...
* </Grid>
* </Grid>
* <div>
* Post related links...
* </div>
* </CreateBase>
* );
*/
export const CreateBase = ({ children, render, loading, authLoading = loading, ...props }) => {
const controllerProps = useCreateController(props);
const isAuthPending = useIsAuthPending({
resource: controllerProps.resource,
action: 'create',
});
if (!render && !children) {
throw new Error('<CreateBase> requires either a `render` prop or `children` prop');
}
const showAuthLoading = isAuthPending &&
!props.disableAuthentication &&
authLoading !== false &&
authLoading !== undefined;
return (
// We pass props.resource here as we don't need to create a new ResourceContext if the props is not provided
React.createElement(OptionalResourceContextProvider, { value: props.resource },
React.createElement(CreateContextProvider, { value: controllerProps }, showAuthLoading
? authLoading
: render
? render(controllerProps)
: children)));
};
//# sourceMappingURL=CreateBase.js.map