react-intl 6.0.6 → 6.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) hide show
  1. package/index.d.ts +43 -0
  2. package/index.d.ts.map +1 -0
  3. package/index.js +51 -0
  4. package/lib/index.d.ts +43 -0
  5. package/lib/index.d.ts.map +1 -0
  6. package/lib/index.js +26 -0
  7. package/lib/src/components/createFormattedComponent.d.ts +29 -0
  8. package/lib/src/components/createFormattedComponent.d.ts.map +1 -0
  9. package/lib/src/components/createFormattedComponent.js +62 -0
  10. package/lib/src/components/dateTimeRange.d.ts +11 -0
  11. package/lib/src/components/dateTimeRange.d.ts.map +1 -0
  12. package/lib/src/components/dateTimeRange.js +15 -0
  13. package/lib/src/components/injectIntl.d.ts +22 -0
  14. package/lib/src/components/injectIntl.d.ts.map +1 -0
  15. package/lib/src/components/injectIntl.js +29 -0
  16. package/lib/src/components/message.d.ts +12 -0
  17. package/lib/src/components/message.d.ts.map +1 -0
  18. package/lib/src/components/message.js +35 -0
  19. package/lib/src/components/plural.d.ts +15 -0
  20. package/lib/src/components/plural.d.ts.map +1 -0
  21. package/lib/src/components/plural.js +26 -0
  22. package/lib/src/components/provider.d.ts +35 -0
  23. package/lib/src/components/provider.d.ts.map +1 -0
  24. package/lib/src/components/provider.js +112 -0
  25. package/lib/src/components/relative.d.ts +12 -0
  26. package/lib/src/components/relative.d.ts.map +1 -0
  27. package/lib/src/components/relative.js +129 -0
  28. package/lib/src/components/useIntl.d.ts +3 -0
  29. package/lib/src/components/useIntl.d.ts.map +1 -0
  30. package/lib/src/components/useIntl.js +8 -0
  31. package/lib/src/types.d.ts +12 -0
  32. package/lib/src/types.d.ts.map +1 -0
  33. package/lib/src/types.js +1 -0
  34. package/lib/src/utils.d.ts +14 -0
  35. package/lib/src/utils.d.ts.map +1 -0
  36. package/lib/src/utils.js +43 -0
  37. package/package.json +10 -10
  38. package/react-intl.iife.js +4688 -0
  39. package/src/components/createFormattedComponent.d.ts +29 -0
  40. package/src/components/createFormattedComponent.d.ts.map +1 -0
  41. package/src/components/createFormattedComponent.js +69 -0
  42. package/src/components/dateTimeRange.d.ts +11 -0
  43. package/src/components/dateTimeRange.d.ts.map +1 -0
  44. package/src/components/dateTimeRange.js +17 -0
  45. package/src/components/injectIntl.d.ts +22 -0
  46. package/src/components/injectIntl.d.ts.map +1 -0
  47. package/src/components/injectIntl.js +33 -0
  48. package/src/components/message.d.ts +12 -0
  49. package/src/components/message.d.ts.map +1 -0
  50. package/src/components/message.js +37 -0
  51. package/src/components/plural.d.ts +15 -0
  52. package/src/components/plural.d.ts.map +1 -0
  53. package/src/components/plural.js +29 -0
  54. package/src/components/provider.d.ts +35 -0
  55. package/src/components/provider.d.ts.map +1 -0
  56. package/src/components/provider.js +116 -0
  57. package/src/components/relative.d.ts +12 -0
  58. package/src/components/relative.d.ts.map +1 -0
  59. package/src/components/relative.js +131 -0
  60. package/src/components/useIntl.d.ts +3 -0
  61. package/src/components/useIntl.d.ts.map +1 -0
  62. package/src/components/useIntl.js +12 -0
  63. package/src/types.d.ts +12 -0
  64. package/src/types.d.ts.map +1 -0
  65. package/src/types.js +2 -0
  66. package/src/utils.d.ts +14 -0
  67. package/src/utils.d.ts.map +1 -0
  68. package/src/utils.js +49 -0
  69. package/BUILD +0 -130
  70. package/CHANGELOG.md +0 -1384
  71. package/example-sandboxes/rescripts/.rescriptsrc.js +0 -28
  72. package/example-sandboxes/rescripts/package-lock.json +0 -18035
  73. package/example-sandboxes/rescripts/package.json +0 -35
  74. package/example-sandboxes/rescripts/public/index.html +0 -42
  75. package/example-sandboxes/rescripts/src/App.tsx +0 -16
  76. package/example-sandboxes/rescripts/src/index.tsx +0 -5
  77. package/example-sandboxes/rescripts/src/react-app-env.d.ts +0 -1
  78. package/example-sandboxes/rescripts/src/styles.css +0 -4
  79. package/example-sandboxes/rescripts/tsconfig.json +0 -20
  80. package/example-sandboxes/strict-locale-type/.env +0 -1
  81. package/example-sandboxes/strict-locale-type/package-lock.json +0 -14595
  82. package/example-sandboxes/strict-locale-type/package.json +0 -35
  83. package/example-sandboxes/strict-locale-type/src/App.tsx +0 -48
  84. package/example-sandboxes/strict-locale-type/src/index.html +0 -28
  85. package/example-sandboxes/strict-locale-type/src/index.tsx +0 -7
  86. package/example-sandboxes/strict-locale-type/src/styles.css +0 -4
  87. package/example-sandboxes/strict-locale-type/tsconfig.json +0 -7
  88. package/example-sandboxes/strict-message-types/.env +0 -1
  89. package/example-sandboxes/strict-message-types/package-lock.json +0 -14596
  90. package/example-sandboxes/strict-message-types/package.json +0 -35
  91. package/example-sandboxes/strict-message-types/src/App.tsx +0 -31
  92. package/example-sandboxes/strict-message-types/src/index.html +0 -28
  93. package/example-sandboxes/strict-message-types/src/index.tsx +0 -7
  94. package/example-sandboxes/strict-message-types/src/styles.css +0 -4
  95. package/example-sandboxes/strict-message-types/tsconfig.json +0 -7
  96. package/examples/BUILD +0 -70
  97. package/examples/Bug2727.tsx +0 -37
  98. package/examples/HandleChange.tsx +0 -48
  99. package/examples/Hooks.tsx +0 -126
  100. package/examples/Injected.tsx +0 -41
  101. package/examples/Messages.tsx +0 -82
  102. package/examples/StaticTypeSafetyAndCodeSplitting/StaticTypeSafetyAndCodeSplitting.tsx +0 -44
  103. package/examples/StaticTypeSafetyAndCodeSplitting/en.json +0 -3
  104. package/examples/StaticTypeSafetyAndCodeSplitting/intlHelpers.tsx +0 -39
  105. package/examples/StaticTypeSafetyAndCodeSplitting/it.json +0 -3
  106. package/examples/TimeZone.tsx +0 -44
  107. package/examples/advanced/Advanced.tsx +0 -68
  108. package/examples/advanced/compiled-lang/en.json +0 -77
  109. package/examples/advanced/compiled-lang/fr.json +0 -77
  110. package/examples/index.html +0 -20
  111. package/examples/index.tsx +0 -44
  112. package/examples/package.json +0 -20
  113. package/index.ts +0 -127
  114. package/jest.config.js +0 -27
  115. package/src/components/createFormattedComponent.tsx +0 -123
  116. package/src/components/dateTimeRange.tsx +0 -26
  117. package/src/components/injectIntl.tsx +0 -111
  118. package/src/components/message.tsx +0 -73
  119. package/src/components/plural.tsx +0 -45
  120. package/src/components/provider.tsx +0 -196
  121. package/src/components/relative.tsx +0 -192
  122. package/src/components/useIntl.ts +0 -10
  123. package/src/types.ts +0 -29
  124. package/src/utils.ts +0 -77
  125. package/tests/functional/index.ts +0 -18
  126. package/tests/functional/support/build.ts +0 -16
  127. package/tests/functional/support/format.tsx +0 -112
  128. package/tests/perf/index.tsx +0 -196
  129. package/tests/setup.js +0 -10
  130. package/tests/unit/components/__snapshots__/displayName.tsx.snap +0 -19
  131. package/tests/unit/components/__snapshots__/message.tsx.snap +0 -41
  132. package/tests/unit/components/__snapshots__/relative.tsx.snap +0 -11
  133. package/tests/unit/components/__snapshots__/useIntl.tsx.snap +0 -25
  134. package/tests/unit/components/date.tsx +0 -233
  135. package/tests/unit/components/dateTimeRange.tsx +0 -103
  136. package/tests/unit/components/displayName.tsx +0 -57
  137. package/tests/unit/components/message.tsx +0 -509
  138. package/tests/unit/components/number.tsx +0 -198
  139. package/tests/unit/components/plural.tsx +0 -116
  140. package/tests/unit/components/provider.tsx +0 -215
  141. package/tests/unit/components/relative.tsx +0 -263
  142. package/tests/unit/components/time.tsx +0 -242
  143. package/tests/unit/components/useIntl.tsx +0 -64
  144. package/tests/unit/components/withIntl.tsx +0 -66
  145. package/tests/unit/react-intl.ts +0 -88
  146. package/tests/unit/testUtils.tsx +0 -42
  147. package/tsconfig.json +0 -5
@@ -1,68 +0,0 @@
1
- import * as React from 'react'
2
- import {IntlProvider, FormattedMessage} from 'react-intl'
3
-
4
- type IntlProviderProps = React.ComponentProps<typeof IntlProvider>
5
-
6
- function loadLocaleData(locale: string) {
7
- switch (locale) {
8
- case 'fr':
9
- return import('./compiled-lang/fr.json')
10
- default:
11
- return import('./compiled-lang/en.json')
12
- }
13
- }
14
-
15
- interface Props {
16
- locale: IntlProviderProps['locale']
17
- messages: IntlProviderProps['messages']
18
- }
19
-
20
- const App: React.FC<Props> = props => {
21
- return (
22
- <IntlProvider
23
- locale={props.locale}
24
- defaultLocale="en"
25
- messages={props.messages}
26
- >
27
- <p>
28
- <span style={{fontSize: '30px'}}>AST</span>
29
- <br />
30
- <FormattedMessage id="simple" />
31
- <br />
32
- <FormattedMessage id="placeholder" values={{name: 'John'}} />
33
- <br />
34
- <FormattedMessage id="date" values={{ts: Date.now()}} />
35
- <br />
36
- <FormattedMessage id="time" values={{ts: Date.now()}} />
37
- <br />
38
- <FormattedMessage id="number" values={{num: Math.random() * 1000}} />
39
- <br />
40
- <FormattedMessage id="plural" values={{num: 1}} />
41
- <br />
42
- <FormattedMessage id="plural" values={{num: 99}} />
43
- <br />
44
- <FormattedMessage id="select" values={{gender: 'male'}} />
45
- <br />
46
- <FormattedMessage id="select" values={{gender: 'female'}} />
47
- <br />
48
- <FormattedMessage id="selectordinal" values={{order: 1}} />
49
- <br />
50
- <FormattedMessage id="selectordinal" values={{order: 2}} />
51
- <br />
52
- <FormattedMessage id="selectordinal" values={{order: 3}} />
53
- <br />
54
- <FormattedMessage id="selectordinal" values={{order: 4}} />
55
- <br />
56
- <FormattedMessage
57
- id="richtext"
58
- values={{num: 99, bold: (chunks: any) => <strong>{chunks}</strong>}}
59
- />
60
- </p>
61
- </IntlProvider>
62
- )
63
- }
64
-
65
- export async function bootstrapApplication(locale: string) {
66
- const messages = await loadLocaleData(locale)
67
- return <App locale={locale} messages={messages.default} />
68
- }
@@ -1,77 +0,0 @@
1
- {
2
- "simple": [{"type": 0, "value": "Hello world"}],
3
- "placeholder": [
4
- {"type": 0, "value": "Hello "},
5
- {"type": 1, "value": "name"}
6
- ],
7
- "date": [
8
- {"type": 0, "value": "Hello "},
9
- {"type": 3, "style": null, "value": "ts"}
10
- ],
11
- "time": [
12
- {"type": 0, "value": "Hello "},
13
- {"type": 4, "style": null, "value": "ts"}
14
- ],
15
- "number": [
16
- {"type": 0, "value": "Hello "},
17
- {"type": 2, "style": null, "value": "num"}
18
- ],
19
- "plural": [
20
- {"type": 0, "value": "I have "},
21
- {
22
- "type": 6,
23
- "pluralType": "cardinal",
24
- "value": "num",
25
- "offset": 0,
26
- "options": {
27
- "one": {"value": [{"type": 7}, {"type": 0, "value": " dog"}]},
28
- "other": {"value": [{"type": 7}, {"type": 0, "value": " dogs"}]}
29
- }
30
- }
31
- ],
32
- "select": [
33
- {"type": 0, "value": "I am a "},
34
- {
35
- "type": 5,
36
- "value": "gender",
37
- "options": {
38
- "male": {"value": [{"type": 0, "value": "boy"}]},
39
- "female": {"value": [{"type": 0, "value": "girl"}]}
40
- }
41
- }
42
- ],
43
- "selectordinal": [
44
- {"type": 0, "value": "I am the "},
45
- {
46
- "type": 6,
47
- "pluralType": "ordinal",
48
- "value": "order",
49
- "offset": 0,
50
- "options": {
51
- "one": {"value": [{"type": 7}, {"type": 0, "value": "st person"}]},
52
- "two": {"value": [{"type": 7}, {"type": 0, "value": "nd person"}]},
53
- "=3": {"value": [{"type": 7}, {"type": 0, "value": "rd person"}]},
54
- "other": {"value": [{"type": 7}, {"type": 0, "value": "th person"}]}
55
- }
56
- }
57
- ],
58
- "richtext": [
59
- {"type": 0, "value": "I have "},
60
- {
61
- "type": 8,
62
- "value": "bold",
63
- "children": [
64
- {
65
- "type": 6,
66
- "pluralType": "cardinal",
67
- "value": "num",
68
- "offset": 0,
69
- "options": {
70
- "one": {"value": [{"type": 7}, {"type": 0, "value": " dog"}]},
71
- "other": {"value": [{"type": 7}, {"type": 0, "value": " dogs"}]}
72
- }
73
- }
74
- ]
75
- }
76
- ]
77
- }
@@ -1,77 +0,0 @@
1
- {
2
- "simple": [{"type": 0, "value": "Hello world"}],
3
- "placeholder": [
4
- {"type": 0, "value": "Hello "},
5
- {"type": 1, "value": "name"}
6
- ],
7
- "date": [
8
- {"type": 0, "value": "Hello "},
9
- {"type": 3, "style": null, "value": "ts"}
10
- ],
11
- "time": [
12
- {"type": 0, "value": "Hello "},
13
- {"type": 4, "style": null, "value": "ts"}
14
- ],
15
- "number": [
16
- {"type": 0, "value": "Hello "},
17
- {"type": 2, "style": null, "value": "num"}
18
- ],
19
- "plural": [
20
- {"type": 0, "value": "I have "},
21
- {
22
- "type": 6,
23
- "pluralType": "cardinal",
24
- "value": "num",
25
- "offset": 0,
26
- "options": {
27
- "one": {"value": [{"type": 7}, {"type": 0, "value": " dog"}]},
28
- "other": {"value": [{"type": 7}, {"type": 0, "value": " dogs"}]}
29
- }
30
- }
31
- ],
32
- "select": [
33
- {"type": 0, "value": "I am a "},
34
- {
35
- "type": 5,
36
- "value": "gender",
37
- "options": {
38
- "male": {"value": [{"type": 0, "value": "boy"}]},
39
- "female": {"value": [{"type": 0, "value": "girl"}]}
40
- }
41
- }
42
- ],
43
- "selectordinal": [
44
- {"type": 0, "value": "I am the "},
45
- {
46
- "type": 6,
47
- "pluralType": "ordinal",
48
- "value": "order",
49
- "offset": 0,
50
- "options": {
51
- "one": {"value": [{"type": 7}, {"type": 0, "value": "st person"}]},
52
- "two": {"value": [{"type": 7}, {"type": 0, "value": "nd person"}]},
53
- "=3": {"value": [{"type": 7}, {"type": 0, "value": "rd person"}]},
54
- "other": {"value": [{"type": 7}, {"type": 0, "value": "th person"}]}
55
- }
56
- }
57
- ],
58
- "richtext": [
59
- {"type": 0, "value": "I have "},
60
- {
61
- "type": 8,
62
- "value": "bold",
63
- "children": [
64
- {
65
- "type": 6,
66
- "pluralType": "cardinal",
67
- "value": "num",
68
- "offset": 0,
69
- "options": {
70
- "one": {"value": [{"type": 7}, {"type": 0, "value": " dog"}]},
71
- "other": {"value": [{"type": 7}, {"type": 0, "value": " dogs"}]}
72
- }
73
- }
74
- ]
75
- }
76
- ]
77
- }
@@ -1,20 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width, initial-scale=1" />
6
- <title>React Intl Explicit Example</title>
7
- </head>
8
- <body>
9
- <div id="timezone"></div>
10
- <div id="messages"></div>
11
- <div id="hooks"></div>
12
- <div id="advanced"></div>
13
- <div id="injected"></div>
14
- <div id="handlechange"></div>
15
- <div id="static-type-safety-and-code-splitting"></div>
16
- <div id="bug2727"></div>
17
- <!-- Name of the bazel esbuild target -->
18
- <script src="./main.js"></script>
19
- </body>
20
- </html>
@@ -1,44 +0,0 @@
1
- import 'core-js/stable'
2
- import '@formatjs/intl-pluralrules/polyfill'
3
- import * as React from 'react'
4
- import * as ReactDOM from 'react-dom'
5
- import Timezone from './TimeZone'
6
- import Messages from './Messages'
7
- import Hooks from './Hooks'
8
- import {bootstrapApplication} from './advanced/Advanced'
9
- import Injected from './Injected'
10
- import HandleChange from './HandleChange'
11
- import StaticTypeSafetyAndCodeSplitting from './StaticTypeSafetyAndCodeSplitting/StaticTypeSafetyAndCodeSplitting'
12
- import {App as Bug2727} from './Bug2727'
13
- //polyfills
14
- import '@formatjs/intl-getcanonicallocales/polyfill'
15
- import '@formatjs/intl-locale/polyfill'
16
-
17
- import '@formatjs/intl-pluralrules/polyfill'
18
- import '@formatjs/intl-pluralrules/locale-data/en' // locale-data for en
19
-
20
- import '@formatjs/intl-numberformat/polyfill'
21
- import '@formatjs/intl-numberformat/locale-data/en' // locale-data for en
22
-
23
- import '@formatjs/intl-datetimeformat/polyfill'
24
- import '@formatjs/intl-datetimeformat/locale-data/en' // locale-data for en
25
- import '@formatjs/intl-datetimeformat/add-all-tz' // Add ALL tz data
26
-
27
- ReactDOM.render(<Timezone />, document.getElementById('timezone'))
28
-
29
- ReactDOM.render(<Messages />, document.getElementById('messages'))
30
- ReactDOM.render(<Hooks />, document.getElementById('hooks'))
31
-
32
- ReactDOM.render(<Injected />, document.getElementById('injected'))
33
- ReactDOM.render(<HandleChange />, document.getElementById('handlechange'))
34
-
35
- ReactDOM.render(
36
- <StaticTypeSafetyAndCodeSplitting />,
37
- document.getElementById('static-type-safety-and-code-splitting')
38
- )
39
-
40
- ReactDOM.render(<Bug2727 />, document.getElementById('bug2727'))
41
-
42
- bootstrapApplication('en').then(app => {
43
- ReactDOM.render(app, document.getElementById('advanced'))
44
- })
@@ -1,20 +0,0 @@
1
- {
2
- "devDependencies": {
3
- "@formatjs/intl-datetimeformat": "workspace:*",
4
- "@formatjs/intl-getcanonicallocales": "workspace:*",
5
- "@formatjs/intl-locale": "workspace:*",
6
- "@formatjs/intl-numberformat": "workspace:*",
7
- "@formatjs/intl-pluralrules": "workspace:*",
8
- "@formatjs/ecma402-abstract": "workspace:*",
9
- "@formatjs/icu-messageformat-parser": "workspace:*",
10
- "@formatjs/intl": "workspace:*",
11
- "@formatjs/intl-displaynames": "workspace:*",
12
- "@formatjs/intl-listformat": "workspace:*",
13
- "@types/hoist-non-react-statics": "^3.3.1",
14
- "@types/react": "16 || 17 || 18",
15
- "hoist-non-react-statics": "^3.3.2",
16
- "intl-messageformat": "workspace:*",
17
- "react-intl": "workspace:*",
18
- "tslib": "2.4.0"
19
- }
20
- }
package/index.ts DELETED
@@ -1,127 +0,0 @@
1
- /*
2
- * Copyright 2015, Yahoo Inc.
3
- * Copyrights licensed under the New BSD License.
4
- * See the accompanying LICENSE file for terms.
5
- */
6
- import * as React from 'react'
7
- import {
8
- createFormattedComponent,
9
- createFormattedDateTimePartsComponent,
10
- } from './src/components/createFormattedComponent'
11
- import {
12
- MessageDescriptor,
13
- CustomFormatConfig,
14
- FormatDateOptions,
15
- } from '@formatjs/intl'
16
- import {IntlListFormatOptions} from '@formatjs/intl-listformat'
17
- import {DisplayNamesOptions} from '@formatjs/intl-displaynames'
18
- import {NumberFormatOptions} from '@formatjs/ecma402-abstract'
19
- import injectIntl, {
20
- Provider as RawIntlProvider,
21
- Context as IntlContext,
22
- WithIntlProps,
23
- WrappedComponentProps,
24
- } from './src/components/injectIntl'
25
- import useIntl from './src/components/useIntl'
26
- import IntlProvider, {createIntl} from './src/components/provider'
27
- import FormattedRelativeTime from './src/components/relative'
28
- import FormattedPlural from './src/components/plural'
29
- import FormattedMessage from './src/components/message'
30
- import FormattedDateTimeRange from './src/components/dateTimeRange'
31
- export {
32
- FormattedDateTimeRange,
33
- FormattedMessage,
34
- FormattedPlural,
35
- FormattedRelativeTime,
36
- IntlContext,
37
- IntlProvider,
38
- RawIntlProvider,
39
- WithIntlProps,
40
- WrappedComponentProps,
41
- createIntl,
42
- injectIntl,
43
- useIntl,
44
- }
45
- export {IntlConfig, ResolvedIntlConfig, IntlShape} from './src/types'
46
- export {
47
- createIntlCache,
48
- MessageDescriptor,
49
- IntlCache,
50
- Formatters,
51
- IntlFormatters,
52
- FormatDisplayNameOptions,
53
- FormatListOptions,
54
- FormatPluralOptions,
55
- FormatRelativeTimeOptions,
56
- FormatNumberOptions,
57
- FormatDateOptions,
58
- CustomFormatConfig,
59
- CustomFormats,
60
- UnsupportedFormatterError,
61
- InvalidConfigError,
62
- MissingDataError,
63
- MessageFormatError,
64
- MissingTranslationError,
65
- IntlErrorCode as ReactIntlErrorCode,
66
- IntlError as ReactIntlError,
67
- } from '@formatjs/intl'
68
-
69
- export function defineMessages<
70
- K extends keyof any,
71
- T = MessageDescriptor,
72
- U extends Record<K, T> = Record<K, T>
73
- >(msgs: U): U {
74
- return msgs
75
- }
76
-
77
- export function defineMessage<T extends MessageDescriptor>(msg: T): T {
78
- return msg
79
- }
80
- // IMPORTANT: Explicit here to prevent api-extractor from outputing `import('./src/types').CustomFormatConfig`
81
- export const FormattedDate: React.FC<
82
- Intl.DateTimeFormatOptions &
83
- CustomFormatConfig & {
84
- value: string | number | Date | undefined
85
- }
86
- > = createFormattedComponent('formatDate')
87
- export const FormattedTime: React.FC<
88
- Intl.DateTimeFormatOptions &
89
- CustomFormatConfig & {
90
- value: string | number | Date | undefined
91
- }
92
- > = createFormattedComponent('formatTime')
93
- // @ts-ignore issue w/ TS Intl types
94
- export const FormattedNumber: React.FC<
95
- NumberFormatOptions &
96
- CustomFormatConfig & {
97
- value: number | bigint
98
- }
99
- > = createFormattedComponent('formatNumber')
100
- export const FormattedList: React.FC<
101
- IntlListFormatOptions & {
102
- value: readonly React.ReactNode[]
103
- }
104
- > = createFormattedComponent('formatList')
105
- export const FormattedDisplayName: React.FC<
106
- DisplayNamesOptions & {
107
- value: string | number | Record<string, unknown>
108
- }
109
- > = createFormattedComponent('formatDisplayName')
110
- export const FormattedDateParts: React.FC<
111
- FormatDateOptions & {
112
- value: Parameters<Intl.DateTimeFormat['format']>[0] | string
113
- children(val: Intl.DateTimeFormatPart[]): React.ReactElement | null
114
- }
115
- > = createFormattedDateTimePartsComponent('formatDate')
116
- export const FormattedTimeParts: React.FC<
117
- FormatDateOptions & {
118
- value: Parameters<Intl.DateTimeFormat['format']>[0] | string
119
- children(val: Intl.DateTimeFormatPart[]): React.ReactElement | null
120
- }
121
- > = createFormattedDateTimePartsComponent('formatTime')
122
-
123
- export {
124
- FormattedNumberParts,
125
- FormattedListParts,
126
- } from './src/components/createFormattedComponent'
127
- export type {MessageFormatElement} from '@formatjs/icu-messageformat-parser'
package/jest.config.js DELETED
@@ -1,27 +0,0 @@
1
- module.exports = {
2
- preset: 'ts-jest',
3
- globals: {
4
- 'ts-jest': {
5
- tsconfig: 'tsconfig.json',
6
- diagnostics: false,
7
- },
8
- },
9
- testEnvironment: 'jsdom',
10
- testRegex: ['/tests/(functional|unit)/.*\\.(ts|tsx)'],
11
- testPathIgnorePatterns: [
12
- 'tests/functional/support',
13
- '/tests/unit/testUtils',
14
- '__snapshots__',
15
- ],
16
- collectCoverageFrom: ['src/**/*.{ts,tsx}'],
17
- coverageReporters: ['lcov', 'text', 'text-summary', 'html'],
18
- setupFilesAfterEnv: ['<rootDir>/tests/setup.js'],
19
- coverageThreshold: {
20
- global: {
21
- branches: 85,
22
- functions: 100,
23
- lines: 95,
24
- statements: 95,
25
- },
26
- },
27
- }
@@ -1,123 +0,0 @@
1
- import * as React from 'react'
2
- import {
3
- FormatDateOptions,
4
- FormatNumberOptions,
5
- FormatListOptions,
6
- FormatDisplayNameOptions,
7
- } from '@formatjs/intl'
8
- import {IntlShape} from '../types'
9
- import useIntl from './useIntl'
10
- import {Part} from '@formatjs/intl-listformat'
11
-
12
- enum DisplayName {
13
- formatDate = 'FormattedDate',
14
- formatTime = 'FormattedTime',
15
- formatNumber = 'FormattedNumber',
16
- formatList = 'FormattedList',
17
- // Note that this DisplayName is the locale display name, not to be confused with
18
- // the name of the enum, which is for React component display name in dev tools.
19
- formatDisplayName = 'FormattedDisplayName',
20
- }
21
-
22
- enum DisplayNameParts {
23
- formatDate = 'FormattedDateParts',
24
- formatTime = 'FormattedTimeParts',
25
- formatNumber = 'FormattedNumberParts',
26
- formatList = 'FormattedListParts',
27
- }
28
-
29
- type Formatter = {
30
- formatDate: FormatDateOptions
31
- formatTime: FormatDateOptions
32
- formatNumber: FormatNumberOptions
33
- formatList: FormatListOptions
34
- formatDisplayName: FormatDisplayNameOptions
35
- }
36
-
37
- export const FormattedNumberParts: React.FC<
38
- Formatter['formatNumber'] & {
39
- value: Parameters<IntlShape['formatNumber']>[0]
40
-
41
- children(val: Intl.NumberFormatPart[]): React.ReactElement | null
42
- }
43
- > = props => {
44
- const intl = useIntl()
45
- const {value, children, ...formatProps} = props
46
- return children(intl.formatNumberToParts(value, formatProps))
47
- }
48
- FormattedNumberParts.displayName = 'FormattedNumberParts'
49
-
50
- export const FormattedListParts: React.FC<
51
- Formatter['formatList'] & {
52
- value: Parameters<IntlShape['formatList']>[0]
53
-
54
- children(val: Part[]): React.ReactElement | null
55
- }
56
- > = props => {
57
- const intl = useIntl()
58
- const {value, children, ...formatProps} = props
59
- return children(intl.formatListToParts(value, formatProps))
60
- }
61
- FormattedNumberParts.displayName = 'FormattedNumberParts'
62
-
63
- export function createFormattedDateTimePartsComponent<
64
- Name extends 'formatDate' | 'formatTime'
65
- >(
66
- name: Name
67
- ): React.FC<
68
- Formatter[Name] & {
69
- value: Parameters<IntlShape[Name]>[0]
70
- children(val: Intl.DateTimeFormatPart[]): React.ReactElement | null
71
- }
72
- > {
73
- type FormatFn = IntlShape[Name]
74
- type Props = Formatter[Name] & {
75
- value: Parameters<FormatFn>[0]
76
- children(val: Intl.DateTimeFormatPart[]): React.ReactElement | null
77
- }
78
-
79
- const ComponentParts: React.FC<Props> = props => {
80
- const intl = useIntl()
81
- const {value, children, ...formatProps} = props
82
- const date = typeof value === 'string' ? new Date(value || 0) : value
83
- const formattedParts: Intl.DateTimeFormatPart[] =
84
- name === 'formatDate'
85
- ? intl.formatDateToParts(date, formatProps)
86
- : intl.formatTimeToParts(date, formatProps)
87
-
88
- return children(formattedParts)
89
- }
90
- ComponentParts.displayName = DisplayNameParts[name]
91
- return ComponentParts
92
- }
93
-
94
- export function createFormattedComponent<Name extends keyof Formatter>(
95
- name: Name
96
- ): React.FC<
97
- Formatter[Name] & {
98
- value: Parameters<IntlShape[Name]>[0]
99
- children?(val: string): React.ReactElement | null
100
- }
101
- > {
102
- type FormatFn = IntlShape[Name]
103
- type Props = Formatter[Name] & {
104
- value: Parameters<FormatFn>[0]
105
- children?(val: string): React.ReactElement | null
106
- }
107
-
108
- const Component: React.FC<Props> = props => {
109
- const intl = useIntl()
110
- const {value, children, ...formatProps} = props
111
- // TODO: fix TS type definition for localeMatcher upstream
112
- const formattedValue = intl[name](value as any, formatProps as any)
113
-
114
- if (typeof children === 'function') {
115
- return children(formattedValue as any)
116
- }
117
- const Text = intl.textComponent || React.Fragment
118
- return <Text>{formattedValue}</Text>
119
- }
120
-
121
- Component.displayName = DisplayName[name]
122
- return Component
123
- }
@@ -1,26 +0,0 @@
1
- import * as React from 'react'
2
- import {FormatDateOptions} from '@formatjs/intl'
3
- import {DateTimeFormat} from '@formatjs/ecma402-abstract'
4
- import useIntl from './useIntl'
5
-
6
- interface Props extends FormatDateOptions {
7
- from: Parameters<DateTimeFormat['formatRange']>[0]
8
- to: Parameters<DateTimeFormat['formatRange']>[1]
9
- children?(value: React.ReactNode): React.ReactElement | null
10
- }
11
-
12
- const FormattedDateTimeRange: React.FC<Props> = props => {
13
- const intl = useIntl()
14
-
15
- const {from, to, children, ...formatProps} = props
16
- const formattedValue = intl.formatDateTimeRange(from, to, formatProps)
17
-
18
- if (typeof children === 'function') {
19
- return children(formattedValue as any)
20
- }
21
- const Text = intl.textComponent || React.Fragment
22
- return <Text>{formattedValue}</Text>
23
- }
24
-
25
- FormattedDateTimeRange.displayName = 'FormattedDateTimeRange'
26
- export default FormattedDateTimeRange