Skip to content

Commit 3426195

Browse files
committed
feat: expose Globalize instance via ref
1 parent caddf38 commit 3426195

4 files changed

Lines changed: 57 additions & 26 deletions

File tree

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"@types/hoist-non-react-statics": "^3.3.1",
5656
"@types/jest": "^26.0.14",
5757
"@types/node": "^14.11.2",
58+
"@types/react-test-renderer": "^17.0.0",
5859
"@typescript-eslint/eslint-plugin": "^4.2.0",
5960
"@typescript-eslint/parser": "^4.2.0",
6061
"cldr-data": "^36.0.0",
@@ -113,8 +114,8 @@
113114
},
114115
"release-it": {
115116
"git": {
116-
"commitMessage": "chore: release %s",
117-
"tagName": "v%s"
117+
"commitMessage": "chore: release ${version}",
118+
"tagName": "v${version}"
118119
},
119120
"npm": {
120121
"publish": true

src/components/GlobalizeProvider.tsx

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
* https://github.com/joshswan/react-native-globalize/blob/master/LICENSE
77
*/
88

9-
import React, { useEffect, useState } from 'react';
10-
import { createGlobalize } from '../globalize';
9+
import React, { useEffect, useImperativeHandle, useState } from 'react';
10+
import { createGlobalize, Globalize } from '../globalize';
1111
import { GlobalizeContext } from '../context';
1212

1313
export interface Props {
@@ -19,26 +19,19 @@ export interface Props {
1919
onError?(message: string, exception?: Error): void;
2020
}
2121

22-
export const GlobalizeProvider: React.FC<Props> = ({
23-
children,
24-
currency: currencyCode = 'USD',
25-
defaultLocale,
26-
locale = 'en',
27-
localeFallback: fallback = false,
28-
...options
29-
}) => {
30-
const [globalize, setGlobalize] = useState(() =>
31-
createGlobalize({
32-
...options,
33-
locale,
34-
currencyCode,
22+
export const GlobalizeProvider = React.forwardRef<Globalize, Props>(
23+
(
24+
{
25+
children,
26+
currency: currencyCode = 'USD',
3527
defaultLocale,
36-
fallback,
37-
}),
38-
);
39-
40-
useEffect(() => {
41-
setGlobalize(
28+
locale = 'en',
29+
localeFallback: fallback = false,
30+
...options
31+
},
32+
ref,
33+
) => {
34+
const [globalize, setGlobalize] = useState(() =>
4235
createGlobalize({
4336
...options,
4437
locale,
@@ -47,7 +40,21 @@ export const GlobalizeProvider: React.FC<Props> = ({
4740
fallback,
4841
}),
4942
);
50-
}, [currencyCode, defaultLocale, locale, fallback]); // eslint-disable-line react-hooks/exhaustive-deps
5143

52-
return <GlobalizeContext.Provider value={globalize}>{children}</GlobalizeContext.Provider>;
53-
};
44+
useEffect(() => {
45+
setGlobalize(
46+
createGlobalize({
47+
...options,
48+
locale,
49+
currencyCode,
50+
defaultLocale,
51+
fallback,
52+
}),
53+
);
54+
}, [currencyCode, defaultLocale, locale, fallback]); // eslint-disable-line react-hooks/exhaustive-deps
55+
56+
useImperativeHandle(ref, () => globalize);
57+
58+
return <GlobalizeContext.Provider value={globalize}>{children}</GlobalizeContext.Provider>;
59+
},
60+
);

src/components/__tests__/GlobalizeProvider.test.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88

99
import React from 'react';
1010
import renderer from 'react-test-renderer';
11+
import type { Globalize } from '../../globalize';
1112
import { FormattedCurrency, GlobalizeProvider, FormattedUnit } from '..';
1213

1314
describe('<GlobalizeProvider />', () => {
15+
jest.useFakeTimers();
16+
1417
test('renders correctly', () => {
1518
const tree = renderer
1619
.create(
@@ -59,4 +62,17 @@ describe('<GlobalizeProvider />', () => {
5962

6063
expect(root.toJSON()).toMatchSnapshot();
6164
});
65+
66+
test('exposes Globalize instance via ref', () => {
67+
const ref = React.createRef<Globalize>();
68+
69+
renderer.create(
70+
<GlobalizeProvider ref={ref} locale="en">
71+
<FormattedUnit unit="mile-per-hour" value={75} />
72+
</GlobalizeProvider>,
73+
);
74+
75+
expect(ref.current).toHaveProperty('locale', 'en');
76+
expect(ref.current?.formatUnit(75, 'mile-per-hour')).toBe('75 miles per hour');
77+
});
6278
});

yarn.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,6 +1646,13 @@
16461646
dependencies:
16471647
"@types/react" "*"
16481648

1649+
"@types/react-test-renderer@^17.0.0":
1650+
version "17.0.0"
1651+
resolved "https://registry.yarnpkg.com/@types/react-test-renderer/-/react-test-renderer-17.0.0.tgz#9be47b375eeb906fced37049e67284a438d56620"
1652+
integrity sha512-nvw+F81OmyzpyIE1S0xWpLonLUZCMewslPuA8BtjSKc5XEbn8zEQBXS7KuOLHTNnSOEM2Pum50gHOoZ62tqTRg==
1653+
dependencies:
1654+
"@types/react" "*"
1655+
16491656
"@types/react@*", "@types/react@^16.9.49":
16501657
version "16.9.49"
16511658
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.9.49.tgz#09db021cf8089aba0cdb12a49f8021a69cce4872"

0 commit comments

Comments
 (0)