[inulax] connect API 支持forwardRef参数
This commit is contained in:
parent
b19741dae4
commit
55af46db34
|
@ -17,6 +17,7 @@ import { useState, useContext, useEffect, useRef } from '../../renderer/hooks/Ho
|
||||||
import { createContext } from '../../renderer/components/context/CreateContext';
|
import { createContext } from '../../renderer/components/context/CreateContext';
|
||||||
import { createElement } from '../../external/JSXElement';
|
import { createElement } from '../../external/JSXElement';
|
||||||
import type { ReduxStoreHandler, ReduxAction, BoundActionCreator } from './redux';
|
import type { ReduxStoreHandler, ReduxAction, BoundActionCreator } from './redux';
|
||||||
|
import { forwardRef } from '../../renderer/components/ForwardRef';
|
||||||
|
|
||||||
const DefaultContext = createContext(null);
|
const DefaultContext = createContext(null);
|
||||||
type Context = typeof DefaultContext;
|
type Context = typeof DefaultContext;
|
||||||
|
@ -90,6 +91,11 @@ type MergePropsP<StateProps, DispatchProps, OwnProps, MergedProps> = (
|
||||||
type WrappedComponent<OwnProps> = (props: OwnProps) => ReturnType<typeof createElement>;
|
type WrappedComponent<OwnProps> = (props: OwnProps) => ReturnType<typeof createElement>;
|
||||||
type OriginalComponent<MergedProps> = (props: MergedProps) => ReturnType<typeof createElement>;
|
type OriginalComponent<MergedProps> = (props: MergedProps) => ReturnType<typeof createElement>;
|
||||||
type Connector<OwnProps, MergedProps> = (Component: OriginalComponent<MergedProps>) => WrappedComponent<OwnProps>;
|
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>(
|
export function connect<StateProps, DispatchProps, OwnProps, MergedProps>(
|
||||||
mapStateToProps: MapStateToPropsP<StateProps, OwnProps> = () => ({} as StateProps),
|
mapStateToProps: MapStateToPropsP<StateProps, OwnProps> = () => ({} as StateProps),
|
||||||
|
@ -99,10 +105,7 @@ export function connect<StateProps, DispatchProps, OwnProps, MergedProps>(
|
||||||
dispatchProps,
|
dispatchProps,
|
||||||
ownProps
|
ownProps
|
||||||
): MergedProps => ({ ...stateProps, ...dispatchProps, ...ownProps } as unknown as MergedProps),
|
): MergedProps => ({ ...stateProps, ...dispatchProps, ...ownProps } as unknown as MergedProps),
|
||||||
options?: {
|
options: ConnectOption,
|
||||||
areStatesEqual?: (oldState: any, newState: any) => boolean;
|
|
||||||
context?: Context;
|
|
||||||
}
|
|
||||||
): Connector<OwnProps, MergedProps> {
|
): Connector<OwnProps, MergedProps> {
|
||||||
if (!options) {
|
if (!options) {
|
||||||
options = {};
|
options = {};
|
||||||
|
@ -172,6 +175,13 @@ export function connect<StateProps, DispatchProps, OwnProps, MergedProps>(
|
||||||
return node;
|
return node;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (options.forwardRef) {
|
||||||
|
const forwarded = forwardRef((props, ref) => {
|
||||||
|
return Wrapper({ ...props, ref: ref });
|
||||||
|
});
|
||||||
|
return forwarded as WrappedComponent<OwnProps>;
|
||||||
|
}
|
||||||
|
|
||||||
return Wrapper;
|
return Wrapper;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue