Skip to content

Commit

Permalink
feat: 增加高阶组件ref透传
Browse files Browse the repository at this point in the history
  • Loading branch information
SummerOverture committed Dec 29, 2020
1 parent 8b7197a commit 63e19ce
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions source/components/Config/Locale/WithLocale.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ function getDisplayName(WrappedComponent) {
const withLocale = ({ componentName }) =>
<WrappedProps extends {}>(WrappedComponent: React.ComponentType<WrappedProps>): any => {

type WithLocaleProps = WrappedProps;
type WithLocaleProps = WrappedProps & {
forwardRef: any,
};
type WithLocaleState = {
Locale?: object
};

class WithLocale extends Component<WithLocaleProps, WithLocaleState> {
static displayName: string;
constructor (props) {
constructor(props) {
super(props);

this.state = {
Expand All @@ -27,21 +29,27 @@ const withLocale = ({ componentName }) =>
}

render() {
const { forwardRef, ...otherProps } = this.props;
return (
<Consumer componentName={componentName}>
{
(Locale) =>
<WrappedComponent
{...this.props as WrappedProps}
ref={forwardRef}
{...otherProps as WrappedProps}
Locale={Locale}
/>
}
</Consumer>
);
}
}

WithLocale.displayName = `WithLocale(${getDisplayName(WrappedComponent)})`;
return hoistNonReactStatic(WithLocale, WrappedComponent);
return React.forwardRef(function WithLocaleRef(props, ref) {
const WithLocaleRef = hoistNonReactStatic(WithLocale, WrappedComponent)
return <WithLocaleRef {...props as WrappedProps} forwardedRef={ref} />;
});
};

export default withLocale;

0 comments on commit 63e19ce

Please sign in to comment.