Skip to content

Commit a79024c

Browse files
committed
feat: refactor internals, simplify API & add useGlobalize hook
The messy (and old) Globalize class has been removed in favor of a cleaner object interface, containing the same memoized getFormatter functions as well as new simplified formatX functions. Any formatting logic that was in the Formatted components has been removed and placed directly in the formatting functions to keep results consistent whether using a component or a function. And finally, the `useGlobalize` hook allows easy access to the globalize object in any component, and due to the new object interface, formatting functions can simply be destructured straight from it (e.g. `const { formatDate } = useGlobalize();`). BREAKING CHANGE: FormattedProvider no longer accepts the `cldr` or `messages` props as they were unreliable with regards to updates. Additionally, advanced users will need to convert from the old `new Globalize(locale...)` signature to the new `createGlobalize(options)` one. re #47
1 parent 86cdee3 commit a79024c

32 files changed

+1129
-828
lines changed

.eslintrc

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,30 @@
3030
}],
3131
"import/prefer-default-export": "off",
3232
"react/jsx-filename-extension": ["error", { "extensions": [".jsx", ".tsx"] }],
33+
"react/jsx-props-no-spreading": "off",
3334
"react/prop-types": "off",
3435
"react/static-property-placement": ["error", "static public field"],
3536
"@typescript-eslint/ban-ts-ignore": "off",
36-
"@typescript-eslint/ban-types": "off",
3737
"@typescript-eslint/explicit-function-return-type": "off",
3838
"@typescript-eslint/no-explicit-any": "off",
3939
"@typescript-eslint/no-non-null-assertion": "off"
4040
},
4141
"overrides": [
4242
{
43-
"files": ["*.test.ts", "*.test.tsx"],
44-
"env": { "jest": true, "node": true }
43+
"files": ["*.ts", "*.tsx"],
44+
"rules": {
45+
"lines-between-class-members": "off",
46+
"no-dupe-class-members": "off"
47+
}
48+
},
49+
{
50+
"files": ["*.test.*"],
51+
"env": { "jest": true, "node": true },
52+
"rules": {
53+
"global-require": "off",
54+
"@typescript-eslint/no-empty-function": "off",
55+
"@typescript-eslint/no-var-requires": "off"
56+
}
4557
}
4658
]
4759
}

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
./artifacts
2-
./dist
1+
/artifacts
2+
/dist
33

44
### Node ###
55
# Logs

package.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
},
3636
"dependencies": {
3737
"@types/globalize": "file:types/globalize",
38+
"@types/react": "^16.9.19",
39+
"@types/react-native": "^0.61.15",
3840
"cldrjs": "^0.5.0",
3941
"globalize": "^1.4.2",
4042
"moment": "^2.19.3"
@@ -44,8 +46,7 @@
4446
"@babel/preset-typescript": "^7.8.3",
4547
"@types/cldrjs": "^0.4.22",
4648
"@types/jest": "^25.1.2",
47-
"@types/react": "^16.9.19",
48-
"@types/react-native": "^0.61.14",
49+
"@types/node": "^13.7.1",
4950
"@typescript-eslint/eslint-plugin": "^2.19.2",
5051
"@typescript-eslint/parser": "^2.19.2",
5152
"cldr-data": "^36.0.0",
@@ -62,6 +63,7 @@
6263
"jest": "^25.1.0",
6364
"jest-junit": "^10.0.0",
6465
"metro-react-native-babel-preset": "^0.58.0",
66+
"mockdate": "^2.0.5",
6567
"react": "16.9.0",
6668
"react-native": "^0.61.5",
6769
"react-test-renderer": "^16.12.0",
@@ -73,7 +75,13 @@
7375
"react-native": ">=0.59.0"
7476
},
7577
"jest": {
76-
"preset": "react-native"
78+
"preset": "react-native",
79+
"roots": [
80+
"<rootDir>/src"
81+
],
82+
"setupFilesAfterEnv": [
83+
"<rootDir>/test/setup.test.js"
84+
]
7785
},
7886
"jest-junit": {
7987
"outputDirectory": "./artifacts/jest",

src/components/FormattedCurrency.tsx

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/components/FormattedDate.tsx

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/components/FormattedMessage.tsx

Lines changed: 0 additions & 82 deletions
This file was deleted.

src/components/FormattedNumber.tsx

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/components/FormattedPlural.tsx

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

9-
import { PluralGeneratorOptions } from 'globalize';
10-
import React, { useContext } from 'react';
11-
import { Text, TextStyle } from 'react-native';
12-
import { GlobalizeContext } from '../context';
13-
14-
export interface FormattedPluralProps extends PluralGeneratorOptions {
15-
accessibilityLabel?: string;
16-
adjustsFontSizeToFit?: boolean;
17-
allowFontScaling?: boolean;
18-
style?: TextStyle;
19-
value: number;
9+
import React from 'react';
10+
import { Text } from 'react-native';
11+
import { Globalize, PluralGeneratorOptions } from '../globalize';
12+
import { useGlobalize } from '../hooks';
13+
import { createPropFilter, TextProps } from './utils';
2014

15+
type Props = PluralGeneratorOptions & TextProps & {
16+
value: Parameters<Globalize['formatPlural']>[0];
2117
other: React.ReactNode;
2218
zero?: React.ReactNode;
2319
one?: React.ReactNode;
2420
two?: React.ReactNode;
2521
few?: React.ReactNode;
2622
many?: React.ReactNode;
27-
}
23+
};
2824

29-
export const FormattedPlural: React.FC<FormattedPluralProps> = ({
30-
accessibilityLabel = '',
31-
adjustsFontSizeToFit = false,
32-
allowFontScaling = true,
33-
style,
25+
const filterProps = createPropFilter<Props, Parameters<Globalize['formatPlural']>>(({
3426
type,
3527
value,
36-
...options
28+
}) => [value, { type }]);
29+
30+
export const FormattedPlural: React.FC<Props> = ({
31+
children,
32+
...props
3733
}) => {
38-
const globalize = useContext(GlobalizeContext);
39-
const formatPlural = globalize.getPluralGenerator({ type });
40-
const pluralCategory = (typeof value === 'number') ? formatPlural(value) : 'other';
41-
const formattedPlural = options[pluralCategory] || options.other;
34+
const { formatPlural } = useGlobalize();
35+
const [args, textProps] = filterProps(props);
36+
const plural = formatPlural(...args);
37+
const { [plural]: formatted, other } = props;
4238

4339
return (
44-
<Text
45-
accessible
46-
accessibilityLabel={accessibilityLabel}
47-
adjustsFontSizeToFit={adjustsFontSizeToFit}
48-
allowFontScaling={allowFontScaling}
49-
style={style}
50-
>
51-
{formattedPlural}
40+
<Text {...textProps}>
41+
{formatted || other}
42+
{children}
5243
</Text>
5344
);
5445
};

0 commit comments

Comments
 (0)