@inertiajs/react 2.2.8 → 2.2.9

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.js CHANGED
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -57,13 +58,13 @@ var import_react3 = require("react");
57
58
 
58
59
  // src/HeadContext.ts
59
60
  var import_react = require("react");
60
- var headContext = (0, import_react.createContext)(void 0);
61
+ var headContext = (0, import_react.createContext)(null);
61
62
  headContext.displayName = "InertiaHeadContext";
62
63
  var HeadContext_default = headContext;
63
64
 
64
65
  // src/PageContext.ts
65
66
  var import_react2 = require("react");
66
- var pageContext = (0, import_react2.createContext)(void 0);
67
+ var pageContext = (0, import_react2.createContext)(null);
67
68
  pageContext.displayName = "InertiaPageContext";
68
69
  var PageContext_default = pageContext;
69
70
 
@@ -123,13 +124,13 @@ function App({
123
124
  (0, import_react3.createElement)(PageContext_default.Provider, { value: current.page }, null)
124
125
  );
125
126
  }
126
- const renderChildren = children || (({ Component, props, key }) => {
127
- const child = (0, import_react3.createElement)(Component, { key, ...props });
128
- if (typeof Component.layout === "function") {
129
- return Component.layout(child);
127
+ const renderChildren = children || (({ component, props, key }) => {
128
+ const child = (0, import_react3.createElement)(component, { key, ...props });
129
+ if (typeof component.layout === "function") {
130
+ return component.layout(child);
130
131
  }
131
- if (Array.isArray(Component.layout)) {
132
- return Component.layout.concat(child).reverse().reduce((children2, Layout) => (0, import_react3.createElement)(Layout, { children: children2, ...props }));
132
+ if (Array.isArray(component.layout)) {
133
+ return component.layout.concat(child).reverse().reduce((children2, Layout) => (0, import_react3.createElement)(Layout, { children: children2, ...props }));
133
134
  }
134
135
  return child;
135
136
  });
@@ -140,7 +141,7 @@ function App({
140
141
  PageContext_default.Provider,
141
142
  { value: current.page },
142
143
  renderChildren({
143
- Component: current.component,
144
+ component: current.component,
144
145
  key: current.key,
145
146
  props: current.page.props
146
147
  })
@@ -161,7 +162,7 @@ async function createInertiaApp({
161
162
  }) {
162
163
  const isServer = typeof window === "undefined";
163
164
  const el = isServer ? null : document.getElementById(id);
164
- const initialPage = page || JSON.parse(el.dataset.page);
165
+ const initialPage = page || JSON.parse(el?.dataset.page || "{}");
165
166
  const resolveComponent = (name) => Promise.resolve(resolve(name)).then((module2) => module2.default || module2);
166
167
  let head = [];
167
168
  const reactApp = await Promise.all([
@@ -169,23 +170,31 @@ async function createInertiaApp({
169
170
  import_core2.router.decryptHistory().catch(() => {
170
171
  })
171
172
  ]).then(([initialComponent]) => {
172
- return setup({
173
- // @ts-expect-error
173
+ const props = {
174
+ initialPage,
175
+ initialComponent,
176
+ resolveComponent,
177
+ titleCallback: title
178
+ };
179
+ if (isServer) {
180
+ const ssrSetup = setup;
181
+ return ssrSetup({
182
+ el: null,
183
+ App,
184
+ props: { ...props, onHeadUpdate: (elements) => head = elements }
185
+ });
186
+ }
187
+ const csrSetup = setup;
188
+ return csrSetup({
174
189
  el,
175
190
  App,
176
- props: {
177
- initialPage,
178
- initialComponent,
179
- resolveComponent,
180
- titleCallback: title,
181
- onHeadUpdate: isServer ? (elements) => head = elements : null
182
- }
191
+ props
183
192
  });
184
193
  });
185
194
  if (!isServer && progress2) {
186
195
  (0, import_core2.setupProgress)(progress2);
187
196
  }
188
- if (isServer) {
197
+ if (isServer && render) {
189
198
  const body = await render(
190
199
  (0, import_react4.createElement)(
191
200
  "div",
@@ -193,7 +202,6 @@ async function createInertiaApp({
193
202
  id,
194
203
  "data-page": JSON.stringify(initialPage)
195
204
  },
196
- // @ts-expect-error
197
205
  reactApp
198
206
  )
199
207
  );
@@ -285,13 +293,13 @@ function useRemember(initialState, key) {
285
293
 
286
294
  // src/useForm.ts
287
295
  function useForm(rememberKeyOrInitialValues, maybeInitialValues) {
288
- const isMounted = (0, import_react9.useRef)(null);
296
+ const isMounted = (0, import_react9.useRef)(false);
289
297
  const rememberKey = typeof rememberKeyOrInitialValues === "string" ? rememberKeyOrInitialValues : null;
290
298
  const [defaults, setDefaults] = (0, import_react9.useState)(
291
299
  (typeof rememberKeyOrInitialValues === "string" ? maybeInitialValues : rememberKeyOrInitialValues) || {}
292
300
  );
293
301
  const cancelToken = (0, import_react9.useRef)(null);
294
- const recentlySuccessfulTimeoutId = (0, import_react9.useRef)(null);
302
+ const recentlySuccessfulTimeoutId = (0, import_react9.useRef)(void 0);
295
303
  const [data, setData] = rememberKey ? useRemember(defaults, `${rememberKey}:data`) : (0, import_react9.useState)(defaults);
296
304
  const [errors, setErrors] = rememberKey ? useRemember({}, `${rememberKey}:errors`) : (0, import_react9.useState)({});
297
305
  const [hasErrors, setHasErrors] = (0, import_react9.useState)(false);
@@ -338,7 +346,7 @@ function useForm(rememberKeyOrInitialValues, maybeInitialValues) {
338
346
  }
339
347
  },
340
348
  onProgress: (event) => {
341
- setProgress(event);
349
+ setProgress(event || null);
342
350
  if (options.onProgress) {
343
351
  return options.onProgress(event);
344
352
  }
@@ -397,10 +405,11 @@ function useForm(rememberKeyOrInitialValues, maybeInitialValues) {
397
405
  }
398
406
  }
399
407
  };
408
+ const transformedData = transform.current(data);
400
409
  if (method === "delete") {
401
- import_core4.router.delete(url, { ..._options, data: transform.current(data) });
410
+ import_core4.router.delete(url, { ..._options, data: transformedData });
402
411
  } else {
403
- import_core4.router[method](url, transform.current(data), _options);
412
+ import_core4.router[method](url, transformedData, _options);
404
413
  }
405
414
  },
406
415
  [data, setErrors, transform]
@@ -498,7 +507,7 @@ function useForm(rememberKeyOrInitialValues, maybeInitialValues) {
498
507
  },
499
508
  [reset, clearErrors]
500
509
  );
501
- const createSubmitMethod = (method) => (url, options) => {
510
+ const createSubmitMethod = (method) => (url, options = {}) => {
502
511
  submit(method, url, options);
503
512
  };
504
513
  const getMethod = (0, import_react9.useCallback)(createSubmitMethod("get"), [submit]);
@@ -573,7 +582,7 @@ var Form = (0, import_react11.forwardRef)(
573
582
  ...props
574
583
  }, ref) => {
575
584
  const form = useForm({});
576
- const formElement = (0, import_react11.useRef)(null);
585
+ const formElement = (0, import_react11.useRef)(void 0);
577
586
  const resolvedMethod = (0, import_react11.useMemo)(() => {
578
587
  return (0, import_core5.isUrlMethodPair)(action) ? action.method : method.toLowerCase();
579
588
  }, [action, method]);
@@ -591,7 +600,9 @@ var Form = (0, import_react11.forwardRef)(
591
600
  return () => formEvents.forEach((e) => formElement.current?.removeEventListener(e, updateDirtyState));
592
601
  }, []);
593
602
  const reset = (...fields) => {
594
- (0, import_core5.resetFormFields)(formElement.current, defaultData.current, fields);
603
+ if (formElement.current) {
604
+ (0, import_core5.resetFormFields)(formElement.current, defaultData.current, fields);
605
+ }
595
606
  };
596
607
  const resetAndClearErrors = (...fields) => {
597
608
  form.clearErrors(...fields);
@@ -704,7 +715,7 @@ var Head = function({ children, title }) {
704
715
  };
705
716
  }, [provider, children, title]);
706
717
  function isUnaryTag(node) {
707
- return [
718
+ return typeof node.type === "string" && [
708
719
  "area",
709
720
  "base",
710
721
  "br",
@@ -730,14 +741,20 @@ var Head = function({ children, title }) {
730
741
  const value = String(node.props[name]);
731
742
  if (value === "") {
732
743
  return carry + ` ${name}`;
733
- } else {
734
- return carry + ` ${name}="${(0, import_lodash_es3.escape)(value)}"`;
735
744
  }
745
+ return carry + ` ${name}="${(0, import_lodash_es3.escape)(value)}"`;
736
746
  }, "");
737
- return `<${node.type}${attrs}>`;
747
+ return `<${String(node.type)}${attrs}>`;
738
748
  }
739
749
  function renderTagChildren(node) {
740
- return typeof node.props.children === "string" ? node.props.children : node.props.children.reduce((html, child) => html + renderTag(child), "");
750
+ const { children: children2 } = node.props;
751
+ if (typeof children2 === "string") {
752
+ return children2;
753
+ }
754
+ if (Array.isArray(children2)) {
755
+ return children2.reduce((html, child) => html + renderTag(child), "");
756
+ }
757
+ return "";
741
758
  }
742
759
  function renderTag(node) {
743
760
  let html = renderTagStart(node);
@@ -748,7 +765,7 @@ var Head = function({ children, title }) {
748
765
  html += node.props.dangerouslySetInnerHTML.__html;
749
766
  }
750
767
  if (!isUnaryTag(node)) {
751
- html += `</${node.type}>`;
768
+ html += `</${String(node.type)}>`;
752
769
  }
753
770
  return html;
754
771
  }
@@ -761,11 +778,11 @@ var Head = function({ children, title }) {
761
778
  return renderTag(ensureNodeHasInertiaProp(node));
762
779
  }
763
780
  function renderNodes(nodes) {
764
- const computed = import_react12.default.Children.toArray(nodes).filter((node) => node).map((node) => renderNode(node));
765
- if (title && !computed.find((tag) => tag.startsWith("<title"))) {
766
- computed.push(`<title inertia>${title}</title>`);
781
+ const elements = import_react12.default.Children.toArray(nodes).filter((node) => node).map((node) => renderNode(node));
782
+ if (title && !elements.find((tag) => tag.startsWith("<title"))) {
783
+ elements.push(`<title inertia>${title}</title>`);
767
784
  }
768
- return computed;
785
+ return elements;
769
786
  }
770
787
  if (isServer) {
771
788
  provider.update(renderNodes(children));
@@ -1041,7 +1058,7 @@ var Link = (0, import_react14.forwardRef)(
1041
1058
  ...props
1042
1059
  }, ref) => {
1043
1060
  const [inFlightCount, setInFlightCount] = (0, import_react14.useState)(0);
1044
- const hoverTimeout = (0, import_react14.useRef)(null);
1061
+ const hoverTimeout = (0, import_react14.useRef)(void 0);
1045
1062
  const _method = (0, import_react14.useMemo)(() => {
1046
1063
  return (0, import_core7.isUrlMethodPair)(href) ? href.method : method.toLowerCase();
1047
1064
  }, [href, method]);
@@ -1167,7 +1184,7 @@ var Link = (0, import_react14.forwardRef)(
1167
1184
  }
1168
1185
  },
1169
1186
  onKeyDown: (event) => {
1170
- if ((0, import_core7.shouldIntercept)(event) && (0, import_core7.shouldNavigate)(event)) {
1187
+ if ((0, import_core7.shouldNavigate)(event)) {
1171
1188
  event.preventDefault();
1172
1189
  doPrefetch();
1173
1190
  }