UNPKG

ra-core

Version:

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

170 lines 7.71 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; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.getArrayInputError = exports.ArrayInputBase = void 0; const React = __importStar(require("react")); const react_1 = require("react"); const react_hook_form_1 = require("react-hook-form"); const validate_1 = require("../../form/validation/validate.cjs"); const useGetValidationErrorMessage_1 = require("../../form/validation/useGetValidationErrorMessage.cjs"); const useApplyInputDefaultValues_1 = require("../../form/useApplyInputDefaultValues.cjs"); const useFormGroupContext_1 = require("../../form/groups/useFormGroupContext.cjs"); const useFormGroups_1 = require("../../form/groups/useFormGroups.cjs"); const core_1 = require("../../core/index.cjs"); const ArrayInputContext_1 = require("./ArrayInputContext.cjs"); /** * To edit arrays of data embedded inside a record, <ArrayInputBase> creates a list of sub-forms. * * @example * * import { ArrayInputBase } from 'ra-core'; * import { SimpleFormIterator, DateInput, TextInput } from 'my-react-admin-ui'; * * <ArrayInputBase source="backlinks"> * <SimpleFormIterator> * <DateInput source="date" /> * <TextInput source="url" /> * </SimpleFormIterator> * </ArrayInputBase> * * <ArrayInputBase> allows the edition of embedded arrays, like the backlinks field * in the following post record: * * { * id: 123 * backlinks: [ * { * date: '2012-08-10T00:00:00.000Z', * url: 'http://example.com/foo/bar.html', * }, * { * date: '2012-08-14T00:00:00.000Z', * url: 'https://blog.johndoe.com/2012/08/12/foobar.html', * } * ] * } * * <ArrayInputBase> expects a single child, which must be a *form iterator* component. * A form iterator is a component accepting a fields object as passed by * react-hook-form-arrays's useFieldArray() hook, and defining a layout for * an array of fields. * * @see {@link https://react-hook-form.com/docs/usefieldarray} */ const ArrayInputBase = (props) => { const { children, defaultValue = [], resource: resourceFromProps, source: arraySource, validate, } = props; const formGroupName = (0, useFormGroupContext_1.useFormGroupContext)(); const formGroups = (0, useFormGroups_1.useFormGroups)(); const parentSourceContext = (0, core_1.useSourceContext)(); const finalSource = parentSourceContext.getSource(arraySource); const sanitizedValidate = Array.isArray(validate) ? (0, validate_1.composeSyncValidators)(validate) : validate; const getValidationErrorMessage = (0, useGetValidationErrorMessage_1.useGetValidationErrorMessage)(); const { getValues } = (0, react_hook_form_1.useFormContext)(); const fieldProps = (0, react_hook_form_1.useFieldArray)({ name: finalSource, rules: { validate: async (value) => { if (!sanitizedValidate) return true; const error = await sanitizedValidate(value, getValues(), props); if (!error) return true; return getValidationErrorMessage(error); }, }, }); (0, react_1.useEffect)(() => { if (formGroups && formGroupName != null) { formGroups.registerField(finalSource, formGroupName); } return () => { if (formGroups && formGroupName != null) { formGroups.unregisterField(finalSource, formGroupName); } }; }, [finalSource, formGroups, formGroupName]); (0, useApplyInputDefaultValues_1.useApplyInputDefaultValues)({ inputProps: { ...props, defaultValue }, isArrayInput: true, fieldArrayInputControl: fieldProps, }); // The SourceContext will be read by children of ArrayInput to compute their composed source and label // // <ArrayInput source="orders" /> => SourceContext is "orders" // <SimpleFormIterator> => SourceContext is "orders.0" // <DateInput source="date" /> => final source for this input will be "orders.0.date" // </SimpleFormIterator> // </ArrayInput> // const sourceContext = React.useMemo(() => ({ // source is the source of the ArrayInput child getSource: (source) => { if (!source) { // SimpleFormIterator calls getSource('') to get the arraySource return parentSourceContext.getSource(arraySource); } // We want to support nesting and composition with other inputs (e.g. TranslatableInputs, ReferenceOneInput, etc), // we must also take into account the parent SourceContext // // <ArrayInput source="orders" /> => SourceContext is "orders" // <SimpleFormIterator> => SourceContext is "orders.0" // <DateInput source="date" /> => final source for this input will be "orders.0.date" // <ArrayInput source="items" /> => SourceContext is "orders.0.items" // <SimpleFormIterator> => SourceContext is "orders.0.items.0" // <TextInput source="reference" /> => final source for this input will be "orders.0.items.0.reference" // </SimpleFormIterator> // </ArrayInput> // </SimpleFormIterator> // </ArrayInput> return parentSourceContext.getSource(`${arraySource}.${source}`); }, // if Array source is items, and child source is name, .0.name => resources.orders.fields.items.name getLabel: (source) => parentSourceContext.getLabel(`${arraySource}.${source}`), }), [parentSourceContext, arraySource]); return (React.createElement(ArrayInputContext_1.ArrayInputContext.Provider, { value: fieldProps }, React.createElement(core_1.OptionalResourceContextProvider, { value: resourceFromProps }, React.createElement(core_1.SourceContextProvider, { value: sourceContext }, children)))); }; exports.ArrayInputBase = ArrayInputBase; const getArrayInputError = error => { if (Array.isArray(error)) { return undefined; } return error; }; exports.getArrayInputError = getArrayInputError; //# sourceMappingURL=ArrayInputBase.js.map