withFormState.tsx 772 B

123456789101112131415161718192021
  1. import React, { forwardRef } from 'react';
  2. import { FormStateContext } from '../context';
  3. import type { FormState } from '@douyinfe/semi-foundation/form/interface';
  4. function withFormState<
  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 WithStateCom = (props: any, ref: React.MutableRefObject<any> | ((instance: any) => void)) => {
  10. return (
  11. <FormStateContext.Consumer>
  12. {(formState: FormState) => <Component formState={formState} ref={ref} {...props} />}
  13. </FormStateContext.Consumer>
  14. );
  15. };
  16. WithStateCom = forwardRef(WithStateCom);
  17. return WithStateCom as R;
  18. }
  19. export default withFormState;