react.ts 629 B

12345678910111213141516171819202122232425262728
  1. import {
  2. forwardRef,
  3. PropsWithoutRef,
  4. ReactElement,
  5. Ref,
  6. RefAttributes,
  7. FunctionComponent,
  8. } from 'react'
  9. export const fixedForwardRef = <
  10. T,
  11. P = object,
  12. A extends Record<string, FunctionComponent> = Record<
  13. string,
  14. FunctionComponent
  15. >,
  16. >(
  17. render: (props: PropsWithoutRef<P>, ref: Ref<T>) => ReactElement | null,
  18. propsToAttach: A = {} as A
  19. ): ((props: P & RefAttributes<T>) => ReactElement | null) & A => {
  20. const ForwardReferredComponent = forwardRef(render) as any
  21. for (const i in propsToAttach) {
  22. ForwardReferredComponent[i] = propsToAttach[i]
  23. }
  24. return ForwardReferredComponent
  25. }