ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
74 lines • 2.8 kB
JavaScript
import * as React from 'react';
import { useEditController, } from "./useEditController.js";
import { EditContextProvider } from "./EditContextProvider.js";
import { OptionalResourceContextProvider } from "../../core/index.js";
import { useIsAuthPending } from "../../auth/index.js";
/**
* Call useEditController and put the value in a EditContext
*
* Base class for <Edit> components, without UI.
*
* Accepts any props accepted by useEditController:
* - id: The record identifier
* - resource: The resource
*
* @example // Custom edit layout
*
* const PostEdit = () => (
* <EditBase resource="posts">
* <Grid container>
* <Grid item xs={8}>
* <SimpleForm>
* ...
* </SimpleForm>
* </Grid>
* <Grid item xs={4}>
* Edit instructions...
* </Grid>
* </Grid>
* <div>
* Post related links...
* </div>
* </EditBase>
* );
*/
export const EditBase = ({ authLoading, loading, offline, error, redirectOnError, children, render, ...props }) => {
const hasError = error !== false && error !== undefined;
const controllerProps = useEditController({
...props,
redirectOnError: redirectOnError ?? (hasError ? false : undefined),
});
const isAuthPending = useIsAuthPending({
resource: controllerProps.resource,
action: 'edit',
});
if (!render && !children) {
throw new Error("<EditBase> requires either a 'render' prop or 'children' prop");
}
const { isPaused, isPending, error: errorState } = controllerProps;
const showAuthLoading = isAuthPending &&
!props.disableAuthentication &&
authLoading !== false &&
authLoading !== undefined;
const showLoading = !isPaused &&
((!props.disableAuthentication && isAuthPending) || isPending) &&
loading !== false &&
loading !== undefined;
const showOffline = isPaused && isPending && offline !== false && offline !== undefined;
const showError = errorState && hasError;
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(EditContextProvider, { value: controllerProps }, showAuthLoading
? authLoading
: showLoading
? loading
: showOffline
? offline
: showError
? error
: render
? render(controllerProps)
: children)));
};
//# sourceMappingURL=EditBase.js.map