UNPKG

ra-core

Version:

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

105 lines 4.36 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); }; import * as React from 'react'; import { useReferenceArrayInputController, } from "./useReferenceArrayInputController.js"; import { ResourceContextProvider } from "../../core/ResourceContextProvider.js"; import { ChoicesContextProvider } from "../../form/choices/ChoicesContextProvider.js"; /** * An Input component for fields containing a list of references to another resource. * Useful for 'hasMany' relationship. * * @example * The post object has many tags, so the post resource looks like: * { * id: 1234, * tag_ids: [ "1", "23", "4" ] * } * * ReferenceArrayInputBase component fetches the current resources (using * `dataProvider.getMany()`) as well as possible resources (using * `dataProvider.getList()`) in the reference endpoint. It then * delegates rendering to its child component, to which it makes the possible * choices available through the ChoicesContext. * * Use it with a selector component as child, like `<SelectArrayInput>` * or <CheckboxGroupInput>. * * @example * export const PostEdit = () => ( * <Edit> * <SimpleForm> * <ReferenceArrayInputBase source="tag_ids" reference="tags"> * <SelectArrayInput optionText="name" /> * </ReferenceArrayInputBase> * </SimpleForm> * </Edit> * ); * * By default, restricts the possible values to 25. You can extend this limit * by setting the `perPage` prop. * * @example * <ReferenceArrayInputBase * source="tag_ids" * reference="tags" * perPage={100}> * <SelectArrayInput optionText="name" /> * </ReferenceArrayInputBase> * * By default, orders the possible values by id desc. You can change this order * by setting the `sort` prop (an object with `field` and `order` properties). * * @example * <ReferenceArrayInputBase * source="tag_ids" * reference="tags" * sort={{ field: 'name', order: 'ASC' }}> * <SelectArrayInput optionText="name" /> * </ReferenceArrayInputBase> * * Also, you can filter the query used to populate the possible values. Use the * `filter` prop for that. * * @example * <ReferenceArrayInputBase * source="tag_ids" * reference="tags" * filter={{ is_public: true }}> * <SelectArrayInput optionText="name" /> * </ReferenceArrayInputBase> * * The enclosed component may filter results. ReferenceArrayInputBase create a ChoicesContext which provides * a `setFilters` function. You can call this function to filter the results. */ export var ReferenceArrayInputBase = function (props) { var children = props.children, _a = props.filter, filter = _a === void 0 ? defaultFilter : _a, offline = props.offline, reference = props.reference, render = props.render, sort = props.sort; if (children && React.Children.count(children) !== 1) { throw new Error('<ReferenceArrayInputBase> only accepts a single child (like <AutocompleteArrayInput>)'); } if (!render && !children) { throw new Error("<ReferenceArrayInputBase> requires either a 'render' prop or 'children' prop"); } var controllerProps = useReferenceArrayInputController(__assign(__assign({}, props), { sort: sort, filter: filter })); var isPaused = controllerProps.isPaused, isPending = controllerProps.isPending; // isPending is true: there's no cached data and no query attempt was finished yet // isPaused is true: the query was paused (e.g. due to a network issue) // Both true: we're offline and have no data to show var shouldRenderOffline = isPaused && isPending && offline !== undefined && offline !== false; return (React.createElement(ResourceContextProvider, { value: reference }, React.createElement(ChoicesContextProvider, { value: controllerProps }, shouldRenderOffline ? offline : render ? render(controllerProps) : children))); }; var defaultFilter = {}; //# sourceMappingURL=ReferenceArrayInputBase.js.map