import React, { Children } from 'react';
import {
  ReferenceArrayInput as RaReferenceArrayInput,
  ReferenceArrayInputProps as RaReferenceArrayInputProps
} from 'react-admin';

type ReferenceArrayInputProps = RaReferenceArrayInputProps;

function ReferenceArrayInput({ children, ...props }: ReferenceArrayInputProps): JSX.Element {
  const Child = Children.only(children);
  return (
    <RaReferenceArrayInput {...props}>
      {React.cloneElement(Child!, {
        ...props,
        source: Child!.props.source || props.source
      })}
    </RaReferenceArrayInput>
  );
}

export { ReferenceArrayInput };
export type { ReferenceArrayInputProps };
