@react-navigation/core 7.8.5 → 7.9.0

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 (43) hide show
  1. package/lib/module/BaseNavigationContainer.js +3 -3
  2. package/lib/module/BaseNavigationContainer.js.map +1 -1
  3. package/lib/module/EnsureSingleNavigator.js +1 -1
  4. package/lib/module/EnsureSingleNavigator.js.map +1 -1
  5. package/lib/module/SceneView.js +1 -1
  6. package/lib/module/SceneView.js.map +1 -1
  7. package/lib/module/index.js.map +1 -1
  8. package/lib/module/types.js.map +1 -1
  9. package/lib/module/useDescriptors.js +1 -0
  10. package/lib/module/useDescriptors.js.map +1 -1
  11. package/lib/module/useFocusEvents.js +1 -1
  12. package/lib/module/useFocusEvents.js.map +1 -1
  13. package/lib/module/useLazyValue.js +1 -1
  14. package/lib/module/useLazyValue.js.map +1 -1
  15. package/lib/module/useNavigationBuilder.js +19 -7
  16. package/lib/module/useNavigationBuilder.js.map +1 -1
  17. package/lib/typescript/src/StaticNavigation.d.ts +4 -4
  18. package/lib/typescript/src/StaticNavigation.d.ts.map +1 -1
  19. package/lib/typescript/src/deepFreeze.d.ts.map +1 -1
  20. package/lib/typescript/src/index.d.ts +1 -1
  21. package/lib/typescript/src/index.d.ts.map +1 -1
  22. package/lib/typescript/src/types.d.ts +10 -18
  23. package/lib/typescript/src/types.d.ts.map +1 -1
  24. package/lib/typescript/src/useDescriptors.d.ts +16 -5
  25. package/lib/typescript/src/useDescriptors.d.ts.map +1 -1
  26. package/lib/typescript/src/useNavigationBuilder.d.ts +18 -3
  27. package/lib/typescript/src/useNavigationBuilder.d.ts.map +1 -1
  28. package/lib/typescript/src/useNavigationCache.d.ts +6 -1
  29. package/lib/typescript/src/useNavigationCache.d.ts.map +1 -1
  30. package/lib/typescript/src/useNavigationHelpers.d.ts +6 -1
  31. package/lib/typescript/src/useNavigationHelpers.d.ts.map +1 -1
  32. package/lib/typescript/src/useOnPreventRemove.d.ts.map +1 -1
  33. package/package.json +17 -17
  34. package/src/BaseNavigationContainer.tsx +3 -3
  35. package/src/EnsureSingleNavigator.tsx +1 -1
  36. package/src/SceneView.tsx +1 -1
  37. package/src/StaticNavigation.tsx +4 -4
  38. package/src/index.tsx +2 -0
  39. package/src/types.tsx +32 -18
  40. package/src/useDescriptors.tsx +5 -3
  41. package/src/useFocusEvents.tsx +1 -1
  42. package/src/useLazyValue.tsx +1 -1
  43. package/src/useNavigationBuilder.tsx +44 -13
@@ -57,7 +57,27 @@ type NavigatorRoute = {
57
57
  params?: NavigatorScreenParams<ParamListBase>;
58
58
  };
59
59
 
60
- const isValidKey = (key: unknown) =>
60
+ const isScreen = (
61
+ child: React.ReactElement<unknown>
62
+ ): child is React.ReactElement<{
63
+ name?: unknown;
64
+ navigationKey?: unknown;
65
+ }> => {
66
+ return child.type === Screen;
67
+ };
68
+
69
+ const isGroup = (
70
+ child: React.ReactElement<unknown>
71
+ ): child is React.ReactElement<{
72
+ navigationKey?: unknown;
73
+ screenOptions?: unknown;
74
+ screenLayout?: unknown;
75
+ children?: unknown;
76
+ }> => {
77
+ return child.type === React.Fragment || child.type === Group;
78
+ };
79
+
80
+ const isValidKey = (key: unknown): key is string | undefined =>
61
81
  key === undefined || (typeof key === 'string' && key !== '');
62
82
 
63
83
  /**
@@ -83,11 +103,27 @@ const getRouteConfigsFromChildren = <
83
103
  ScreenConfigWithParent<State, ScreenOptions, EventMap>[]
84
104
  >((acc, child) => {
85
105
  if (React.isValidElement(child)) {
86
- if (child.type === Screen) {
106
+ if (isScreen(child)) {
87
107
  // We can only extract the config from `Screen` elements
88
108
  // If something else was rendered, it's probably a bug
89
109
 
90
- if (!isValidKey(child.props.navigationKey)) {
110
+ if (typeof child.props !== 'object' || child.props === null) {
111
+ throw new Error(`Got an invalid element for screen.`);
112
+ }
113
+
114
+ if (typeof child.props.name !== 'string' || child.props.name === '') {
115
+ throw new Error(
116
+ `Got an invalid name (${JSON.stringify(
117
+ child.props.name
118
+ )}) for the screen. It must be a non-empty string.`
119
+ );
120
+ }
121
+
122
+ if (
123
+ child.props.navigationKey !== undefined &&
124
+ (typeof child.props.navigationKey !== 'string' ||
125
+ child.props.navigationKey === '')
126
+ ) {
91
127
  throw new Error(
92
128
  `Got an invalid 'navigationKey' prop (${JSON.stringify(
93
129
  child.props.navigationKey
@@ -114,7 +150,7 @@ const getRouteConfigsFromChildren = <
114
150
  return acc;
115
151
  }
116
152
 
117
- if (child.type === React.Fragment || child.type === Group) {
153
+ if (isGroup(child)) {
118
154
  if (!isValidKey(child.props.navigationKey)) {
119
155
  throw new Error(
120
156
  `Got an invalid 'navigationKey' prop (${JSON.stringify(
@@ -127,8 +163,10 @@ const getRouteConfigsFromChildren = <
127
163
  // This is handy to conditionally define a group of screens
128
164
  acc.push(
129
165
  ...getRouteConfigsFromChildren<State, ScreenOptions, EventMap>(
130
- child.props.children,
166
+ child.props.children as React.ReactNode,
131
167
  child.props.navigationKey,
168
+ // FIXME
169
+ // @ts-expect-error: add validation
132
170
  child.type !== Group
133
171
  ? groupOptions
134
172
  : groupOptions != null
@@ -139,6 +177,7 @@ const getRouteConfigsFromChildren = <
139
177
  : groupLayout
140
178
  )
141
179
  );
180
+
142
181
  return acc;
143
182
  }
144
183
  }
@@ -167,14 +206,6 @@ const getRouteConfigsFromChildren = <
167
206
  configs.forEach((config) => {
168
207
  const { name, children, component, getComponent } = config.props;
169
208
 
170
- if (typeof name !== 'string' || !name) {
171
- throw new Error(
172
- `Got an invalid name (${JSON.stringify(
173
- name
174
- )}) for the screen. It must be a non-empty string.`
175
- );
176
- }
177
-
178
209
  if (
179
210
  children != null ||
180
211
  component !== undefined ||