UNPKG

ra-core

Version:

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

38 lines 1.61 kB
import { Children, isValidElement, useRef } from 'react'; import { FormDataConsumer } from "../../form/FormDataConsumer.js"; import { useEvent } from "../../util/index.js"; export const useGetArrayInputNewItemDefaults = (fields) => { const initialDefaultValue = useRef({}); if (fields.length > 0) { const { id, ...rest } = fields[0]; initialDefaultValue.current = rest; for (const k in initialDefaultValue.current) initialDefaultValue.current[k] = null; } return useEvent((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' }]) const defaultValue = initialDefaultValue.current; Children.forEach(inputs, input => { if (isValidElement(input) && input.type !== FormDataConsumer && input.props.source) { defaultValue[input.props.source] = input.props.defaultValue ?? null; } }); return defaultValue; }); }; //# sourceMappingURL=useGetArrayInputNewItemDefaults.js.map