I am new to TypeScript and came across a piece of code which confused me to no end. I am trying to understand what the function componentControl does below but I don't think I fully grasp it.
componentControl:
const componentControl: <P extends object>(
options: BaseProps<P>
) => React.FunctionComponent<P> = <P extends object>(options: BaseProps<P>) => {
const defaultBaseProps = {
onInit: () => Promise.resolve(),
onLoading: () => Promise.resolve(),
onLoaded: () => Promise.resolve(),
onSubmit: () => Promise.resolve(),
onSubmitSuccess: () => Promise.resolve(),
onDestroy: undefined,
onError: () => Promise.resolve(),
onLoadingFailure: () => Promise.resolve(),
onSubmitFailure: () => Promise.resolve(),
InitComponent: () => <PageSpinner />,
LoadingComponent: () => <PageSpinner />,
LoadedComponent: () => <h1>The component has been loaded.</h1>,
SubmittingComponent: () => <PageSpinner />,
SubmitSuccessComponent: () => <h1>Submitted successfully.</h1>,
ErrorComponent: DefaultErrorComponent,
LoadingFailureComponent: DefaultErrorComponent,
SubmitFailureComponent: DefaultErrorComponent,
};
return (wrapped: P) => {
const props: BaseComponentProps<P> = {
base: { ...defaultBaseProps, ...options },
wrapped: wrapped
};
return <BaseComponent {...props} />;
};
};
BaseProps:
export interface BaseProps<P> {
onInit?: (props: P & BaseContextType) => Promise<void>;
onLoading?: (props: P & BaseContextType) => Promise<void>;
onLoaded?: (props: P & BaseContextType) => Promise<void>;
onSubmit?: (
props: P & BaseContextType & { submittedData: unknown }
) => Promise<void>;
onSubmitSuccess?: (props: P & BaseContextType) => Promise<void>;
onDestroy?: (props: P & BaseContextType) => Promise<void>;
onError?: (
props: { err: Error | string | unknown } & BaseContextType
) => Promise<void>;
onLoadingFailure?: (
props: { err: Error | string | unknown } & BaseContextType
) => Promise<void>;
onSubmitFailure?: (
props: { err: Error | string | unknown } & BaseContextType
) => Promise<void>;
InitComponent?: React.FunctionComponent<P>;
LoadingComponent?: React.FunctionComponent<P>;
LoadedComponent?: React.FunctionComponent<P>;
SubmittingComponent?: React.FunctionComponent<P>;
SubmitSuccessComponent?: React.FunctionComponent<P>;
ErrorComponent?: React.FunctionComponent<
P & { err: Error | string | unknown }
>;
LoadingFailureComponent?: React.FunctionComponent<
P & { err: Error | string | unknown }
>;
SubmitFailureComponent?: React.FunctionComponent<
P & { err: Error | string | unknown }
>;
}
BaseComponentProps:
export interface BaseComponentProps<P> {
base: BaseProps<P>;
wrapped: P;
}
If I hover over the function name componentControl in IntelliJ, it shows this: export default function componentControl<P>(options: BaseProps<P>): (wrapped: P) => JSX.Element. What exactly is the input of the this function? Is it the options of type BaseProps or wrapped of type P? What is the use of wrapped? And does this function returns this function below?
(wrapped: P) => {
const props: BaseComponentProps<P> = {
base: { ...defaultBaseProps, ...options },
wrapped: wrapped
};
return <BaseComponent {...props} />;
};
Finally, how am I supposed to use/call this function? Thank you so much for reading.