UNPKG

ra-core

Version:

Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React

94 lines 3.95 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import * as React from 'react'; import { useEditController, } from './useEditController'; import { EditContextProvider } from './EditContextProvider'; import { OptionalResourceContextProvider } from '../../core'; import { useIsAuthPending } from '../../auth'; /** * 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 var EditBase = function (_a) { var authLoading = _a.authLoading, loading = _a.loading, offline = _a.offline, error = _a.error, redirectOnError = _a.redirectOnError, children = _a.children, render = _a.render, props = __rest(_a, ["authLoading", "loading", "offline", "error", "redirectOnError", "children", "render"]); var hasError = error !== false && error !== undefined; var controllerProps = useEditController(__assign(__assign({}, props), { redirectOnError: redirectOnError !== null && redirectOnError !== void 0 ? redirectOnError : (hasError ? false : undefined) })); var isAuthPending = useIsAuthPending({ resource: controllerProps.resource, action: 'edit', }); if (!render && !children) { throw new Error("<EditBase> requires either a 'render' prop or 'children' prop"); } var isPaused = controllerProps.isPaused, isPending = controllerProps.isPending, errorState = controllerProps.error; var showAuthLoading = isAuthPending && !props.disableAuthentication && authLoading !== false && authLoading !== undefined; var showLoading = !isPaused && ((!props.disableAuthentication && isAuthPending) || isPending) && loading !== false && loading !== undefined; var showOffline = isPaused && isPending && offline !== false && offline !== undefined; var 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