[inulax] connect API 支持forwardRef参数

This commit is contained in:
huangxuan 2023-11-30 15:38:05 +08:00
parent b19741dae4
commit 55af46db34
No known key found for this signature in database
GPG Key ID: E79F50C67022565D
1 changed files with 14 additions and 4 deletions

View File

@ -17,6 +17,7 @@ import { useState, useContext, useEffect, useRef } from '../../renderer/hooks/Ho
import { createContext } from '../../renderer/components/context/CreateContext';
import { createElement } from '../../external/JSXElement';
import type { ReduxStoreHandler, ReduxAction, BoundActionCreator } from './redux';
import { forwardRef } from '../../renderer/components/ForwardRef';
const DefaultContext = createContext(null);
type Context = typeof DefaultContext;
@ -90,6 +91,11 @@ type MergePropsP<StateProps, DispatchProps, OwnProps, MergedProps> = (
type WrappedComponent<OwnProps> = (props: OwnProps) => ReturnType<typeof createElement>;
type OriginalComponent<MergedProps> = (props: MergedProps) => ReturnType<typeof createElement>;
type Connector<OwnProps, MergedProps> = (Component: OriginalComponent<MergedProps>) => WrappedComponent<OwnProps>;
type ConnectOption<State = any> = {
areStatesEqual?: (oldState: State, newState: State) => boolean;
context?: Context;
forwardRef?: boolean
}
export function connect<StateProps, DispatchProps, OwnProps, MergedProps>(
mapStateToProps: MapStateToPropsP<StateProps, OwnProps> = () => ({} as StateProps),
@ -99,10 +105,7 @@ export function connect<StateProps, DispatchProps, OwnProps, MergedProps>(
dispatchProps,
ownProps
): MergedProps => ({ ...stateProps, ...dispatchProps, ...ownProps } as unknown as MergedProps),
options?: {
areStatesEqual?: (oldState: any, newState: any) => boolean;
context?: Context;
}
options: ConnectOption,
): Connector<OwnProps, MergedProps> {
if (!options) {
options = {};
@ -172,6 +175,13 @@ export function connect<StateProps, DispatchProps, OwnProps, MergedProps>(
return node;
};
if (options.forwardRef) {
const forwarded = forwardRef((props, ref) => {
return Wrapper({ ...props, ref: ref });
});
return forwarded as WrappedComponent<OwnProps>;
}
return Wrapper;
};
}