ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
50 lines • 2.2 kB
JavaScript
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 { Children, isValidElement, useRef } from 'react';
import { FormDataConsumer } from "../../form/FormDataConsumer.js";
import { useEvent } from "../../util/index.js";
export var useGetArrayInputNewItemDefaults = function (fields) {
var initialDefaultValue = 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 useEvent(function (inputs) {
if (Children.count(inputs) === 1 &&
isValidElement(Children.only(inputs)) &&
// @ts-ignore
!Children.only(inputs).props.source &&
// Make sure it's not a FormDataConsumer
// @ts-ignore
Children.only(inputs).type !== 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;
Children.forEach(inputs, function (input) {
var _a;
if (isValidElement(input) &&
input.type !== FormDataConsumer &&
input.props.source) {
defaultValue[input.props.source] =
(_a = input.props.defaultValue) !== null && _a !== void 0 ? _a : null;
}
});
return defaultValue;
});
};
//# sourceMappingURL=useGetArrayInputNewItemDefaults.js.map