From 0636d8dab05abd839e4bbbaae6ee45729531deed Mon Sep 17 00:00:00 2001 From: wangxinrong <18629193282@163.com> Date: Wed, 1 Nov 2023 21:44:43 +0800 Subject: [PATCH] =?UTF-8?q?[inulax]=20connect=20API=20=E6=94=AF=E6=8C=81fo?= =?UTF-8?q?rwardRef=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../inula-router/src/router/withRouter.tsx | 5 +++-- packages/inula/package.json | 10 +-------- .../inula/src/inulax/adapters/reduxReact.ts | 21 ++++++++++++++----- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/packages/inula-router/src/router/withRouter.tsx b/packages/inula-router/src/router/withRouter.tsx index 3d7f58bd..43f7dc01 100644 --- a/packages/inula-router/src/router/withRouter.tsx +++ b/packages/inula-router/src/router/withRouter.tsx @@ -20,13 +20,14 @@ import RouterContext from './context'; function withRouter(Component: C) { function ComponentWithRouterProp(props: any) { + const { wrappedComponentRef, ...rest } = props; const { history, location, match } = useContext(RouterContext); const routeProps = { history: history, location: location, match: match }; - return ; + return ; } return ComponentWithRouterProp; } -export default withRouter; \ No newline at end of file +export default withRouter; diff --git a/packages/inula/package.json b/packages/inula/package.json index 38de80bf..3ec98d8b 100644 --- a/packages/inula/package.json +++ b/packages/inula/package.json @@ -24,13 +24,5 @@ "watch-test": "yarn test --watch --dev" }, "files": ["build/@types", "build/cjs", "build/umd", "build/index.js", "build/jsx-dev-runtime.js", "build/jsx-runtime.js", "README.md"], - "types": "./build/@types/index.d.ts", - "exports": { - ".": { - "default": "./index.js" - }, - "./package.json": "./package.json", - "./jsx-runtime": "./jsx-runtime.js", - "./jsx-dev-runtime": "./jsx-dev-runtime.js" - } + "types": "./build/@types/index.d.ts" } diff --git a/packages/inula/src/inulax/adapters/reduxReact.ts b/packages/inula/src/inulax/adapters/reduxReact.ts index 51650f98..203622f3 100644 --- a/packages/inula/src/inulax/adapters/reduxReact.ts +++ b/packages/inula/src/inulax/adapters/reduxReact.ts @@ -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 = ( type WrappedComponent = (props: OwnProps) => ReturnType; type OriginalComponent = (props: MergedProps) => ReturnType; type Connector = (Component: OriginalComponent) => WrappedComponent; +type ConnectOption = { + areStatesEqual?: (oldState: State, newState: State) => boolean; + context?: Context; + forwardRef?: boolean +} export function connect( mapStateToProps: MapStateToPropsP = () => ({} as StateProps), @@ -97,12 +103,9 @@ export function connect( mergeProps: MergePropsP = ( stateProps, dispatchProps, - ownProps + ownProps, ): MergedProps => ({ ...stateProps, ...dispatchProps, ...ownProps } as unknown as MergedProps), - options?: { - areStatesEqual?: (oldState: any, newState: any) => boolean; - context?: Context; - } + options: ConnectOption, ): Connector { if (!options) { options = {}; @@ -159,6 +162,7 @@ export function connect( mappedDispatch = mapDispatchToProps(store.dispatch, props); } } + mappedDispatch = Object.assign({}, mappedDispatch, { dispatch: store.dispatch }); const mergedProps = ( mergeProps || ((state, dispatch, originalProps) => { @@ -172,6 +176,13 @@ export function connect( return node; }; + if (options.forwardRef) { + const forwarded = forwardRef(function (props, ref) { + return Wrapper({ ...props, ref: ref }); + }); + return forwarded as WrappedComponent; + } + return Wrapper; }; }