Skip to content

Commit

Permalink
fix: type def for forwardRef in injectIntl, fix #1444
Browse files Browse the repository at this point in the history
  • Loading branch information
longlho committed Aug 23, 2019
1 parent e7bd24e commit 45887bf
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions src/components/injectIntl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ const {Consumer: IntlConsumer, Provider: IntlProvider} = IntlContext;
export const Provider = IntlProvider;
export const Context = IntlContext;

export interface Opts<IntlPropName extends string = 'intl'> {
export interface Opts<
IntlPropName extends string = 'intl',
ForwardRef extends boolean = false
> {
intlPropName?: IntlPropName;
forwardRef?: boolean;
forwardRef?: ForwardRef;
enforceContext?: boolean;
}

Expand All @@ -35,12 +38,37 @@ export type WithIntlProps<P> = Omit<P, keyof WrappedComponentProps> & {
};

export default function injectIntl<
IntlPropName extends string = 'intl',
IntlPropName extends string,
P extends WrappedComponentProps<IntlPropName> = WrappedComponentProps<any>
>(
WrappedComponent: React.ComponentType<P>,
options?: Opts<IntlPropName>
): React.ComponentType<WithIntlProps<P>> & {
options?: Opts<IntlPropName, false>
): React.FC<WithIntlProps<P>> & {
WrappedComponent: typeof WrappedComponent;
};
export default function injectIntl<
IntlPropName extends string = 'intl',
P extends WrappedComponentProps<IntlPropName> = WrappedComponentProps<any>,
T extends React.ComponentType<P> = any
>(
WrappedComponent: React.ComponentType<P>,
options?: Opts<IntlPropName, true>
): React.ForwardRefExoticComponent<
React.PropsWithoutRef<WithIntlProps<P>> & React.RefAttributes<T>
> & {
WrappedComponent: typeof WrappedComponent;
};
export default function injectIntl<
IntlPropName extends string = 'intl',
P extends WrappedComponentProps<IntlPropName> = WrappedComponentProps<any>,
ForwardRef extends boolean = false,
T extends React.ComponentType<P> = any
>(
WrappedComponent: React.ComponentType<P>,
options?: Opts<IntlPropName, ForwardRef>
): React.ForwardRefExoticComponent<
React.PropsWithoutRef<WithIntlProps<P>> & React.RefAttributes<T>
> & {
WrappedComponent: typeof WrappedComponent;
} {
const {intlPropName = 'intl', forwardRef = false, enforceContext = true} =
Expand Down Expand Up @@ -72,7 +100,7 @@ export default function injectIntl<

if (forwardRef) {
return hoistNonReactStatics(
React.forwardRef((props: P, ref) => (
React.forwardRef<T, P>((props: P, ref) => (
<WithIntl {...props} forwardedRef={ref} />
)),
WrappedComponent
Expand Down

0 comments on commit 45887bf

Please sign in to comment.