@@ -298,6 +298,59 @@ const renderNodes = (children, targetString, i18n, i18nOptions, combinedTOpts, s
298
298
return getChildren ( result [ 0 ] ) ;
299
299
} ;
300
300
301
+ const fixComponentProps = ( component , index , translation ) => {
302
+ const componentKey = component . key || index ;
303
+ const comp = cloneElement ( component , { key : componentKey } ) ;
304
+ if (
305
+ ! comp . props ||
306
+ ! comp . props . children ||
307
+ ( translation . indexOf ( `${ index } />` ) < 0 && translation . indexOf ( `${ index } />` ) < 0 )
308
+ ) {
309
+ return comp ;
310
+ }
311
+
312
+ function Componentized ( ) {
313
+ // <>{comp}</>
314
+ return createElement ( Fragment , null , comp ) ;
315
+ }
316
+ // <Componentized />
317
+ return createElement ( Componentized ) ;
318
+ } ;
319
+
320
+ const generateArrayComponents = ( components , translation ) =>
321
+ components . map ( ( c , index ) => fixComponentProps ( c , index , translation ) ) ;
322
+
323
+ const generateObjectComponents = ( components , translation ) => {
324
+ const componentMap = { } ;
325
+
326
+ Object . keys ( components ) . forEach ( ( c ) => {
327
+ Object . assign ( componentMap , {
328
+ [ c ] : fixComponentProps ( components [ c ] , c , translation ) ,
329
+ } ) ;
330
+ } ) ;
331
+
332
+ return componentMap ;
333
+ } ;
334
+
335
+ const generateComponents = ( components , translation ) => {
336
+ if ( ! components ) return null ;
337
+
338
+ // components could be either an array or an object
339
+
340
+ if ( Array . isArray ( components ) ) {
341
+ return generateArrayComponents ( components , translation ) ;
342
+ }
343
+
344
+ if ( isObject ( components ) ) {
345
+ return generateObjectComponents ( components , translation ) ;
346
+ }
347
+
348
+ // if components is not an array or an object, warn the user
349
+ // and return null
350
+ warnOnce ( '<Trans /> component prop expects an object or an array' ) ;
351
+ return null ;
352
+ } ;
353
+
301
354
export function Trans ( {
302
355
children,
303
356
count,
@@ -360,29 +413,10 @@ export function Trans({
360
413
} ;
361
414
const translation = key ? t ( key , combinedTOpts ) : defaultValue ;
362
415
363
- if ( components ) {
364
- Object . keys ( components ) . forEach ( ( c ) => {
365
- const componentKey = components [ c ] . key || c ;
366
- const comp = cloneElement ( components [ c ] , { key : componentKey } ) ;
367
- if (
368
- ! comp . props ||
369
- ! comp . props . children ||
370
- ( translation . indexOf ( `${ c } />` ) < 0 && translation . indexOf ( `${ c } />` ) < 0 )
371
- )
372
- return ;
373
-
374
- // eslint-disable-next-line react/no-unstable-nested-components
375
- function Componentized ( ) {
376
- // <>{comp}</>
377
- return createElement ( Fragment , null , comp ) ;
378
- }
379
- // <Componentized />
380
- components [ c ] = createElement ( Componentized ) ;
381
- } ) ;
382
- }
416
+ const generatedComponents = generateComponents ( components , translation ) ;
383
417
384
418
const content = renderNodes (
385
- components || children ,
419
+ generatedComponents || children ,
386
420
translation ,
387
421
i18n ,
388
422
reactI18nextOptions ,
0 commit comments