-
Notifications
You must be signed in to change notification settings - Fork 76
/
connectToForm.tsx
34 lines (29 loc) 路 1.07 KB
/
connectToForm.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import React from 'react';
import finishHOC from '@airbnb/lunar/lib/utils/finishHOC';
import useFormField, {
Options as BaseOptions,
FieldProps,
FieldReturnProps,
FieldInternalProps,
FieldProvidedProps,
} from '../hooks/useFormField';
if (process.env.NODE_ENV === 'development') {
// eslint-disable-next-line no-console
console.warn(
'`connectToForm` composer is deprecated. Please migrate to the `useFormField` hook.',
);
}
export type Options<T> = BaseOptions<T>;
export type ConnectToFormProps<T> = FieldProvidedProps<T>;
export type ConnectToFormWrapperProps<T> = FieldInternalProps<T>;
export default function connectToForm<T>(options: Options<T>) /* infer */ {
return function connectToFormFactory<P>(
WrappedComponent: React.ComponentType<FieldReturnProps<T, P>>,
): React.ComponentType<FieldProps<T, P>> {
function ConnectToForm(props: FieldProps<T, P>) {
const fieldProps = useFormField<T, P>(props, options);
return <WrappedComponent {...fieldProps} />;
}
return finishHOC('connectToForm', ConnectToForm, WrappedComponent);
};
}