ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
42 lines • 1.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useGetArrayInputNewItemDefaults = void 0;
const react_1 = require("react");
const FormDataConsumer_1 = require("../../form/FormDataConsumer.cjs");
const util_1 = require("../../util/index.cjs");
const useGetArrayInputNewItemDefaults = (fields) => {
const initialDefaultValue = (0, react_1.useRef)({});
if (fields.length > 0) {
const { id, ...rest } = fields[0];
initialDefaultValue.current = rest;
for (const k in initialDefaultValue.current)
initialDefaultValue.current[k] = null;
}
return (0, util_1.useEvent)((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' }])
const defaultValue = initialDefaultValue.current;
react_1.Children.forEach(inputs, input => {
if ((0, react_1.isValidElement)(input) &&
input.type !== FormDataConsumer_1.FormDataConsumer &&
input.props.source) {
defaultValue[input.props.source] =
input.props.defaultValue ?? null;
}
});
return defaultValue;
});
};
exports.useGetArrayInputNewItemDefaults = useGetArrayInputNewItemDefaults;
//# sourceMappingURL=useGetArrayInputNewItemDefaults.js.map