From af283308ed0ce21f9064331fadbba45b24ae83a9 Mon Sep 17 00:00:00 2001 From: Lorenzo Natali Date: Thu, 4 Apr 2019 15:09:37 +0200 Subject: [PATCH] Improved doc --- web/client/components/I18N/enhancers/addI18NProps.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/web/client/components/I18N/enhancers/addI18NProps.js b/web/client/components/I18N/enhancers/addI18NProps.js index e1a384fa19..ff33652efb 100644 --- a/web/client/components/I18N/enhancers/addI18NProps.js +++ b/web/client/components/I18N/enhancers/addI18NProps.js @@ -8,7 +8,7 @@ const {injectIntl} = require('react-intl'); const PropTypes = require('prop-types'); const {omit} = require('lodash'); -const { compose, branch, getContext, withProps, mapProps } = require('recompose'); +const { compose, branch, getContext, withProps, withPropsOnChange, mapProps } = require('recompose'); const omitProps = keys => mapProps(props => omit(props, keys)); @@ -30,19 +30,25 @@ const defaults = { }; /** + * Add i18n functionalities and properties as props. Useful to get format functions when the react-intl components can not be used (i.e. with wrapped libs) * @name addI18NFormat - * @param {string[]} props add the props to format as props. One of these https://github.com/yahoo/react-intl/wiki/API#intlshape + * @param {string[]} props add the props to format as props. Should be keys of of this interface https://github.com/yahoo/react-intl/wiki/API#intlshape + * @example + * addI18NProps(['formatNumber'])(MyCmp); // MyCmp will receive `formatNumber` from current locale Intl object as a property */ module.exports = (propsToAdd = []) => compose( + // check intl and inject (or add default dummy object) getContext({intl: PropTypes.object}), branch( ({intl}) => !!intl, injectIntl, withProps({intl: defaults}) ), - withProps(({ intl = {} }) => propsToAdd.reduce((acc = {}, k) => ({ + // add propsToAdd properties from intl object + withPropsOnChange(['intl'], ({ intl = {} }) => propsToAdd.reduce((acc = {}, k) => ({ ...acc, [k]: intl[k] }), {})), + // clean up intl property omitProps(['intl']) );