Skip to content

Commit

Permalink
Improved doc
Browse files Browse the repository at this point in the history
  • Loading branch information
offtherailz committed Apr 4, 2019
1 parent 2e6ecc2 commit af28330
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions web/client/components/I18N/enhancers/addI18NProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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'])
);

0 comments on commit af28330

Please sign in to comment.