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 { useShowController, } from "./useShowController.js";
import { ShowContextProvider } from "./ShowContextProvider.js";
import { OptionalResourceContextProvider } from "../../core/index.js";
import { useIsAuthPending } from "../../auth/index.js";
/**
* Call useShowController and put the value in a ShowContext
*
* Base class for <Show> components, without UI.
*
* Accepts any props accepted by useShowController:
* - id: The record identifier
* - resource: The resource
*
* @example // Custom show layout
*
* const PostShow = () => (
* <ShowBase resource="posts">
* <Grid container>
* <Grid item xs={8}>
* <SimpleForm>
* ...
* </SimpleForm>
* </Grid>
* <Grid item xs={4}>
* Show instructions...
* </Grid>
* </Grid>
* <div>
* Post related links...
* </div>
* </ShowBase>
* );
*/
export const ShowBase = ({ authLoading, loading, offline, error, redirectOnError, children, render, ...props }) => {
const hasError = error !== false && error !== undefined;
const controllerProps = useShowController({
...props,
redirectOnError: redirectOnError ?? (hasError ? false : undefined),
});
const isAuthPending = useIsAuthPending({
resource: controllerProps.resource,
action: 'show',
});
if (!render && !children) {
throw new Error('<ShowBase> 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(ShowContextProvider, { value: controllerProps }, showAuthLoading
? authLoading
: showLoading
? loading
: showOffline
? offline
: showError
? error
: render
? render(controllerProps)
: children)));
};
//# sourceMappingURL=ShowBase.js.map