From 5847defd122e237f2372ce96027a7d4f91781c28 Mon Sep 17 00:00:00 2001 From: chaosmaker Date: Fri, 22 May 2020 16:21:39 +0100 Subject: [PATCH] Merge option in mapAST (#1120) * Added lodash and merge option in mapAST * Update Trans.js Added !== instead of != * Update Trans.js Remove full lodash loading * Update package.json change to lodash.merge * Update package.json Remove lodash * Update Trans.js Slimline prop match. Only accept new values, if node values in translation weren't set. * Update Trans.js Fix camelCase and remove lodash loading * Add Tests * trans debug * Updated tests * more tests * Comment out console.log in test --- src/Trans.js | 11 ++++++++++- test/i18n.js | 2 ++ test/trans.render.spec.js | 40 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/Trans.js b/src/Trans.js index 5cc98a107..dfa81cb99 100644 --- a/src/Trans.js +++ b/src/Trans.js @@ -21,6 +21,13 @@ function getAsArray(data) { return Array.isArray(data) ? data : [data]; } +function mergeProps(source, target){ + const newTarget = {...target}; + // overwrite source.props when target.props already set + newTarget.props = Object.assign(source.props, target.props); + return newTarget; +} + export function nodesToString(children, i18nOptions) { if (!children) return ''; let stringNode = ''; @@ -132,7 +139,9 @@ function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts) { return astNodes.reduce((mem, node, i) => { const translationContent = node.children && node.children[0] && node.children[0].content; if (node.type === 'tag') { - const child = reactNodes[parseInt(node.name, 10)] || {}; + const tmp = reactNodes[parseInt(node.name, 10)] || {}; + const child = Object.keys(node.attrs).length !== 0 ? mergeProps({props: node.attrs}, tmp) : tmp; + const isElement = React.isValidElement(child); if (typeof child === 'string') { diff --git a/test/i18n.js b/test/i18n.js index 264a06e1a..a1efb0240 100644 --- a/test/i18n.js +++ b/test/i18n.js @@ -39,6 +39,8 @@ i18n.init({ testTrans4KeyWithNestedComponent: 'Result should be a list: <0>', testTrans5KeyWithNestedComponent: 'Result should be a list: <1>', testTrans5KeyWithValue: 'Result should be rendered within tag <0>{{testValue}}', + transTest3: 'Result should be a clickable link <0 href="https://www.google.com">Google', + transTest3_overwrite: 'Result should be a clickable link <0 href="https://www.google.com">Google', }, other: { transTest1: 'Another go <1>there.', diff --git a/test/trans.render.spec.js b/test/trans.render.spec.js index d2db8ddaf..96ffa336f 100644 --- a/test/trans.render.spec.js +++ b/test/trans.render.spec.js @@ -90,6 +90,46 @@ describe('trans simple using ns prop', () => { }); }); +describe('trans using translation prop', () => { + const TestElement = ({ parent }) => ( + + + + ); + + it('should render correct content', () => { + const wrapper = mount(); + // console.log(wrapper.debug()); + expect( + wrapper.contains( +
+ Result should be a clickable link Google +
, + ), + ).toBe(true); + }); +}); + +describe('trans overwrites translation prop', () => { + const TestElement = ({ parent }) => ( + + + + ); + + it('should render correct content', () => { + const wrapper = mount(); + // console.log(wrapper.debug()); + expect( + wrapper.contains( +
+ Result should be a clickable link Google +
, + ), + ).toBe(true); + }); +}); + describe('trans simple with custom html tag', () => { const TestElement = ({ parent }) => (