withFormApi.tsx 763 B

123456789101112131415161718192021
  1. import React, { forwardRef } from 'react';
  2. import { FormApiContext } from '../context';
  3. import type { BaseFormApi as FormApi } from '@douyinfe/semi-foundation/form/interface';
  4. function withFormApi<
  5. C extends React.ElementType,
  6. T extends React.ComponentProps<C> & React.RefAttributes<any>,
  7. R extends React.ComponentType<T>
  8. >(Component: C) {
  9. let WithApiCom = (props: any, ref: React.MutableRefObject<any> | ((instance: any) => void)) => {
  10. return (
  11. <FormApiContext.Consumer>
  12. { (formApi: FormApi) => (<Component formApi={formApi} ref={ref} {...props} />) }
  13. </FormApiContext.Consumer>
  14. );
  15. };
  16. WithApiCom = forwardRef(WithApiCom);
  17. return WithApiCom as R;
  18. }
  19. export default withFormApi;