UNPKG

ra-core

Version:

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

54 lines 2.49 kB
"use strict"; 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.useGetArrayInputNewItemDefaults = void 0; var react_1 = require("react"); var FormDataConsumer_1 = require("../../form/FormDataConsumer.cjs"); var util_1 = require("../../util/index.cjs"); var useGetArrayInputNewItemDefaults = function (fields) { var initialDefaultValue = (0, react_1.useRef)({}); if (fields.length > 0) { var _a = fields[0], id = _a.id, rest = __rest(_a, ["id"]); initialDefaultValue.current = rest; for (var k in initialDefaultValue.current) initialDefaultValue.current[k] = null; } return (0, util_1.useEvent)(function (inputs) { if (react_1.Children.count(inputs) === 1 && (0, react_1.isValidElement)(react_1.Children.only(inputs)) && // @ts-ignore !react_1.Children.only(inputs).props.source && // Make sure it's not a FormDataConsumer // @ts-ignore react_1.Children.only(inputs).type !== FormDataConsumer_1.FormDataConsumer) { // ArrayInput used for an array of scalar values // (e.g. tags: ['foo', 'bar']) return ''; } // ArrayInput used for an array of objects // (e.g. authors: [{ firstName: 'John', lastName: 'Doe' }, { firstName: 'Jane', lastName: 'Doe' }]) var defaultValue = initialDefaultValue.current; react_1.Children.forEach(inputs, function (input) { var _a; if ((0, react_1.isValidElement)(input) && input.type !== FormDataConsumer_1.FormDataConsumer && input.props.source) { defaultValue[input.props.source] = (_a = input.props.defaultValue) !== null && _a !== void 0 ? _a : null; } }); return defaultValue; }); }; exports.useGetArrayInputNewItemDefaults = useGetArrayInputNewItemDefaults; //# sourceMappingURL=useGetArrayInputNewItemDefaults.js.map