@lingui/react 4.0.0-next.4 → 4.0.0-next.6

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.
package/dist/index.cjs CHANGED
@@ -89,6 +89,9 @@ function formatElements(value, elements = {}) {
89
89
  }
90
90
  element = React.createElement(React.Fragment);
91
91
  }
92
+ if (Array.isArray(element)) {
93
+ element = React.createElement(React.Fragment, {}, element);
94
+ }
92
95
  tree.push(
93
96
  React.cloneElement(
94
97
  element,
@@ -121,7 +124,8 @@ function Trans(props) {
121
124
  if (values) {
122
125
  Object.keys(values).forEach((key) => {
123
126
  const value = values[key];
124
- if (!React.isValidElement(value))
127
+ const valueIsReactEl = React.isValidElement(value) || Array.isArray(value) && value.every((el) => React.isValidElement(el));
128
+ if (!valueIsReactEl)
125
129
  return;
126
130
  const index = Object.keys(components).length;
127
131
  components[index] = value;
@@ -133,12 +137,14 @@ function Trans(props) {
133
137
  if (render === null || component === null) {
134
138
  return translation;
135
139
  }
136
- const FallbackComponent = defaultComponent || React.Fragment;
140
+ const FallbackComponent = defaultComponent || RenderFragment;
137
141
  const i18nProps = {
138
142
  id,
139
143
  message,
140
144
  translation,
141
- isTranslated: id !== translation && message !== translation
145
+ isTranslated: id !== translation && message !== translation,
146
+ children: translation
147
+ // for type-compatibility with `component` prop
142
148
  };
143
149
  if (render && component) {
144
150
  console.error(
@@ -152,15 +158,18 @@ function Trans(props) {
152
158
  console.error(
153
159
  `Invalid value supplied to prop \`component\`. It must be a React component, provided ${component}`
154
160
  );
155
- return /* @__PURE__ */ React.createElement(FallbackComponent, { ...i18nProps }, translation);
161
+ return React.createElement(FallbackComponent, i18nProps, translation);
156
162
  }
157
163
  if (typeof render === "function") {
158
164
  return render(i18nProps);
159
165
  }
160
166
  const Component = component || FallbackComponent;
161
- const DefaultComponent = defaultComponent;
162
- return DefaultComponent && !component ? /* @__PURE__ */ React.createElement(DefaultComponent, { ...i18nProps }, translation) : /* @__PURE__ */ React.createElement(Component, null, translation);
167
+ const RenderedComponent = defaultComponent && !component ? defaultComponent : Component;
168
+ return React.createElement(RenderedComponent, i18nProps, translation);
163
169
  }
170
+ const RenderFragment = ({ children }) => {
171
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, children);
172
+ };
164
173
 
165
174
  exports.I18nProvider = I18nProvider;
166
175
  exports.LinguiContext = LinguiContext;
package/dist/index.d.ts CHANGED
@@ -2,12 +2,15 @@ import React, { ComponentType, FunctionComponent } from 'react';
2
2
  import { I18n } from '@lingui/core';
3
3
 
4
4
  type TransRenderProps = {
5
- id?: string;
6
- translation?: React.ReactNode;
7
- children?: React.ReactNode;
5
+ id: string;
6
+ translation: React.ReactNode;
7
+ children: React.ReactNode;
8
8
  message?: string | null;
9
- isTranslated?: boolean;
9
+ isTranslated: boolean;
10
10
  };
11
+ type MaximumOneOf<T, K extends keyof T = keyof T> = K extends keyof T ? {
12
+ [P in K]?: T[K];
13
+ } & Partial<Record<Exclude<keyof T, K>, never>> : never;
11
14
  type TransProps = {
12
15
  id: string;
13
16
  message?: string;
@@ -17,9 +20,10 @@ type TransProps = {
17
20
  };
18
21
  formats?: Record<string, unknown>;
19
22
  children?: React.ReactNode;
23
+ } & MaximumOneOf<{
20
24
  component?: React.ComponentType<TransRenderProps>;
21
25
  render?: (props: TransRenderProps) => React.ReactElement<any, any> | null;
22
- };
26
+ }>;
23
27
  declare function Trans(props: TransProps): React.ReactElement<any, any> | null;
24
28
 
25
29
  type I18nContext = {
package/dist/index.mjs CHANGED
@@ -87,6 +87,9 @@ function formatElements(value, elements = {}) {
87
87
  }
88
88
  element = React.createElement(React.Fragment);
89
89
  }
90
+ if (Array.isArray(element)) {
91
+ element = React.createElement(React.Fragment, {}, element);
92
+ }
90
93
  tree.push(
91
94
  React.cloneElement(
92
95
  element,
@@ -119,7 +122,8 @@ function Trans(props) {
119
122
  if (values) {
120
123
  Object.keys(values).forEach((key) => {
121
124
  const value = values[key];
122
- if (!React.isValidElement(value))
125
+ const valueIsReactEl = React.isValidElement(value) || Array.isArray(value) && value.every((el) => React.isValidElement(el));
126
+ if (!valueIsReactEl)
123
127
  return;
124
128
  const index = Object.keys(components).length;
125
129
  components[index] = value;
@@ -131,12 +135,14 @@ function Trans(props) {
131
135
  if (render === null || component === null) {
132
136
  return translation;
133
137
  }
134
- const FallbackComponent = defaultComponent || React.Fragment;
138
+ const FallbackComponent = defaultComponent || RenderFragment;
135
139
  const i18nProps = {
136
140
  id,
137
141
  message,
138
142
  translation,
139
- isTranslated: id !== translation && message !== translation
143
+ isTranslated: id !== translation && message !== translation,
144
+ children: translation
145
+ // for type-compatibility with `component` prop
140
146
  };
141
147
  if (render && component) {
142
148
  console.error(
@@ -150,14 +156,17 @@ function Trans(props) {
150
156
  console.error(
151
157
  `Invalid value supplied to prop \`component\`. It must be a React component, provided ${component}`
152
158
  );
153
- return /* @__PURE__ */ React.createElement(FallbackComponent, { ...i18nProps }, translation);
159
+ return React.createElement(FallbackComponent, i18nProps, translation);
154
160
  }
155
161
  if (typeof render === "function") {
156
162
  return render(i18nProps);
157
163
  }
158
164
  const Component = component || FallbackComponent;
159
- const DefaultComponent = defaultComponent;
160
- return DefaultComponent && !component ? /* @__PURE__ */ React.createElement(DefaultComponent, { ...i18nProps }, translation) : /* @__PURE__ */ React.createElement(Component, null, translation);
165
+ const RenderedComponent = defaultComponent && !component ? defaultComponent : Component;
166
+ return React.createElement(RenderedComponent, i18nProps, translation);
161
167
  }
168
+ const RenderFragment = ({ children }) => {
169
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, children);
170
+ };
162
171
 
163
172
  export { I18nProvider, LinguiContext, Trans, useLingui };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lingui/react",
3
- "version": "4.0.0-next.4",
3
+ "version": "4.0.0-next.6",
4
4
  "sideEffects": false,
5
5
  "description": "React components for translations",
6
6
  "main": "./dist/index.cjs",
@@ -58,11 +58,14 @@
58
58
  },
59
59
  "dependencies": {
60
60
  "@babel/runtime": "^7.20.13",
61
- "@lingui/core": "^4.0.0-next.4"
61
+ "@lingui/core": "^4.0.0-next.6"
62
62
  },
63
63
  "devDependencies": {
64
+ "@lingui/jest-mocks": "^3.0.3",
64
65
  "@testing-library/react": "^11.0.4",
66
+ "react": "^17.0.2",
67
+ "react-dom": "^17.0.2",
65
68
  "unbuild": "^1.1.2"
66
69
  },
67
- "gitHead": "3b999e35d26e67dec7cf0591f794be782e11022c"
70
+ "gitHead": "d4d9e149b2419d1796e14176d53703a7cff4b82c"
68
71
  }