UNPKG

ra-core

Version:

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

149 lines 6.73 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ListBase = void 0; var React = __importStar(require("react")); var useListController_1 = require("./useListController.cjs"); var core_1 = require("../../core/index.cjs"); var ListContextProvider_1 = require("./ListContextProvider.cjs"); var auth_1 = require("../../auth/index.cjs"); /** * Call useListController and put the value in a ListContext * * Base class for <List> components, without UI. * * Accepts any props accepted by useListController: * - filter: permanent filter applied to the list * - filters: Filter element, to display the filters * - filterDefaultValues: object; * - perPage: Number of results per page * - sort: Default sort * - exporter: exported function * * @example // Custom list layout * * const PostList = () => ( * <ListBase perPage={10}> * <div> * List metrics... * </div> * <Grid container> * <Grid item xs={8}> * <SimpleList primaryText={record => record.title} /> * </Grid> * <Grid item xs={4}> * List instructions... * </Grid> * </Grid> * <div> * Post related links... * </div> * </ListBase> * ); */ var ListBase = function (_a) { var children = _a.children, emptyWhileLoading = _a.emptyWhileLoading, authLoading = _a.authLoading, loading = _a.loading, offline = _a.offline, error = _a.error, empty = _a.empty, render = _a.render, props = __rest(_a, ["children", "emptyWhileLoading", "authLoading", "loading", "offline", "error", "empty", "render"]); var controllerProps = (0, useListController_1.useListController)(props); var isAuthPending = (0, auth_1.useIsAuthPending)({ resource: controllerProps.resource, action: 'list', }); if (!render && !children) { throw new Error("<ListBase> requires either a 'render' prop or 'children' prop"); } var isPaused = controllerProps.isPaused, isPending = controllerProps.isPending, isPlaceholderData = controllerProps.isPlaceholderData, errorState = controllerProps.error, data = controllerProps.data, total = controllerProps.total, hasPreviousPage = controllerProps.hasPreviousPage, hasNextPage = controllerProps.hasNextPage, filterValues = controllerProps.filterValues; var showAuthLoading = isAuthPending && !props.disableAuthentication && authLoading !== false && authLoading !== undefined; var showLoading = !isPaused && ((!props.disableAuthentication && isAuthPending) || isPending) && loading !== false && loading !== undefined; var showOffline = isPaused && // If isPending and isPaused are true, we are offline and couldn't even load the initial data // If isPaused and isPlaceholderData are true, we are offline and couldn't even load data with different parameters on the same useQuery observer (isPending || isPlaceholderData) && offline !== false && offline !== undefined; var showError = errorState && error !== false && error !== undefined; var showEmptyWhileLoading = isPending && !showOffline && emptyWhileLoading === true; var showEmpty = !errorState && // the list is not loading data for the first time !isPending && // the API returned no data (using either normal or partial pagination) (total === 0 || (total == null && hasPreviousPage === false && hasNextPage === false && // @ts-ignore FIXME total may be undefined when using partial pagination but the ListControllerResult type is wrong about it data.length === 0)) && // the user didn't set any filters !Object.keys(filterValues).length && // there is an empty page component empty !== undefined && empty !== false; return ( // We pass props.resource here as we don't need to create a new ResourceContext if the props is not provided React.createElement(core_1.OptionalResourceContextProvider, { value: props.resource }, React.createElement(ListContextProvider_1.ListContextProvider, { value: controllerProps }, showAuthLoading ? authLoading : showLoading ? loading : showOffline ? offline : showError ? error : showEmptyWhileLoading ? null : showEmpty ? empty : render ? render(controllerProps) : children))); }; exports.ListBase = ListBase; //# sourceMappingURL=ListBase.js.map