Skip to content

Commit

Permalink
Add typescript for Translation component - clarify options, simplify …
Browse files Browse the repository at this point in the history
…redundant use of t (#709)
  • Loading branch information
rosskevin authored Jan 30, 2019
1 parent 57d23cb commit 6d321e5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/Translation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import React from 'react';
import { useTranslation } from './useTranslation';

export function Translation(props) {
const { ns, children } = props;
const [t, i18n] = useTranslation(ns, props);
const { ns, children, ...options } = props;
const [t, i18n] = useTranslation(ns, options);

return children(t, {
i18n,
t,
lng: i18n.language,
});
}
14 changes: 14 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,17 @@ export function withTranslation(
): <P extends WithTranslation>(
component: React.ComponentType<P>,
) => React.ComponentType<Omit<P, keyof WithTranslation>>;

export interface TranslationProps {
children: (
t: i18next.TFunction,
options: {
i18n: i18next.i18n;
lng: string;
},
) => React.ReactNode;
ns?: Namespace;
i18n?: i18next.i18n;
}

export function Translation(props: TranslationProps): any;
16 changes: 16 additions & 0 deletions test/typescript/Translation.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as React from 'react';
import { Translation } from 'react-i18next';

function basic() {
return <Translation>{(t, { i18n, lng }) => <div>{t('key1')}</div>}</Translation>;
}

function ns() {
return <Translation ns="foo">{(t, { i18n, lng }) => <div>{t('key1')}</div>}</Translation>;
}

function nsArray() {
return (
<Translation ns={['foo', 'bar']}>{(t, { i18n, lng }) => <div>{t('key1')}</div>}</Translation>
);
}

0 comments on commit 6d321e5

Please sign in to comment.