import React, { Children, PropsWithChildren } from 'react';
import { ReferenceInput as RaReferenceInput, ReferenceInputProps as RaReferenceInputProps } from 'react-admin';

type ReferenceInputProps = PropsWithChildren<Record<string, never>> & RaReferenceInputProps;

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

export { ReferenceInput };
export type { ReferenceInputProps };
