@faasjs/react 3.7.0-beta.2 → 3.7.0-beta.4

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.d.mts CHANGED
@@ -153,7 +153,7 @@ declare function createSplittingContext<T extends Record<string, any>>(defaultVa
153
153
  use: <NewT extends T = T>() => Readonly<NewT>;
154
154
  };
155
155
 
156
- type SetPrefix<S extends string | number | symbol> = S extends string ? (S extends `${infer First}${infer Rest}` ? `set${Capitalize<First>}${Rest}` : never) : never;
156
+ type SetPrefix<S extends string | number | symbol> = S extends string ? S extends `${infer First}${infer Rest}` ? `set${Capitalize<First>}${Rest}` : never : never;
157
157
  type StateSetters<T> = {
158
158
  [K in keyof T as SetPrefix<K>]: Dispatch<SetStateAction<T[K]>>;
159
159
  };
@@ -374,9 +374,10 @@ declare const OptionalWrapper: React.FC<OptionalWrapperProps> & {
374
374
  whyDidYouRender: boolean;
375
375
  };
376
376
 
377
- type FormRules = {
378
- type?: 'string' | 'number';
379
- required?: boolean;
377
+ type FormButtonElementProps = {
378
+ children?: React.ReactNode;
379
+ disabled?: boolean;
380
+ submit?: () => void;
380
381
  };
381
382
 
382
383
  type FormInputElementProps = {
@@ -385,6 +386,11 @@ type FormInputElementProps = {
385
386
  onChange: (value: any) => void;
386
387
  };
387
388
 
389
+ type FormRules = {
390
+ type?: 'string' | 'number';
391
+ required?: boolean;
392
+ };
393
+
388
394
  type InferFormInputProps<T extends ComponentType<FormInputElementProps> | JSXElementConstructor<any>> = T extends ComponentType<FormInputElementProps> ? Omit<ComponentProps<T>, 'name' | 'value' | 'onChange'> : Omit<ComponentProps<T>, 'name' | 'value'>;
389
395
  type FormInputProps<FormElements extends FormElementTypes = FormElementTypes> = {
390
396
  Input?: ComponentType<FormInputElementProps>;
@@ -400,12 +406,6 @@ type FormLabelElementProps<FormElements extends FormElementTypes = FormElementTy
400
406
  input?: FormInputProps<FormElements>;
401
407
  };
402
408
 
403
- type FormButtonElementProps = {
404
- children?: React.ReactNode;
405
- disabled?: boolean;
406
- onClick?: () => void;
407
- };
408
-
409
409
  type FormElementTypes = {
410
410
  Label: ComponentType<FormLabelElementProps>;
411
411
  Input: ComponentType<FormInputElementProps>;
@@ -414,7 +414,7 @@ type FormElementTypes = {
414
414
  declare const FormDefaultElements: FormElementTypes;
415
415
 
416
416
  type FormProps<Values extends Record<string, any> = Record<string, any>, FormElements extends FormElementTypes = FormElementTypes> = {
417
- items: FormLabelElementProps[];
417
+ items: FormLabelElementProps<FormElements>[];
418
418
  onSubmit?: (values: Values) => Promise<void>;
419
419
  elements?: Partial<FormElements>;
420
420
  defaultValues?: Values;
package/dist/index.d.ts CHANGED
@@ -153,7 +153,7 @@ declare function createSplittingContext<T extends Record<string, any>>(defaultVa
153
153
  use: <NewT extends T = T>() => Readonly<NewT>;
154
154
  };
155
155
 
156
- type SetPrefix<S extends string | number | symbol> = S extends string ? (S extends `${infer First}${infer Rest}` ? `set${Capitalize<First>}${Rest}` : never) : never;
156
+ type SetPrefix<S extends string | number | symbol> = S extends string ? S extends `${infer First}${infer Rest}` ? `set${Capitalize<First>}${Rest}` : never : never;
157
157
  type StateSetters<T> = {
158
158
  [K in keyof T as SetPrefix<K>]: Dispatch<SetStateAction<T[K]>>;
159
159
  };
@@ -374,9 +374,10 @@ declare const OptionalWrapper: React.FC<OptionalWrapperProps> & {
374
374
  whyDidYouRender: boolean;
375
375
  };
376
376
 
377
- type FormRules = {
378
- type?: 'string' | 'number';
379
- required?: boolean;
377
+ type FormButtonElementProps = {
378
+ children?: React.ReactNode;
379
+ disabled?: boolean;
380
+ submit?: () => void;
380
381
  };
381
382
 
382
383
  type FormInputElementProps = {
@@ -385,6 +386,11 @@ type FormInputElementProps = {
385
386
  onChange: (value: any) => void;
386
387
  };
387
388
 
389
+ type FormRules = {
390
+ type?: 'string' | 'number';
391
+ required?: boolean;
392
+ };
393
+
388
394
  type InferFormInputProps<T extends ComponentType<FormInputElementProps> | JSXElementConstructor<any>> = T extends ComponentType<FormInputElementProps> ? Omit<ComponentProps<T>, 'name' | 'value' | 'onChange'> : Omit<ComponentProps<T>, 'name' | 'value'>;
389
395
  type FormInputProps<FormElements extends FormElementTypes = FormElementTypes> = {
390
396
  Input?: ComponentType<FormInputElementProps>;
@@ -400,12 +406,6 @@ type FormLabelElementProps<FormElements extends FormElementTypes = FormElementTy
400
406
  input?: FormInputProps<FormElements>;
401
407
  };
402
408
 
403
- type FormButtonElementProps = {
404
- children?: React.ReactNode;
405
- disabled?: boolean;
406
- onClick?: () => void;
407
- };
408
-
409
409
  type FormElementTypes = {
410
410
  Label: ComponentType<FormLabelElementProps>;
411
411
  Input: ComponentType<FormInputElementProps>;
@@ -414,7 +414,7 @@ type FormElementTypes = {
414
414
  declare const FormDefaultElements: FormElementTypes;
415
415
 
416
416
  type FormProps<Values extends Record<string, any> = Record<string, any>, FormElements extends FormElementTypes = FormElementTypes> = {
417
- items: FormLabelElementProps[];
417
+ items: FormLabelElementProps<FormElements>[];
418
418
  onSubmit?: (values: Values) => Promise<void>;
419
419
  elements?: Partial<FormElements>;
420
420
  defaultValues?: Values;
package/dist/index.js CHANGED
@@ -4,37 +4,7 @@ var react = require('react');
4
4
  var jsxRuntime = require('react/jsx-runtime');
5
5
  var browser = require('@faasjs/browser');
6
6
 
7
- var __defProp = Object.defineProperty;
8
- var __defProps = Object.defineProperties;
9
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
10
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
11
- var __hasOwnProp = Object.prototype.hasOwnProperty;
12
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
13
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
14
- var __spreadValues = (a, b) => {
15
- for (var prop in b || (b = {}))
16
- if (__hasOwnProp.call(b, prop))
17
- __defNormalProp(a, prop, b[prop]);
18
- if (__getOwnPropSymbols)
19
- for (var prop of __getOwnPropSymbols(b)) {
20
- if (__propIsEnum.call(b, prop))
21
- __defNormalProp(a, prop, b[prop]);
22
- }
23
- return a;
24
- };
25
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
26
- var __objRest = (source, exclude) => {
27
- var target = {};
28
- for (var prop in source)
29
- if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
30
- target[prop] = source[prop];
31
- if (source != null && __getOwnPropSymbols)
32
- for (var prop of __getOwnPropSymbols(source)) {
33
- if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
34
- target[prop] = source[prop];
35
- }
36
- return target;
37
- };
7
+ // src/constant.ts
38
8
  function useConstant(fn) {
39
9
  const ref = react.useRef();
40
10
  if (!ref.current) {
@@ -118,14 +88,13 @@ function createSplittingContext(defaultValue) {
118
88
  const contexts = {};
119
89
  for (const key of keys) contexts[key] = react.createContext(defaultValues[key]);
120
90
  function Provider(props) {
121
- var _a, _b;
122
91
  let children = props.memo ? useEqualMemo(
123
92
  () => props.children,
124
93
  props.memo === true ? [] : props.memo
125
94
  ) : props.children;
126
95
  for (const key of keys) {
127
96
  const Context = contexts[key];
128
- const value = (_b = (_a = props.value) == null ? void 0 : _a[key]) != null ? _b : defaultValues[key];
97
+ const value = props.value?.[key] ?? defaultValues[key];
129
98
  children = /* @__PURE__ */ jsxRuntime.jsx(Context.Provider, { value, children });
130
99
  }
131
100
  return children;
@@ -153,7 +122,10 @@ function useSplittingState(initialStates) {
153
122
  const states = {};
154
123
  for (const key of Object.keys(initialStates)) {
155
124
  const state = react.useState(initialStates[key]);
156
- Object.assign(states, { [key]: state[0], [`set${String(key).charAt(0).toUpperCase()}${String(key).slice(1)}`]: state[1] });
125
+ Object.assign(states, {
126
+ [key]: state[0],
127
+ [`set${String(key).charAt(0).toUpperCase()}${String(key).slice(1)}`]: state[1]
128
+ });
157
129
  }
158
130
  return states;
159
131
  }
@@ -192,7 +164,18 @@ function FaasDataWrapper(props) {
192
164
  FaasDataWrapper.displayName = "FaasDataWrapper";
193
165
  FaasDataWrapper.whyDidYouRender = true;
194
166
  function withFaasData(Component2, faasProps) {
195
- return (props) => /* @__PURE__ */ jsxRuntime.jsx(FaasDataWrapper, __spreadProps(__spreadValues({}, faasProps), { children: /* @__PURE__ */ jsxRuntime.jsx(Component2, __spreadValues({}, props)) }));
167
+ return (props) => /* @__PURE__ */ jsxRuntime.jsx(FaasDataWrapper, { ...faasProps, children: /* @__PURE__ */ jsxRuntime.jsx(Component2, { ...props }) });
168
+ }
169
+
170
+ // src/faas.ts
171
+ async function faas(action, params, options) {
172
+ const client = getClient(options?.baseUrl);
173
+ if (client.onError)
174
+ return client.browserClient.action(action, params, options).catch(async (res) => {
175
+ await client.onError(action, params)(res);
176
+ return Promise.reject(res);
177
+ });
178
+ return client.browserClient.action(action, params, options);
196
179
  }
197
180
  function useFaas(action, defaultParams, options = {}) {
198
181
  const [loading, setLoading] = react.useState(true);
@@ -235,9 +218,9 @@ function useFaas(action, defaultParams, options = {}) {
235
218
  options.setData ? options.setData(r.data) : setData(r.data);
236
219
  setLoading(false);
237
220
  }).catch(async (e) => {
238
- if (typeof (e == null ? void 0 : e.message) === "string" && e.message.toLowerCase().indexOf("aborted") >= 0)
221
+ if (typeof e?.message === "string" && e.message.toLowerCase().indexOf("aborted") >= 0)
239
222
  return;
240
- if (!fails && typeof (e == null ? void 0 : e.message) === "string" && e.message.indexOf("Failed to fetch") >= 0) {
223
+ if (!fails && typeof e?.message === "string" && e.message.indexOf("Failed to fetch") >= 0) {
241
224
  console.warn(`FaasReactClient: ${e.message} retry...`);
242
225
  setFails(1);
243
226
  return send();
@@ -256,16 +239,14 @@ function useFaas(action, defaultParams, options = {}) {
256
239
  if (options.debounce) {
257
240
  const timeout = setTimeout(send, options.debounce);
258
241
  return () => {
259
- var _a;
260
242
  clearTimeout(timeout);
261
- (_a = controllerRef.current) == null ? void 0 : _a.abort();
243
+ controllerRef.current?.abort();
262
244
  setLoading(false);
263
245
  };
264
246
  }
265
247
  send();
266
248
  return () => {
267
- var _a;
268
- (_a = controllerRef.current) == null ? void 0 : _a.abort();
249
+ controllerRef.current?.abort();
269
250
  setLoading(false);
270
251
  };
271
252
  }, [action, options.params || params, reloadTimes, skip]);
@@ -294,17 +275,6 @@ function useFaas(action, defaultParams, options = {}) {
294
275
  };
295
276
  }
296
277
  useFaas.whyDidYouRender = true;
297
-
298
- // src/faas.ts
299
- async function faas(action, params, options) {
300
- const client = getClient(options == null ? void 0 : options.baseUrl);
301
- if (client.onError)
302
- return client.browserClient.action(action, params, options).catch(async (res) => {
303
- await client.onError(action, params)(res);
304
- return Promise.reject(res);
305
- });
306
- return client.browserClient.action(action, params, options);
307
- }
308
278
  var clients = {};
309
279
  function FaasReactClient({ baseUrl, options, onError } = {
310
280
  baseUrl: "/"
@@ -312,9 +282,9 @@ function FaasReactClient({ baseUrl, options, onError } = {
312
282
  const client = new browser.FaasBrowserClient(baseUrl, options);
313
283
  const reactClient = {
314
284
  id: client.id,
315
- faas: async (action, params, options2) => faas(action, params, __spreadValues({ baseUrl }, options2)),
316
- useFaas: (action, defaultParams, options2) => useFaas(action, defaultParams, __spreadValues({ baseUrl }, options2)),
317
- FaasDataWrapper: (props) => /* @__PURE__ */ jsxRuntime.jsx(FaasDataWrapper, __spreadValues({ baseUrl }, props)),
285
+ faas: async (action, params, options2) => faas(action, params, { baseUrl, ...options2 }),
286
+ useFaas: (action, defaultParams, options2) => useFaas(action, defaultParams, { baseUrl, ...options2 }),
287
+ FaasDataWrapper: (props) => /* @__PURE__ */ jsxRuntime.jsx(FaasDataWrapper, { baseUrl, ...props }),
318
288
  onError,
319
289
  browserClient: client
320
290
  };
@@ -330,6 +300,8 @@ function getClient(host) {
330
300
  return client;
331
301
  }
332
302
  var ErrorBoundary = class extends react.Component {
303
+ static displayName = "ErrorBoundary";
304
+ static whyDidYouRender = true;
333
305
  constructor(props) {
334
306
  super(props);
335
307
  this.state = {
@@ -344,9 +316,8 @@ var ErrorBoundary = class extends react.Component {
344
316
  });
345
317
  }
346
318
  render() {
347
- var _a;
348
319
  const errorMessage = (this.state.error || "").toString();
349
- const errorDescription = ((_a = this.state.info) == null ? void 0 : _a.componentStack) ? this.state.info.componentStack : null;
320
+ const errorDescription = this.state.info?.componentStack ? this.state.info.componentStack : null;
350
321
  if (this.state.error) {
351
322
  if (this.props.onError)
352
323
  this.props.onError(this.state.error, this.state.info);
@@ -365,35 +336,67 @@ var ErrorBoundary = class extends react.Component {
365
336
  return this.props.children;
366
337
  }
367
338
  };
368
- ErrorBoundary.displayName = "ErrorBoundary";
369
- ErrorBoundary.whyDidYouRender = true;
370
339
  var OptionalWrapper = ({ condition, Wrapper, wrapperProps, children }) => {
371
- return condition ? /* @__PURE__ */ jsxRuntime.jsx(Wrapper, __spreadProps(__spreadValues({}, wrapperProps), { children })) : /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children });
340
+ return condition ? /* @__PURE__ */ jsxRuntime.jsx(Wrapper, { ...wrapperProps, children }) : /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children });
372
341
  };
373
342
  OptionalWrapper.displayName = "OptionalWrapper";
374
343
  OptionalWrapper.whyDidYouRender = true;
375
344
 
376
345
  // src/Form/context.tsx
377
- var FormContext = createSplittingContext(["items", "onSubmit", "Elements", "submitting", "setSubmitting", "values", "setValues"]);
346
+ var FormContext = createSplittingContext([
347
+ "items",
348
+ "onSubmit",
349
+ "Elements",
350
+ "submitting",
351
+ "setSubmitting",
352
+ "values",
353
+ "setValues"
354
+ ]);
378
355
  var FormContextProvider = FormContext.Provider;
379
356
  var useFormContext = FormContext.use;
380
357
  function FormLabel(props) {
381
358
  const { Elements } = useFormContext();
382
- if (props.Label) return /* @__PURE__ */ jsxRuntime.jsx(props.Label, __spreadValues({}, props));
383
- return /* @__PURE__ */ jsxRuntime.jsx(Elements.Label, __spreadValues({}, props));
359
+ if (props.Label) return /* @__PURE__ */ jsxRuntime.jsx(props.Label, { ...props });
360
+ return /* @__PURE__ */ jsxRuntime.jsx(Elements.Label, { ...props });
384
361
  }
385
362
  FormLabel.displayName = "FormLabel";
386
363
  FormLabel.whyDidYouRender = true;
387
364
  function FormBody() {
388
365
  const { items } = useFormContext();
389
- return items.map((item) => /* @__PURE__ */ jsxRuntime.jsx(FormLabel, __spreadValues({}, item), item.name));
366
+ return items.map((item) => /* @__PURE__ */ jsxRuntime.jsx(FormLabel, { ...item }, item.name));
390
367
  }
391
368
  FormBody.displayName = "FormBody";
392
369
  FormBody.whyDidYouRender = true;
393
- var FormInputElement = react.forwardRef((_a, ref) => {
394
- var _b = _a, { onChange } = _b, props = __objRest(_b, ["onChange"]);
395
- return /* @__PURE__ */ jsxRuntime.jsx("input", __spreadProps(__spreadValues({}, props), { onChange: (e) => onChange(e.target.value), ref }));
396
- });
370
+ function FormFooter() {
371
+ const { submitting, setSubmitting, onSubmit, values, Elements } = useFormContext();
372
+ return /* @__PURE__ */ jsxRuntime.jsx(
373
+ Elements.Button,
374
+ {
375
+ disabled: submitting,
376
+ submit: () => {
377
+ setSubmitting(true);
378
+ onSubmit(values).finally(() => setSubmitting(false));
379
+ },
380
+ children: "Submit"
381
+ }
382
+ );
383
+ }
384
+ FormFooter.displayName = "FormFooter";
385
+ FormFooter.whyDidYouRender = true;
386
+ var FormButtonElement = react.forwardRef(({ disabled, children, submit, ...props }, ref) => /* @__PURE__ */ jsxRuntime.jsx(
387
+ "button",
388
+ {
389
+ type: "button",
390
+ disabled,
391
+ onClick: submit,
392
+ ...props,
393
+ ref,
394
+ children
395
+ }
396
+ ));
397
+ FormButtonElement.displayName = "FormButtonElement";
398
+ FormButtonElement.whyDidYouRender = true;
399
+ var FormInputElement = react.forwardRef(({ onChange, ...props }, ref) => /* @__PURE__ */ jsxRuntime.jsx("input", { ...props, onChange: (e) => onChange(e.target.value), ref }));
397
400
  FormInputElement.displayName = "FormInputElement";
398
401
  FormInputElement.whyDidYouRender = true;
399
402
  var FormLabelElement = ({
@@ -415,13 +418,14 @@ var FormLabelElement = ({
415
418
  }
416
419
  );
417
420
  return /* @__PURE__ */ jsxRuntime.jsxs("label", { children: [
418
- title != null ? title : name,
419
- (input == null ? void 0 : input.Input) ? /* @__PURE__ */ jsxRuntime.jsx(
421
+ title ?? name,
422
+ input?.Input ? /* @__PURE__ */ jsxRuntime.jsx(
420
423
  input.Input,
421
424
  {
422
425
  name,
423
426
  value: values[name],
424
- onChange: (v) => setValues((prev) => __spreadProps(__spreadValues({}, prev), {
427
+ onChange: (v) => setValues((prev) => ({
428
+ ...prev,
425
429
  [name]: v
426
430
  }))
427
431
  }
@@ -430,7 +434,8 @@ var FormLabelElement = ({
430
434
  {
431
435
  name,
432
436
  value: values[name],
433
- onChange: (v) => setValues((prev) => __spreadProps(__spreadValues({}, prev), {
437
+ onChange: (v) => setValues((prev) => ({
438
+ ...prev,
434
439
  [name]: v
435
440
  }))
436
441
  }
@@ -440,22 +445,6 @@ var FormLabelElement = ({
440
445
  };
441
446
  FormLabelElement.displayName = "FormLabelElement";
442
447
  FormLabelElement.whyDidYouRender = true;
443
- var FormButtonElement = react.forwardRef((_a, ref) => {
444
- var _b = _a, { disabled, children, onClick } = _b, props = __objRest(_b, ["disabled", "children", "onClick"]);
445
- return /* @__PURE__ */ jsxRuntime.jsx(
446
- "button",
447
- __spreadProps(__spreadValues({
448
- type: "button",
449
- disabled,
450
- onClick
451
- }, props), {
452
- ref,
453
- children
454
- })
455
- );
456
- });
457
- FormButtonElement.displayName = "FormButtonElement";
458
- FormButtonElement.whyDidYouRender = true;
459
448
 
460
449
  // src/Form/elements/index.ts
461
450
  var FormDefaultElements = {
@@ -463,31 +452,13 @@ var FormDefaultElements = {
463
452
  Input: FormInputElement,
464
453
  Button: FormButtonElement
465
454
  };
466
- function FormFooter() {
467
- const { submitting, setSubmitting, onSubmit, values, Elements } = useFormContext();
468
- return /* @__PURE__ */ jsxRuntime.jsx(
469
- Elements.Button,
470
- {
471
- disabled: submitting,
472
- onClick: () => {
473
- setSubmitting(true);
474
- onSubmit(values).finally(() => setSubmitting(false));
475
- },
476
- children: "Submit"
477
- }
478
- );
479
- }
480
- FormFooter.displayName = "FormFooter";
481
- FormFooter.whyDidYouRender = true;
482
455
  function mergeValues(items, defaultValues = {}) {
483
- var _a;
484
456
  const values = {};
485
457
  for (const item of items)
486
- values[item.name] = (_a = defaultValues[item.name]) != null ? _a : "";
458
+ values[item.name] = defaultValues[item.name] ?? "";
487
459
  return values;
488
460
  }
489
- function FormContainer(_a) {
490
- var _b = _a, { defaultValues, elements } = _b, props = __objRest(_b, ["defaultValues", "elements"]);
461
+ function FormContainer({ defaultValues, elements, ...props }) {
491
462
  const states = useSplittingState({
492
463
  values: mergeValues(props.items, defaultValues),
493
464
  submitting: false,
@@ -496,7 +467,11 @@ function FormContainer(_a) {
496
467
  return /* @__PURE__ */ jsxRuntime.jsxs(
497
468
  FormContextProvider,
498
469
  {
499
- value: __spreadValues(__spreadValues({}, states), props),
470
+ value: {
471
+ ...states,
472
+ ...props
473
+ },
474
+ memo: true,
500
475
  children: [
501
476
  /* @__PURE__ */ jsxRuntime.jsx(FormBody, {}),
502
477
  /* @__PURE__ */ jsxRuntime.jsx(FormFooter, {})
package/dist/index.mjs CHANGED
@@ -2,37 +2,7 @@ import { forwardRef, useRef, useMemo, useEffect, useCallback, createContext, use
2
2
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
3
3
  import { FaasBrowserClient } from '@faasjs/browser';
4
4
 
5
- var __defProp = Object.defineProperty;
6
- var __defProps = Object.defineProperties;
7
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
8
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
9
- var __hasOwnProp = Object.prototype.hasOwnProperty;
10
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
11
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
12
- var __spreadValues = (a, b) => {
13
- for (var prop in b || (b = {}))
14
- if (__hasOwnProp.call(b, prop))
15
- __defNormalProp(a, prop, b[prop]);
16
- if (__getOwnPropSymbols)
17
- for (var prop of __getOwnPropSymbols(b)) {
18
- if (__propIsEnum.call(b, prop))
19
- __defNormalProp(a, prop, b[prop]);
20
- }
21
- return a;
22
- };
23
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
24
- var __objRest = (source, exclude) => {
25
- var target = {};
26
- for (var prop in source)
27
- if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
28
- target[prop] = source[prop];
29
- if (source != null && __getOwnPropSymbols)
30
- for (var prop of __getOwnPropSymbols(source)) {
31
- if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
32
- target[prop] = source[prop];
33
- }
34
- return target;
35
- };
5
+ // src/constant.ts
36
6
  function useConstant(fn) {
37
7
  const ref = useRef();
38
8
  if (!ref.current) {
@@ -116,14 +86,13 @@ function createSplittingContext(defaultValue) {
116
86
  const contexts = {};
117
87
  for (const key of keys) contexts[key] = createContext(defaultValues[key]);
118
88
  function Provider(props) {
119
- var _a, _b;
120
89
  let children = props.memo ? useEqualMemo(
121
90
  () => props.children,
122
91
  props.memo === true ? [] : props.memo
123
92
  ) : props.children;
124
93
  for (const key of keys) {
125
94
  const Context = contexts[key];
126
- const value = (_b = (_a = props.value) == null ? void 0 : _a[key]) != null ? _b : defaultValues[key];
95
+ const value = props.value?.[key] ?? defaultValues[key];
127
96
  children = /* @__PURE__ */ jsx(Context.Provider, { value, children });
128
97
  }
129
98
  return children;
@@ -151,7 +120,10 @@ function useSplittingState(initialStates) {
151
120
  const states = {};
152
121
  for (const key of Object.keys(initialStates)) {
153
122
  const state = useState(initialStates[key]);
154
- Object.assign(states, { [key]: state[0], [`set${String(key).charAt(0).toUpperCase()}${String(key).slice(1)}`]: state[1] });
123
+ Object.assign(states, {
124
+ [key]: state[0],
125
+ [`set${String(key).charAt(0).toUpperCase()}${String(key).slice(1)}`]: state[1]
126
+ });
155
127
  }
156
128
  return states;
157
129
  }
@@ -190,7 +162,18 @@ function FaasDataWrapper(props) {
190
162
  FaasDataWrapper.displayName = "FaasDataWrapper";
191
163
  FaasDataWrapper.whyDidYouRender = true;
192
164
  function withFaasData(Component2, faasProps) {
193
- return (props) => /* @__PURE__ */ jsx(FaasDataWrapper, __spreadProps(__spreadValues({}, faasProps), { children: /* @__PURE__ */ jsx(Component2, __spreadValues({}, props)) }));
165
+ return (props) => /* @__PURE__ */ jsx(FaasDataWrapper, { ...faasProps, children: /* @__PURE__ */ jsx(Component2, { ...props }) });
166
+ }
167
+
168
+ // src/faas.ts
169
+ async function faas(action, params, options) {
170
+ const client = getClient(options?.baseUrl);
171
+ if (client.onError)
172
+ return client.browserClient.action(action, params, options).catch(async (res) => {
173
+ await client.onError(action, params)(res);
174
+ return Promise.reject(res);
175
+ });
176
+ return client.browserClient.action(action, params, options);
194
177
  }
195
178
  function useFaas(action, defaultParams, options = {}) {
196
179
  const [loading, setLoading] = useState(true);
@@ -233,9 +216,9 @@ function useFaas(action, defaultParams, options = {}) {
233
216
  options.setData ? options.setData(r.data) : setData(r.data);
234
217
  setLoading(false);
235
218
  }).catch(async (e) => {
236
- if (typeof (e == null ? void 0 : e.message) === "string" && e.message.toLowerCase().indexOf("aborted") >= 0)
219
+ if (typeof e?.message === "string" && e.message.toLowerCase().indexOf("aborted") >= 0)
237
220
  return;
238
- if (!fails && typeof (e == null ? void 0 : e.message) === "string" && e.message.indexOf("Failed to fetch") >= 0) {
221
+ if (!fails && typeof e?.message === "string" && e.message.indexOf("Failed to fetch") >= 0) {
239
222
  console.warn(`FaasReactClient: ${e.message} retry...`);
240
223
  setFails(1);
241
224
  return send();
@@ -254,16 +237,14 @@ function useFaas(action, defaultParams, options = {}) {
254
237
  if (options.debounce) {
255
238
  const timeout = setTimeout(send, options.debounce);
256
239
  return () => {
257
- var _a;
258
240
  clearTimeout(timeout);
259
- (_a = controllerRef.current) == null ? void 0 : _a.abort();
241
+ controllerRef.current?.abort();
260
242
  setLoading(false);
261
243
  };
262
244
  }
263
245
  send();
264
246
  return () => {
265
- var _a;
266
- (_a = controllerRef.current) == null ? void 0 : _a.abort();
247
+ controllerRef.current?.abort();
267
248
  setLoading(false);
268
249
  };
269
250
  }, [action, options.params || params, reloadTimes, skip]);
@@ -292,17 +273,6 @@ function useFaas(action, defaultParams, options = {}) {
292
273
  };
293
274
  }
294
275
  useFaas.whyDidYouRender = true;
295
-
296
- // src/faas.ts
297
- async function faas(action, params, options) {
298
- const client = getClient(options == null ? void 0 : options.baseUrl);
299
- if (client.onError)
300
- return client.browserClient.action(action, params, options).catch(async (res) => {
301
- await client.onError(action, params)(res);
302
- return Promise.reject(res);
303
- });
304
- return client.browserClient.action(action, params, options);
305
- }
306
276
  var clients = {};
307
277
  function FaasReactClient({ baseUrl, options, onError } = {
308
278
  baseUrl: "/"
@@ -310,9 +280,9 @@ function FaasReactClient({ baseUrl, options, onError } = {
310
280
  const client = new FaasBrowserClient(baseUrl, options);
311
281
  const reactClient = {
312
282
  id: client.id,
313
- faas: async (action, params, options2) => faas(action, params, __spreadValues({ baseUrl }, options2)),
314
- useFaas: (action, defaultParams, options2) => useFaas(action, defaultParams, __spreadValues({ baseUrl }, options2)),
315
- FaasDataWrapper: (props) => /* @__PURE__ */ jsx(FaasDataWrapper, __spreadValues({ baseUrl }, props)),
283
+ faas: async (action, params, options2) => faas(action, params, { baseUrl, ...options2 }),
284
+ useFaas: (action, defaultParams, options2) => useFaas(action, defaultParams, { baseUrl, ...options2 }),
285
+ FaasDataWrapper: (props) => /* @__PURE__ */ jsx(FaasDataWrapper, { baseUrl, ...props }),
316
286
  onError,
317
287
  browserClient: client
318
288
  };
@@ -328,6 +298,8 @@ function getClient(host) {
328
298
  return client;
329
299
  }
330
300
  var ErrorBoundary = class extends Component {
301
+ static displayName = "ErrorBoundary";
302
+ static whyDidYouRender = true;
331
303
  constructor(props) {
332
304
  super(props);
333
305
  this.state = {
@@ -342,9 +314,8 @@ var ErrorBoundary = class extends Component {
342
314
  });
343
315
  }
344
316
  render() {
345
- var _a;
346
317
  const errorMessage = (this.state.error || "").toString();
347
- const errorDescription = ((_a = this.state.info) == null ? void 0 : _a.componentStack) ? this.state.info.componentStack : null;
318
+ const errorDescription = this.state.info?.componentStack ? this.state.info.componentStack : null;
348
319
  if (this.state.error) {
349
320
  if (this.props.onError)
350
321
  this.props.onError(this.state.error, this.state.info);
@@ -363,35 +334,67 @@ var ErrorBoundary = class extends Component {
363
334
  return this.props.children;
364
335
  }
365
336
  };
366
- ErrorBoundary.displayName = "ErrorBoundary";
367
- ErrorBoundary.whyDidYouRender = true;
368
337
  var OptionalWrapper = ({ condition, Wrapper, wrapperProps, children }) => {
369
- return condition ? /* @__PURE__ */ jsx(Wrapper, __spreadProps(__spreadValues({}, wrapperProps), { children })) : /* @__PURE__ */ jsx(Fragment, { children });
338
+ return condition ? /* @__PURE__ */ jsx(Wrapper, { ...wrapperProps, children }) : /* @__PURE__ */ jsx(Fragment, { children });
370
339
  };
371
340
  OptionalWrapper.displayName = "OptionalWrapper";
372
341
  OptionalWrapper.whyDidYouRender = true;
373
342
 
374
343
  // src/Form/context.tsx
375
- var FormContext = createSplittingContext(["items", "onSubmit", "Elements", "submitting", "setSubmitting", "values", "setValues"]);
344
+ var FormContext = createSplittingContext([
345
+ "items",
346
+ "onSubmit",
347
+ "Elements",
348
+ "submitting",
349
+ "setSubmitting",
350
+ "values",
351
+ "setValues"
352
+ ]);
376
353
  var FormContextProvider = FormContext.Provider;
377
354
  var useFormContext = FormContext.use;
378
355
  function FormLabel(props) {
379
356
  const { Elements } = useFormContext();
380
- if (props.Label) return /* @__PURE__ */ jsx(props.Label, __spreadValues({}, props));
381
- return /* @__PURE__ */ jsx(Elements.Label, __spreadValues({}, props));
357
+ if (props.Label) return /* @__PURE__ */ jsx(props.Label, { ...props });
358
+ return /* @__PURE__ */ jsx(Elements.Label, { ...props });
382
359
  }
383
360
  FormLabel.displayName = "FormLabel";
384
361
  FormLabel.whyDidYouRender = true;
385
362
  function FormBody() {
386
363
  const { items } = useFormContext();
387
- return items.map((item) => /* @__PURE__ */ jsx(FormLabel, __spreadValues({}, item), item.name));
364
+ return items.map((item) => /* @__PURE__ */ jsx(FormLabel, { ...item }, item.name));
388
365
  }
389
366
  FormBody.displayName = "FormBody";
390
367
  FormBody.whyDidYouRender = true;
391
- var FormInputElement = forwardRef((_a, ref) => {
392
- var _b = _a, { onChange } = _b, props = __objRest(_b, ["onChange"]);
393
- return /* @__PURE__ */ jsx("input", __spreadProps(__spreadValues({}, props), { onChange: (e) => onChange(e.target.value), ref }));
394
- });
368
+ function FormFooter() {
369
+ const { submitting, setSubmitting, onSubmit, values, Elements } = useFormContext();
370
+ return /* @__PURE__ */ jsx(
371
+ Elements.Button,
372
+ {
373
+ disabled: submitting,
374
+ submit: () => {
375
+ setSubmitting(true);
376
+ onSubmit(values).finally(() => setSubmitting(false));
377
+ },
378
+ children: "Submit"
379
+ }
380
+ );
381
+ }
382
+ FormFooter.displayName = "FormFooter";
383
+ FormFooter.whyDidYouRender = true;
384
+ var FormButtonElement = forwardRef(({ disabled, children, submit, ...props }, ref) => /* @__PURE__ */ jsx(
385
+ "button",
386
+ {
387
+ type: "button",
388
+ disabled,
389
+ onClick: submit,
390
+ ...props,
391
+ ref,
392
+ children
393
+ }
394
+ ));
395
+ FormButtonElement.displayName = "FormButtonElement";
396
+ FormButtonElement.whyDidYouRender = true;
397
+ var FormInputElement = forwardRef(({ onChange, ...props }, ref) => /* @__PURE__ */ jsx("input", { ...props, onChange: (e) => onChange(e.target.value), ref }));
395
398
  FormInputElement.displayName = "FormInputElement";
396
399
  FormInputElement.whyDidYouRender = true;
397
400
  var FormLabelElement = ({
@@ -413,13 +416,14 @@ var FormLabelElement = ({
413
416
  }
414
417
  );
415
418
  return /* @__PURE__ */ jsxs("label", { children: [
416
- title != null ? title : name,
417
- (input == null ? void 0 : input.Input) ? /* @__PURE__ */ jsx(
419
+ title ?? name,
420
+ input?.Input ? /* @__PURE__ */ jsx(
418
421
  input.Input,
419
422
  {
420
423
  name,
421
424
  value: values[name],
422
- onChange: (v) => setValues((prev) => __spreadProps(__spreadValues({}, prev), {
425
+ onChange: (v) => setValues((prev) => ({
426
+ ...prev,
423
427
  [name]: v
424
428
  }))
425
429
  }
@@ -428,7 +432,8 @@ var FormLabelElement = ({
428
432
  {
429
433
  name,
430
434
  value: values[name],
431
- onChange: (v) => setValues((prev) => __spreadProps(__spreadValues({}, prev), {
435
+ onChange: (v) => setValues((prev) => ({
436
+ ...prev,
432
437
  [name]: v
433
438
  }))
434
439
  }
@@ -438,22 +443,6 @@ var FormLabelElement = ({
438
443
  };
439
444
  FormLabelElement.displayName = "FormLabelElement";
440
445
  FormLabelElement.whyDidYouRender = true;
441
- var FormButtonElement = forwardRef((_a, ref) => {
442
- var _b = _a, { disabled, children, onClick } = _b, props = __objRest(_b, ["disabled", "children", "onClick"]);
443
- return /* @__PURE__ */ jsx(
444
- "button",
445
- __spreadProps(__spreadValues({
446
- type: "button",
447
- disabled,
448
- onClick
449
- }, props), {
450
- ref,
451
- children
452
- })
453
- );
454
- });
455
- FormButtonElement.displayName = "FormButtonElement";
456
- FormButtonElement.whyDidYouRender = true;
457
446
 
458
447
  // src/Form/elements/index.ts
459
448
  var FormDefaultElements = {
@@ -461,31 +450,13 @@ var FormDefaultElements = {
461
450
  Input: FormInputElement,
462
451
  Button: FormButtonElement
463
452
  };
464
- function FormFooter() {
465
- const { submitting, setSubmitting, onSubmit, values, Elements } = useFormContext();
466
- return /* @__PURE__ */ jsx(
467
- Elements.Button,
468
- {
469
- disabled: submitting,
470
- onClick: () => {
471
- setSubmitting(true);
472
- onSubmit(values).finally(() => setSubmitting(false));
473
- },
474
- children: "Submit"
475
- }
476
- );
477
- }
478
- FormFooter.displayName = "FormFooter";
479
- FormFooter.whyDidYouRender = true;
480
453
  function mergeValues(items, defaultValues = {}) {
481
- var _a;
482
454
  const values = {};
483
455
  for (const item of items)
484
- values[item.name] = (_a = defaultValues[item.name]) != null ? _a : "";
456
+ values[item.name] = defaultValues[item.name] ?? "";
485
457
  return values;
486
458
  }
487
- function FormContainer(_a) {
488
- var _b = _a, { defaultValues, elements } = _b, props = __objRest(_b, ["defaultValues", "elements"]);
459
+ function FormContainer({ defaultValues, elements, ...props }) {
489
460
  const states = useSplittingState({
490
461
  values: mergeValues(props.items, defaultValues),
491
462
  submitting: false,
@@ -494,7 +465,11 @@ function FormContainer(_a) {
494
465
  return /* @__PURE__ */ jsxs(
495
466
  FormContextProvider,
496
467
  {
497
- value: __spreadValues(__spreadValues({}, states), props),
468
+ value: {
469
+ ...states,
470
+ ...props
471
+ },
472
+ memo: true,
498
473
  children: [
499
474
  /* @__PURE__ */ jsx(FormBody, {}),
500
475
  /* @__PURE__ */ jsx(FormFooter, {})
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/react",
3
- "version": "3.7.0-beta.2",
3
+ "version": "3.7.0-beta.4",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -34,10 +34,10 @@
34
34
  "dist"
35
35
  ],
36
36
  "peerDependencies": {
37
- "@faasjs/browser": "3.7.0-beta.2"
37
+ "@faasjs/browser": "3.7.0-beta.4"
38
38
  },
39
39
  "devDependencies": {
40
- "@faasjs/browser": "3.7.0-beta.2",
40
+ "@faasjs/browser": "3.7.0-beta.4",
41
41
  "@types/react": "*",
42
42
  "react": "*"
43
43
  },