From 3e276b4b527d637e11823c0ce81935b89fd687af Mon Sep 17 00:00:00 2001 From: Oliver Lazoroski Date: Sat, 22 Jun 2019 21:43:51 +0200 Subject: [PATCH] [internal] withMessages HOC simplification - remove indirection --- src/lib/withMessages.tsx | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/lib/withMessages.tsx b/src/lib/withMessages.tsx index f7b1b92..de4f6af 100644 --- a/src/lib/withMessages.tsx +++ b/src/lib/withMessages.tsx @@ -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) { - 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) { + if (keyPrefixOrComponent == null || typeof keyPrefixOrComponent === 'string') { + return function wrapWithMessages(Component: React.ComponentType) { + return enhanceWithMessages(keyPrefixOrComponent, Component); + }; + } + + return enhanceWithMessages(undefined, keyPrefixOrComponent); +}