Skip to content

Commit

Permalink
[internal] withMessages HOC simplification - remove indirection
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverlaz committed Jun 22, 2019
1 parent 3b920c7 commit 3e276b4
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions src/lib/withMessages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,23 @@ function enhanceWithMessages(keyPrefix: string | undefined, WrappedComponent: Re
}

/**
* An internal implementation of the argument resolution logic.
* Creates a higher order component and provides the MessageSourceApi as `props`.
*
* @param keyPrefixOrComponent the passed argument ([Component] | [[keyPrefix], [Component]])
*/
function internalWithMessages(keyPrefixOrComponent?: string | React.ComponentType<any>) {
if (keyPrefixOrComponent == null || typeof keyPrefixOrComponent === 'string') {
// consumer used the curried API
return enhanceWithMessages.bind(undefined, keyPrefixOrComponent);
}

return enhanceWithMessages(undefined, keyPrefixOrComponent);
}

/**
* Example usages:
*
* 1. MessageSource.withMessages('keyPrefix')(Component)
* 2. MessageSource.withMessages(Component)
* 3. compose(MessageSource.withMessages('keyPrefix'))(Component)
* 4. compose(MessageSource.withMessages)(Component)
*
* @param keyPrefixOrComponent the passed argument ([Component] | [[keyPrefix], [Component]])
*/
export const withMessages = internalWithMessages;
export function withMessages(keyPrefixOrComponent?: string | React.ComponentType<any>) {
if (keyPrefixOrComponent == null || typeof keyPrefixOrComponent === 'string') {
return function wrapWithMessages(Component: React.ComponentType<any>) {
return enhanceWithMessages(keyPrefixOrComponent, Component);
};
}

return enhanceWithMessages(undefined, keyPrefixOrComponent);
}

0 comments on commit 3e276b4

Please sign in to comment.