import React, { ComponentType, Component, ComponentClass } from 'react'; import { default as wrapDisplayName } from 'recompose/wrapDisplayName'; import { default as warning } from '../util/warning'; import { TranslationContextProps, TranslationContext, } from './TranslationContext'; /** * Higher-Order Component for getting access to the `translate` function in props. * * Requires that the app is decorated by the to inject * the translation dictionaries and function in the context. * * @example * import React from 'react'; * import { translate } from 'react-admin'; * * const MyHelloButton = ({ translate }) => ( * * ); * * export default translate(MyHelloButton); * * @param {*} BaseComponent The component to decorate */ const withTranslate = ( BaseComponent: ComponentType ): ComponentClass => { warning( typeof BaseComponent === 'string', `The translate function is a Higher Order Component, and should not be called directly with a translation key. Use the translate function passed as prop to your component props instead: const MyHelloButton = ({ translate }) => ( );` ); const { translate: translateToDiscard, ...defaultProps } = (BaseComponent.defaultProps || {}) as any; class TranslatedComponent extends Component { static defaultProps = defaultProps; static displayName = wrapDisplayName(BaseComponent, 'translate'); render() { return ( {({ translate, locale }) => ( )} ); } } return TranslatedComponent; }; export default withTranslate;