|
410 | 410 | }], ast, getAsArray(children || []));
|
411 | 411 | return getChildren(result[0]);
|
412 | 412 | };
|
| 413 | + const fixComponentProps = (component, index, translation) => { |
| 414 | + const componentKey = component.key || index; |
| 415 | + const comp = react.cloneElement(component, { |
| 416 | + key: componentKey |
| 417 | + }); |
| 418 | + if (!comp.props || !comp.props.children || translation.indexOf(`${index}/>`) < 0 && translation.indexOf(`${index} />`) < 0) { |
| 419 | + return comp; |
| 420 | + } |
| 421 | + function Componentized() { |
| 422 | + return react.createElement(react.Fragment, null, comp); |
| 423 | + } |
| 424 | + return react.createElement(Componentized); |
| 425 | + }; |
| 426 | + const generateArrayComponents = (components, translation) => components.map((c, index) => fixComponentProps(c, index, translation)); |
| 427 | + const generateObjectComponents = (components, translation) => { |
| 428 | + const componentMap = {}; |
| 429 | + Object.keys(components).forEach(c => { |
| 430 | + Object.assign(componentMap, { |
| 431 | + [c]: fixComponentProps(components[c], c, translation) |
| 432 | + }); |
| 433 | + }); |
| 434 | + return componentMap; |
| 435 | + }; |
| 436 | + const generateComponents = (components, translation) => { |
| 437 | + if (!components) return null; |
| 438 | + if (Array.isArray(components)) { |
| 439 | + return generateArrayComponents(components, translation); |
| 440 | + } |
| 441 | + if (isObject(components)) { |
| 442 | + return generateObjectComponents(components, translation); |
| 443 | + } |
| 444 | + warnOnce('<Trans /> component prop expects an object or an array'); |
| 445 | + return null; |
| 446 | + }; |
413 | 447 | function Trans$1(_ref) {
|
414 | 448 | let {
|
415 | 449 | children,
|
|
470 | 504 | ns: namespaces
|
471 | 505 | };
|
472 | 506 | const translation = key ? t(key, combinedTOpts) : defaultValue;
|
473 |
| - if (components) { |
474 |
| - Object.keys(components).forEach(c => { |
475 |
| - const componentKey = components[c].key || c; |
476 |
| - const comp = react.cloneElement(components[c], { |
477 |
| - key: componentKey |
478 |
| - }); |
479 |
| - if (!comp.props || !comp.props.children || translation.indexOf(`${c}/>`) < 0 && translation.indexOf(`${c} />`) < 0) return; |
480 |
| - function Componentized() { |
481 |
| - return react.createElement(react.Fragment, null, comp); |
482 |
| - } |
483 |
| - components[c] = react.createElement(Componentized); |
484 |
| - }); |
485 |
| - } |
486 |
| - const content = renderNodes(components || children, translation, i18n, reactI18nextOptions, combinedTOpts, shouldUnescape); |
| 507 | + const generatedComponents = generateComponents(components, translation); |
| 508 | + const content = renderNodes(generatedComponents || children, translation, i18n, reactI18nextOptions, combinedTOpts, shouldUnescape); |
487 | 509 | const useAsParent = parent ?? reactI18nextOptions.defaultTransParent;
|
488 | 510 | return useAsParent ? react.createElement(useAsParent, additionalProps, content) : content;
|
489 | 511 | }
|
|
0 commit comments