Skip to content

Commit 6b327ab

Browse files
committed
feat: create <Translations> and <Translate>
1 parent da2c45c commit 6b327ab

File tree

5 files changed

+115
-12
lines changed

5 files changed

+115
-12
lines changed

src/context/index.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
import {createElement as h} from 'react';
22
import {Provider, Consumer} from 'freestyler-context';
3+
import faccToHoc from '../util/faccToHoc';
34

4-
export const withContext = (Comp: React.ComponentClass<any> | React.StatelessComponent<any>, name?: string) => {
5-
return (props) => {
6-
const {contextName = name, ...rest} = props;
7-
8-
return h(Consumer, {name: contextName}, (value) => {
9-
rest[contextName] = value;
10-
11-
return h(Comp, rest);
12-
});
13-
};
14-
};
5+
const withContext = faccToHoc(Consumer, '');
156

167
export {
178
Provider,
18-
Consumer
9+
Consumer,
10+
withContext
1911
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`route <Route> does basic routing 1`] = `"<div><div>/foo</div><!-- react-empty: 3 --></div>"`;

src/route/__tests__/index.test.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import {mount} from 'enzyme';
2+
import {h} from '../../util';
3+
import {Router, Route} from '..';
4+
5+
describe('route', () => {
6+
describe('<Router>', () => {
7+
it('is a component', () => {
8+
expect(Router).toBeInstanceOf(Function);
9+
});
10+
});
11+
12+
describe('<Route>', () => {
13+
it('is a component', () => {
14+
expect(Route).toBeInstanceOf(Function);
15+
});
16+
17+
it('does basic routing', () => {
18+
const wrapper = mount(
19+
<Router route='/foo'>
20+
<div>
21+
<Route match='/foo'>
22+
/foo
23+
</Route>
24+
<Route match='/bar'>
25+
/foo
26+
</Route>
27+
</div>
28+
</Router>
29+
);
30+
31+
expect(wrapper.html()).toMatchSnapshot();
32+
});
33+
});
34+
});

src/translate/__story__/story.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {createElement as h} from 'react';
2+
import {storiesOf} from '@storybook/react';
3+
import {action} from '@storybook/addon-actions';
4+
import {linkTo} from '@storybook/addon-links';
5+
import {LocationSensor} from '../../LocationSensor';
6+
import {Translations, Translate, T, withT} from '..';
7+
import ShowDocs from '../../../.storybook/ShowDocs'
8+
9+
storiesOf('Context/translate', module)
10+
.add('Documentation', () => h(ShowDocs, {name: 'translate'}))
11+
.add('Example', () => (
12+
<Translations map={{
13+
hello: 'world'
14+
}}>
15+
<div>
16+
<T>{(T) =>
17+
<span>{T('omg')}: {T('hello')}</span>
18+
}</T>
19+
</div>
20+
</Translations>
21+
));

src/translate/index.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import {Component} from 'react';
2+
import {h, ns} from '../util';
3+
import {Provider, Consumer} from '../context';
4+
import faccToHoc from '../util/faccToHoc';
5+
import renderProp from '../util/renderProp';
6+
7+
export interface ITranslationsProps {
8+
map: {[key: string]: string | React.StatelessComponent<any>};
9+
ns?: string;
10+
}
11+
12+
export interface ITranslationsState {
13+
14+
}
15+
16+
export class Translations extends Component<ITranslationsProps, ITranslationsState> {
17+
render () {
18+
return h(Provider, {
19+
name: ns(`T/${this.props.ns}`),
20+
value: this.props.map
21+
}, this.props.children);
22+
}
23+
}
24+
25+
export interface ITranslateProps {
26+
ns?: string;
27+
}
28+
29+
export interface ITranslateState {
30+
31+
}
32+
33+
export class Translate extends Component<ITranslateProps, ITranslateState> {
34+
render () {
35+
return h(Consumer, {name: ns(`T/${this.props.ns}`)}, (map) => {
36+
const T = (key) => {
37+
const value = map[key];
38+
39+
if (typeof value === 'function') {
40+
return value(T);
41+
}
42+
43+
return value || key;
44+
};
45+
46+
return renderProp(this.props, T);
47+
});
48+
}
49+
}
50+
51+
export const T = Translate;
52+
53+
export const withT = faccToHoc(Translate, 'T');

0 commit comments

Comments
 (0)