Skip to content

Commit

Permalink
feat: 🎸 allow non-functional children in <Trans> component
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jan 8, 2019
1 parent e9c3943 commit aa8ae78
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/__stories__/Trans.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ storiesOf('Trans', module)
<Trans>{({t, T}) =>
<Demo t={t} T={T} />
}</Trans>
<Trans>Hello</Trans>
<Trans> yo {'welcome'} ... {({t}) => t('welcome')}!</Trans>
</Provider>
)
.add('Switch preloaded translations as "children" render prop', () =>
Expand Down
10 changes: 9 additions & 1 deletion src/createTranslations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,15 @@ export const createTranslations = (ns: string = 'main'): Result => {
? [props.ns as unknown as string]
: undefined;
const t = T.createT ? T.createT(nss) : defaultT;
return props.children({t, T}) || null;
if (typeof props.children === 'function') {
return props.children({t, T}) || null;
} else if(Array.isArray(props.children)) {
return React.createElement(React.Fragment, null, ...props.children.map(item =>
typeof item === 'function' ? (item as any)({t, T}) : t(item)
));
} else {
return props.children || null;
}
});
};

Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export interface ProviderState {

export interface TransProps extends UniversalProps<{t: TranslatorFn, T: ProviderState}> {
ns?: string | string[];
children: ({t: TranslatorFn, T: ProviderState}) => React.ReactNode;
// children: ({t: TranslatorFn, T: ProviderState}) => React.ReactNode;
children: any;
}

// React hook.
Expand Down

0 comments on commit aa8ae78

Please sign in to comment.