@faasjs/react 3.7.0-beta.9 → 3.7.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.
- package/README.md +3 -0
- package/dist/index.d.mts +86 -1
- package/dist/index.d.ts +86 -1
- package/dist/index.js +56 -14
- package/dist/index.mjs +55 -15
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -56,6 +56,7 @@ npm install @faasjs/react react
|
|
|
56
56
|
- [useEqualMemoize](functions/useEqualMemoize.md)
|
|
57
57
|
- [useFaas](functions/useFaas.md)
|
|
58
58
|
- [useFormContext](functions/useFormContext.md)
|
|
59
|
+
- [usePrevious](functions/usePrevious.md)
|
|
59
60
|
- [useSplittingState](functions/useSplittingState.md)
|
|
60
61
|
- [validValues](functions/validValues.md)
|
|
61
62
|
- [withFaasData](functions/withFaasData.md)
|
|
@@ -88,6 +89,7 @@ npm install @faasjs/react react
|
|
|
88
89
|
- [FormItemName](type-aliases/FormItemName.md)
|
|
89
90
|
- [FormItemProps](type-aliases/FormItemProps.md)
|
|
90
91
|
- [FormLabelElementProps](type-aliases/FormLabelElementProps.md)
|
|
92
|
+
- [FormLang](type-aliases/FormLang.md)
|
|
91
93
|
- [FormProps](type-aliases/FormProps.md)
|
|
92
94
|
- [FormRule](type-aliases/FormRule.md)
|
|
93
95
|
- [FormRules](type-aliases/FormRules.md)
|
|
@@ -101,4 +103,5 @@ npm install @faasjs/react react
|
|
|
101
103
|
## Variables
|
|
102
104
|
|
|
103
105
|
- [FormDefaultElements](variables/FormDefaultElements.md)
|
|
106
|
+
- [FormDefaultLang](variables/FormDefaultLang.md)
|
|
104
107
|
- [FormDefaultRules](variables/FormDefaultRules.md)
|
package/dist/index.d.mts
CHANGED
|
@@ -57,6 +57,14 @@ declare function useEqualMemo<T>(callback: () => T, dependencies: any[]): T;
|
|
|
57
57
|
* @returns The result of the `useCallback` hook with memoized dependencies.
|
|
58
58
|
*/
|
|
59
59
|
declare function useEqualCallback<T extends (...args: any[]) => any>(callback: T, dependencies: any[]): T;
|
|
60
|
+
/**
|
|
61
|
+
* Hook to store the previous value of a state or prop.
|
|
62
|
+
*
|
|
63
|
+
* @template T - The type of the value.
|
|
64
|
+
* @param {T} value - The current value to be stored.
|
|
65
|
+
* @returns {T | undefined} - The previous value, or undefined if there is no previous value.
|
|
66
|
+
*/
|
|
67
|
+
declare function usePrevious<T>(value: T): T | undefined;
|
|
60
68
|
|
|
61
69
|
/**
|
|
62
70
|
* Creates a splitting context with the given default value.
|
|
@@ -396,18 +404,42 @@ declare const OptionalWrapper: React.FC<OptionalWrapperProps> & {
|
|
|
396
404
|
whyDidYouRender: boolean;
|
|
397
405
|
};
|
|
398
406
|
|
|
407
|
+
/**
|
|
408
|
+
* Props for the FormButtonElement component.
|
|
409
|
+
*
|
|
410
|
+
* @property {React.ReactNode} [children] - The content to be displayed inside the button.
|
|
411
|
+
* @property {boolean} disabled - Indicates whether the button is disabled.
|
|
412
|
+
* @property {() => Promise<void>} submit - A function to be called when the button is clicked, which returns a promise.
|
|
413
|
+
*/
|
|
399
414
|
type FormButtonElementProps = {
|
|
400
415
|
children?: React.ReactNode;
|
|
401
416
|
disabled: boolean;
|
|
402
417
|
submit: () => Promise<void>;
|
|
403
418
|
};
|
|
404
419
|
|
|
420
|
+
/**
|
|
421
|
+
* Props for the Form Input Element component.
|
|
422
|
+
*
|
|
423
|
+
* @property {string} name - The name of the input element.
|
|
424
|
+
* @property {any} value - The current value of the input element.
|
|
425
|
+
* @property {(value: any) => void} onChange - Callback function to handle changes to the input value.
|
|
426
|
+
*/
|
|
405
427
|
type FormInputElementProps = {
|
|
406
428
|
name: string;
|
|
407
429
|
value: any;
|
|
408
430
|
onChange: (value: any) => void;
|
|
409
431
|
};
|
|
410
432
|
|
|
433
|
+
/**
|
|
434
|
+
* Props for the FormLabelElement component.
|
|
435
|
+
*
|
|
436
|
+
* @typedef {Object} FormLabelElementProps
|
|
437
|
+
* @property {string} name - The name of the form element.
|
|
438
|
+
* @property {ReactNode} [title] - Optional title for the form element.
|
|
439
|
+
* @property {ReactNode} [description] - Optional description for the form element.
|
|
440
|
+
* @property {Error} [error] - Optional error associated with the form element.
|
|
441
|
+
* @property {ReactNode} children - The child elements, typically an input element.
|
|
442
|
+
*/
|
|
411
443
|
type FormLabelElementProps = {
|
|
412
444
|
name: string;
|
|
413
445
|
title?: ReactNode;
|
|
@@ -417,6 +449,14 @@ type FormLabelElementProps = {
|
|
|
417
449
|
children: ReactNode;
|
|
418
450
|
};
|
|
419
451
|
|
|
452
|
+
/**
|
|
453
|
+
* Represents the types of form elements used in the form.
|
|
454
|
+
*
|
|
455
|
+
* @typedef {Object} FormElementTypes
|
|
456
|
+
* @property {ComponentType<FormLabelElementProps>} Label - The component type for the form label element.
|
|
457
|
+
* @property {ComponentType<FormInputElementProps>} Input - The component type for the form input element.
|
|
458
|
+
* @property {ComponentType<FormButtonElementProps>} Button - The component type for the form button element.
|
|
459
|
+
*/
|
|
420
460
|
type FormElementTypes = {
|
|
421
461
|
Label: ComponentType<FormLabelElementProps>;
|
|
422
462
|
Input: ComponentType<FormInputElementProps>;
|
|
@@ -432,12 +472,44 @@ declare const FormDefaultLang: {
|
|
|
432
472
|
};
|
|
433
473
|
type FormLang = typeof FormDefaultLang;
|
|
434
474
|
|
|
475
|
+
/**
|
|
476
|
+
* A type representing a form validation rule.
|
|
477
|
+
*
|
|
478
|
+
* @template Options - The type of the options that can be passed to the rule.
|
|
479
|
+
*
|
|
480
|
+
* @param value - The value to be validated.
|
|
481
|
+
* @param options - Optional. Additional options that can be used in the validation.
|
|
482
|
+
* @param lang - Optional. The language settings that can be used in the validation.
|
|
483
|
+
*
|
|
484
|
+
* @returns A promise that resolves if the validation is successful, or rejects with an error if the validation fails.
|
|
485
|
+
*
|
|
486
|
+
* @example
|
|
487
|
+
* ```ts
|
|
488
|
+
* async function required(value: any, options: boolean, lang?: FormLang) {
|
|
489
|
+
* if (value === null || value === undefined || value === '' || Number.isNaN(value))
|
|
490
|
+
* throw Error(lang?.required)
|
|
491
|
+
* }
|
|
492
|
+
* ```
|
|
493
|
+
*/
|
|
435
494
|
type FormRule<Options = any> = (value: any, options?: Options, lang?: FormLang) => Promise<void>;
|
|
436
495
|
type InferRuleOption<T> = T extends (value: any, options: infer O, lang?: FormLang) => Promise<void> ? O : never;
|
|
496
|
+
/**
|
|
497
|
+
* A type representing a set of form validation rules.
|
|
498
|
+
*
|
|
499
|
+
* @typedef {Record<string, FormRule>} FormRules
|
|
500
|
+
*
|
|
501
|
+
* Each key in the record represents the name of a form field, and the corresponding value is a `FormRule` object that defines the validation rules for that field.
|
|
502
|
+
*/
|
|
437
503
|
type FormRules = Record<string, FormRule>;
|
|
438
504
|
type InferFormRulesOptions<T> = {
|
|
439
505
|
[K in keyof T]: InferRuleOption<T[K]>;
|
|
440
506
|
};
|
|
507
|
+
/**
|
|
508
|
+
* Default validation rules for a form.
|
|
509
|
+
*
|
|
510
|
+
* @constant
|
|
511
|
+
* @type {FormRules}
|
|
512
|
+
*/
|
|
441
513
|
declare const FormDefaultRules: FormRules;
|
|
442
514
|
type FormDefaultRulesOptions = InferFormRulesOptions<typeof FormDefaultRules>;
|
|
443
515
|
declare function validValues(rules: FormRules, items: FormItemProps[], values: Record<string, any>, lang: FormLang): Promise<Record<string, Error>>;
|
|
@@ -487,6 +559,19 @@ type FormProps<Values extends Record<string, any> = Record<string, any>, FormEle
|
|
|
487
559
|
* @param {Partial<FormContextProps>} props - Additional properties for the form context.
|
|
488
560
|
*
|
|
489
561
|
* @returns {JSX.Element} The FormContainer component.
|
|
562
|
+
*
|
|
563
|
+
* @example
|
|
564
|
+
* ```tsx
|
|
565
|
+
* import { Form } from '@faasjs/react'
|
|
566
|
+
*
|
|
567
|
+
* function MyForm() {
|
|
568
|
+
* return <Form
|
|
569
|
+
* items={[
|
|
570
|
+
* { name: 'name' },
|
|
571
|
+
* ]}
|
|
572
|
+
* />
|
|
573
|
+
* }
|
|
574
|
+
* ```
|
|
490
575
|
*/
|
|
491
576
|
declare function FormContainer<Values extends Record<string, any> = Record<string, any>, FormElements extends FormElementTypes = FormElementTypes, Rules extends FormRules = typeof FormDefaultRules>({ defaultValues, Elements, rules, lang, ...props }: FormProps<Values, FormElements, Rules>): react_jsx_runtime.JSX.Element;
|
|
492
577
|
declare namespace FormContainer {
|
|
@@ -515,4 +600,4 @@ declare const FormContextProvider: <NewT extends FormContextProps<Record<string,
|
|
|
515
600
|
}) => react.ReactNode;
|
|
516
601
|
declare const useFormContext: <NewT extends FormContextProps<Record<string, any>, FormRules> = FormContextProps<Record<string, any>, FormRules>>() => Readonly<NewT>;
|
|
517
602
|
|
|
518
|
-
export { ErrorBoundary, type ErrorBoundaryProps, type ErrorChildrenProps, type FaasDataInjection, FaasDataWrapper, type FaasDataWrapperProps, FaasReactClient, type FaasReactClientInstance, type FaasReactClientOptions, FormContainer as Form, type FormButtonElementProps, type FormContextProps, FormContextProvider, FormDefaultElements, FormDefaultRules, type FormDefaultRulesOptions, type FormElementTypes, type FormInputElementProps, FormItem, type FormItemName, type FormItemProps, type FormLabelElementProps, type FormProps, type FormRule, type FormRules, type InferFormRulesOptions, type OnError, OptionalWrapper, type OptionalWrapperProps, createSplittingContext, equal, faas, getClient, useConstant, useEqualCallback, useEqualEffect, useEqualMemo, useEqualMemoize, useFaas, type useFaasOptions, useFormContext, useSplittingState, validValues, withFaasData };
|
|
603
|
+
export { ErrorBoundary, type ErrorBoundaryProps, type ErrorChildrenProps, type FaasDataInjection, FaasDataWrapper, type FaasDataWrapperProps, FaasReactClient, type FaasReactClientInstance, type FaasReactClientOptions, FormContainer as Form, type FormButtonElementProps, type FormContextProps, FormContextProvider, FormDefaultElements, FormDefaultLang, FormDefaultRules, type FormDefaultRulesOptions, type FormElementTypes, type FormInputElementProps, FormItem, type FormItemName, type FormItemProps, type FormLabelElementProps, type FormLang, type FormProps, type FormRule, type FormRules, type InferFormRulesOptions, type OnError, OptionalWrapper, type OptionalWrapperProps, createSplittingContext, equal, faas, getClient, useConstant, useEqualCallback, useEqualEffect, useEqualMemo, useEqualMemoize, useFaas, type useFaasOptions, useFormContext, usePrevious, useSplittingState, validValues, withFaasData };
|
package/dist/index.d.ts
CHANGED
|
@@ -57,6 +57,14 @@ declare function useEqualMemo<T>(callback: () => T, dependencies: any[]): T;
|
|
|
57
57
|
* @returns The result of the `useCallback` hook with memoized dependencies.
|
|
58
58
|
*/
|
|
59
59
|
declare function useEqualCallback<T extends (...args: any[]) => any>(callback: T, dependencies: any[]): T;
|
|
60
|
+
/**
|
|
61
|
+
* Hook to store the previous value of a state or prop.
|
|
62
|
+
*
|
|
63
|
+
* @template T - The type of the value.
|
|
64
|
+
* @param {T} value - The current value to be stored.
|
|
65
|
+
* @returns {T | undefined} - The previous value, or undefined if there is no previous value.
|
|
66
|
+
*/
|
|
67
|
+
declare function usePrevious<T>(value: T): T | undefined;
|
|
60
68
|
|
|
61
69
|
/**
|
|
62
70
|
* Creates a splitting context with the given default value.
|
|
@@ -396,18 +404,42 @@ declare const OptionalWrapper: React.FC<OptionalWrapperProps> & {
|
|
|
396
404
|
whyDidYouRender: boolean;
|
|
397
405
|
};
|
|
398
406
|
|
|
407
|
+
/**
|
|
408
|
+
* Props for the FormButtonElement component.
|
|
409
|
+
*
|
|
410
|
+
* @property {React.ReactNode} [children] - The content to be displayed inside the button.
|
|
411
|
+
* @property {boolean} disabled - Indicates whether the button is disabled.
|
|
412
|
+
* @property {() => Promise<void>} submit - A function to be called when the button is clicked, which returns a promise.
|
|
413
|
+
*/
|
|
399
414
|
type FormButtonElementProps = {
|
|
400
415
|
children?: React.ReactNode;
|
|
401
416
|
disabled: boolean;
|
|
402
417
|
submit: () => Promise<void>;
|
|
403
418
|
};
|
|
404
419
|
|
|
420
|
+
/**
|
|
421
|
+
* Props for the Form Input Element component.
|
|
422
|
+
*
|
|
423
|
+
* @property {string} name - The name of the input element.
|
|
424
|
+
* @property {any} value - The current value of the input element.
|
|
425
|
+
* @property {(value: any) => void} onChange - Callback function to handle changes to the input value.
|
|
426
|
+
*/
|
|
405
427
|
type FormInputElementProps = {
|
|
406
428
|
name: string;
|
|
407
429
|
value: any;
|
|
408
430
|
onChange: (value: any) => void;
|
|
409
431
|
};
|
|
410
432
|
|
|
433
|
+
/**
|
|
434
|
+
* Props for the FormLabelElement component.
|
|
435
|
+
*
|
|
436
|
+
* @typedef {Object} FormLabelElementProps
|
|
437
|
+
* @property {string} name - The name of the form element.
|
|
438
|
+
* @property {ReactNode} [title] - Optional title for the form element.
|
|
439
|
+
* @property {ReactNode} [description] - Optional description for the form element.
|
|
440
|
+
* @property {Error} [error] - Optional error associated with the form element.
|
|
441
|
+
* @property {ReactNode} children - The child elements, typically an input element.
|
|
442
|
+
*/
|
|
411
443
|
type FormLabelElementProps = {
|
|
412
444
|
name: string;
|
|
413
445
|
title?: ReactNode;
|
|
@@ -417,6 +449,14 @@ type FormLabelElementProps = {
|
|
|
417
449
|
children: ReactNode;
|
|
418
450
|
};
|
|
419
451
|
|
|
452
|
+
/**
|
|
453
|
+
* Represents the types of form elements used in the form.
|
|
454
|
+
*
|
|
455
|
+
* @typedef {Object} FormElementTypes
|
|
456
|
+
* @property {ComponentType<FormLabelElementProps>} Label - The component type for the form label element.
|
|
457
|
+
* @property {ComponentType<FormInputElementProps>} Input - The component type for the form input element.
|
|
458
|
+
* @property {ComponentType<FormButtonElementProps>} Button - The component type for the form button element.
|
|
459
|
+
*/
|
|
420
460
|
type FormElementTypes = {
|
|
421
461
|
Label: ComponentType<FormLabelElementProps>;
|
|
422
462
|
Input: ComponentType<FormInputElementProps>;
|
|
@@ -432,12 +472,44 @@ declare const FormDefaultLang: {
|
|
|
432
472
|
};
|
|
433
473
|
type FormLang = typeof FormDefaultLang;
|
|
434
474
|
|
|
475
|
+
/**
|
|
476
|
+
* A type representing a form validation rule.
|
|
477
|
+
*
|
|
478
|
+
* @template Options - The type of the options that can be passed to the rule.
|
|
479
|
+
*
|
|
480
|
+
* @param value - The value to be validated.
|
|
481
|
+
* @param options - Optional. Additional options that can be used in the validation.
|
|
482
|
+
* @param lang - Optional. The language settings that can be used in the validation.
|
|
483
|
+
*
|
|
484
|
+
* @returns A promise that resolves if the validation is successful, or rejects with an error if the validation fails.
|
|
485
|
+
*
|
|
486
|
+
* @example
|
|
487
|
+
* ```ts
|
|
488
|
+
* async function required(value: any, options: boolean, lang?: FormLang) {
|
|
489
|
+
* if (value === null || value === undefined || value === '' || Number.isNaN(value))
|
|
490
|
+
* throw Error(lang?.required)
|
|
491
|
+
* }
|
|
492
|
+
* ```
|
|
493
|
+
*/
|
|
435
494
|
type FormRule<Options = any> = (value: any, options?: Options, lang?: FormLang) => Promise<void>;
|
|
436
495
|
type InferRuleOption<T> = T extends (value: any, options: infer O, lang?: FormLang) => Promise<void> ? O : never;
|
|
496
|
+
/**
|
|
497
|
+
* A type representing a set of form validation rules.
|
|
498
|
+
*
|
|
499
|
+
* @typedef {Record<string, FormRule>} FormRules
|
|
500
|
+
*
|
|
501
|
+
* Each key in the record represents the name of a form field, and the corresponding value is a `FormRule` object that defines the validation rules for that field.
|
|
502
|
+
*/
|
|
437
503
|
type FormRules = Record<string, FormRule>;
|
|
438
504
|
type InferFormRulesOptions<T> = {
|
|
439
505
|
[K in keyof T]: InferRuleOption<T[K]>;
|
|
440
506
|
};
|
|
507
|
+
/**
|
|
508
|
+
* Default validation rules for a form.
|
|
509
|
+
*
|
|
510
|
+
* @constant
|
|
511
|
+
* @type {FormRules}
|
|
512
|
+
*/
|
|
441
513
|
declare const FormDefaultRules: FormRules;
|
|
442
514
|
type FormDefaultRulesOptions = InferFormRulesOptions<typeof FormDefaultRules>;
|
|
443
515
|
declare function validValues(rules: FormRules, items: FormItemProps[], values: Record<string, any>, lang: FormLang): Promise<Record<string, Error>>;
|
|
@@ -487,6 +559,19 @@ type FormProps<Values extends Record<string, any> = Record<string, any>, FormEle
|
|
|
487
559
|
* @param {Partial<FormContextProps>} props - Additional properties for the form context.
|
|
488
560
|
*
|
|
489
561
|
* @returns {JSX.Element} The FormContainer component.
|
|
562
|
+
*
|
|
563
|
+
* @example
|
|
564
|
+
* ```tsx
|
|
565
|
+
* import { Form } from '@faasjs/react'
|
|
566
|
+
*
|
|
567
|
+
* function MyForm() {
|
|
568
|
+
* return <Form
|
|
569
|
+
* items={[
|
|
570
|
+
* { name: 'name' },
|
|
571
|
+
* ]}
|
|
572
|
+
* />
|
|
573
|
+
* }
|
|
574
|
+
* ```
|
|
490
575
|
*/
|
|
491
576
|
declare function FormContainer<Values extends Record<string, any> = Record<string, any>, FormElements extends FormElementTypes = FormElementTypes, Rules extends FormRules = typeof FormDefaultRules>({ defaultValues, Elements, rules, lang, ...props }: FormProps<Values, FormElements, Rules>): react_jsx_runtime.JSX.Element;
|
|
492
577
|
declare namespace FormContainer {
|
|
@@ -515,4 +600,4 @@ declare const FormContextProvider: <NewT extends FormContextProps<Record<string,
|
|
|
515
600
|
}) => react.ReactNode;
|
|
516
601
|
declare const useFormContext: <NewT extends FormContextProps<Record<string, any>, FormRules> = FormContextProps<Record<string, any>, FormRules>>() => Readonly<NewT>;
|
|
517
602
|
|
|
518
|
-
export { ErrorBoundary, type ErrorBoundaryProps, type ErrorChildrenProps, type FaasDataInjection, FaasDataWrapper, type FaasDataWrapperProps, FaasReactClient, type FaasReactClientInstance, type FaasReactClientOptions, FormContainer as Form, type FormButtonElementProps, type FormContextProps, FormContextProvider, FormDefaultElements, FormDefaultRules, type FormDefaultRulesOptions, type FormElementTypes, type FormInputElementProps, FormItem, type FormItemName, type FormItemProps, type FormLabelElementProps, type FormProps, type FormRule, type FormRules, type InferFormRulesOptions, type OnError, OptionalWrapper, type OptionalWrapperProps, createSplittingContext, equal, faas, getClient, useConstant, useEqualCallback, useEqualEffect, useEqualMemo, useEqualMemoize, useFaas, type useFaasOptions, useFormContext, useSplittingState, validValues, withFaasData };
|
|
603
|
+
export { ErrorBoundary, type ErrorBoundaryProps, type ErrorChildrenProps, type FaasDataInjection, FaasDataWrapper, type FaasDataWrapperProps, FaasReactClient, type FaasReactClientInstance, type FaasReactClientOptions, FormContainer as Form, type FormButtonElementProps, type FormContextProps, FormContextProvider, FormDefaultElements, FormDefaultLang, FormDefaultRules, type FormDefaultRulesOptions, type FormElementTypes, type FormInputElementProps, FormItem, type FormItemName, type FormItemProps, type FormLabelElementProps, type FormLang, type FormProps, type FormRule, type FormRules, type InferFormRulesOptions, type OnError, OptionalWrapper, type OptionalWrapperProps, createSplittingContext, equal, faas, getClient, useConstant, useEqualCallback, useEqualEffect, useEqualMemo, useEqualMemoize, useFaas, type useFaasOptions, useFormContext, usePrevious, useSplittingState, validValues, withFaasData };
|
package/dist/index.js
CHANGED
|
@@ -48,10 +48,8 @@ function equal(a, b) {
|
|
|
48
48
|
case Promise:
|
|
49
49
|
return a === b;
|
|
50
50
|
case Object: {
|
|
51
|
-
const
|
|
52
|
-
for (const key of keys) {
|
|
51
|
+
for (const key of /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)]))
|
|
53
52
|
if (!equal(a[key], b[key])) return false;
|
|
54
|
-
}
|
|
55
53
|
return true;
|
|
56
54
|
}
|
|
57
55
|
default:
|
|
@@ -79,6 +77,13 @@ function useEqualCallback(callback, dependencies) {
|
|
|
79
77
|
useEqualMemoize(dependencies)
|
|
80
78
|
);
|
|
81
79
|
}
|
|
80
|
+
function usePrevious(value) {
|
|
81
|
+
const ref = react.useRef();
|
|
82
|
+
react.useEffect(() => {
|
|
83
|
+
ref.current = value;
|
|
84
|
+
});
|
|
85
|
+
return ref.current;
|
|
86
|
+
}
|
|
82
87
|
function useSplittingState(initialStates) {
|
|
83
88
|
const states = {};
|
|
84
89
|
for (const key of Object.keys(initialStates)) {
|
|
@@ -359,21 +364,56 @@ var FormContext = createSplittingContext([
|
|
|
359
364
|
]);
|
|
360
365
|
var FormContextProvider = FormContext.Provider;
|
|
361
366
|
var useFormContext = FormContext.use;
|
|
362
|
-
function
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
367
|
+
function processValue(input, rules) {
|
|
368
|
+
switch (rules?.type) {
|
|
369
|
+
case "number":
|
|
370
|
+
return Number(input);
|
|
371
|
+
case "string":
|
|
372
|
+
return String(input);
|
|
373
|
+
default:
|
|
374
|
+
return input;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
function FormInput({
|
|
378
|
+
name,
|
|
379
|
+
rules,
|
|
380
|
+
...rest
|
|
381
|
+
}) {
|
|
382
|
+
const { Elements, values, setValues } = useFormContext();
|
|
383
|
+
const value = values?.[name];
|
|
384
|
+
if (rest.Input) {
|
|
385
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
386
|
+
rest.Input,
|
|
387
|
+
{
|
|
388
|
+
name,
|
|
389
|
+
value,
|
|
390
|
+
onChange: (v) => setValues((prev) => ({
|
|
391
|
+
...prev,
|
|
392
|
+
[name]: processValue(v, rules)
|
|
393
|
+
})),
|
|
394
|
+
...rest.props
|
|
395
|
+
}
|
|
396
|
+
);
|
|
397
|
+
}
|
|
398
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
399
|
+
Elements.Input,
|
|
368
400
|
{
|
|
369
|
-
name
|
|
370
|
-
value
|
|
401
|
+
name,
|
|
402
|
+
value,
|
|
371
403
|
onChange: (v) => setValues((prev) => ({
|
|
372
404
|
...prev,
|
|
373
|
-
[
|
|
374
|
-
}))
|
|
405
|
+
[name]: processValue(v, rules)
|
|
406
|
+
})),
|
|
407
|
+
...rest.props
|
|
375
408
|
}
|
|
376
|
-
)
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
FormInput.displayName = "FormInput";
|
|
412
|
+
FormInput.whyDidYouRender = true;
|
|
413
|
+
function FormItem(props) {
|
|
414
|
+
const { Elements, errors } = useFormContext();
|
|
415
|
+
const Label = props.label?.Label ?? Elements.Label;
|
|
416
|
+
return /* @__PURE__ */ jsxRuntime.jsx(Label, { name: props.name, ...props.label, error: errors[props.name], children: /* @__PURE__ */ jsxRuntime.jsx(FormInput, { name: props.name, rules: props.rules, ...props.input }) });
|
|
377
417
|
}
|
|
378
418
|
FormItem.displayName = "FormItem";
|
|
379
419
|
FormItem.whyDidYouRender = true;
|
|
@@ -547,6 +587,7 @@ exports.FaasReactClient = FaasReactClient;
|
|
|
547
587
|
exports.Form = FormContainer;
|
|
548
588
|
exports.FormContextProvider = FormContextProvider;
|
|
549
589
|
exports.FormDefaultElements = FormDefaultElements;
|
|
590
|
+
exports.FormDefaultLang = FormDefaultLang;
|
|
550
591
|
exports.FormDefaultRules = FormDefaultRules;
|
|
551
592
|
exports.FormItem = FormItem;
|
|
552
593
|
exports.OptionalWrapper = OptionalWrapper;
|
|
@@ -561,6 +602,7 @@ exports.useEqualMemo = useEqualMemo;
|
|
|
561
602
|
exports.useEqualMemoize = useEqualMemoize;
|
|
562
603
|
exports.useFaas = useFaas;
|
|
563
604
|
exports.useFormContext = useFormContext;
|
|
605
|
+
exports.usePrevious = usePrevious;
|
|
564
606
|
exports.useSplittingState = useSplittingState;
|
|
565
607
|
exports.validValues = validValues;
|
|
566
608
|
exports.withFaasData = withFaasData;
|
package/dist/index.mjs
CHANGED
|
@@ -46,10 +46,8 @@ function equal(a, b) {
|
|
|
46
46
|
case Promise:
|
|
47
47
|
return a === b;
|
|
48
48
|
case Object: {
|
|
49
|
-
const
|
|
50
|
-
for (const key of keys) {
|
|
49
|
+
for (const key of /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)]))
|
|
51
50
|
if (!equal(a[key], b[key])) return false;
|
|
52
|
-
}
|
|
53
51
|
return true;
|
|
54
52
|
}
|
|
55
53
|
default:
|
|
@@ -77,6 +75,13 @@ function useEqualCallback(callback, dependencies) {
|
|
|
77
75
|
useEqualMemoize(dependencies)
|
|
78
76
|
);
|
|
79
77
|
}
|
|
78
|
+
function usePrevious(value) {
|
|
79
|
+
const ref = useRef();
|
|
80
|
+
useEffect(() => {
|
|
81
|
+
ref.current = value;
|
|
82
|
+
});
|
|
83
|
+
return ref.current;
|
|
84
|
+
}
|
|
80
85
|
function useSplittingState(initialStates) {
|
|
81
86
|
const states = {};
|
|
82
87
|
for (const key of Object.keys(initialStates)) {
|
|
@@ -357,21 +362,56 @@ var FormContext = createSplittingContext([
|
|
|
357
362
|
]);
|
|
358
363
|
var FormContextProvider = FormContext.Provider;
|
|
359
364
|
var useFormContext = FormContext.use;
|
|
360
|
-
function
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
365
|
+
function processValue(input, rules) {
|
|
366
|
+
switch (rules?.type) {
|
|
367
|
+
case "number":
|
|
368
|
+
return Number(input);
|
|
369
|
+
case "string":
|
|
370
|
+
return String(input);
|
|
371
|
+
default:
|
|
372
|
+
return input;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
function FormInput({
|
|
376
|
+
name,
|
|
377
|
+
rules,
|
|
378
|
+
...rest
|
|
379
|
+
}) {
|
|
380
|
+
const { Elements, values, setValues } = useFormContext();
|
|
381
|
+
const value = values?.[name];
|
|
382
|
+
if (rest.Input) {
|
|
383
|
+
return /* @__PURE__ */ jsx(
|
|
384
|
+
rest.Input,
|
|
385
|
+
{
|
|
386
|
+
name,
|
|
387
|
+
value,
|
|
388
|
+
onChange: (v) => setValues((prev) => ({
|
|
389
|
+
...prev,
|
|
390
|
+
[name]: processValue(v, rules)
|
|
391
|
+
})),
|
|
392
|
+
...rest.props
|
|
393
|
+
}
|
|
394
|
+
);
|
|
395
|
+
}
|
|
396
|
+
return /* @__PURE__ */ jsx(
|
|
397
|
+
Elements.Input,
|
|
366
398
|
{
|
|
367
|
-
name
|
|
368
|
-
value
|
|
399
|
+
name,
|
|
400
|
+
value,
|
|
369
401
|
onChange: (v) => setValues((prev) => ({
|
|
370
402
|
...prev,
|
|
371
|
-
[
|
|
372
|
-
}))
|
|
403
|
+
[name]: processValue(v, rules)
|
|
404
|
+
})),
|
|
405
|
+
...rest.props
|
|
373
406
|
}
|
|
374
|
-
)
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
FormInput.displayName = "FormInput";
|
|
410
|
+
FormInput.whyDidYouRender = true;
|
|
411
|
+
function FormItem(props) {
|
|
412
|
+
const { Elements, errors } = useFormContext();
|
|
413
|
+
const Label = props.label?.Label ?? Elements.Label;
|
|
414
|
+
return /* @__PURE__ */ jsx(Label, { name: props.name, ...props.label, error: errors[props.name], children: /* @__PURE__ */ jsx(FormInput, { name: props.name, rules: props.rules, ...props.input }) });
|
|
375
415
|
}
|
|
376
416
|
FormItem.displayName = "FormItem";
|
|
377
417
|
FormItem.whyDidYouRender = true;
|
|
@@ -539,4 +579,4 @@ function FormContainer({
|
|
|
539
579
|
FormContainer.displayName = "FormContainer";
|
|
540
580
|
FormContainer.whyDidYouRender = true;
|
|
541
581
|
|
|
542
|
-
export { ErrorBoundary, FaasDataWrapper, FaasReactClient, FormContainer as Form, FormContextProvider, FormDefaultElements, FormDefaultRules, FormItem, OptionalWrapper, createSplittingContext, equal, faas, getClient, useConstant, useEqualCallback, useEqualEffect, useEqualMemo, useEqualMemoize, useFaas, useFormContext, useSplittingState, validValues, withFaasData };
|
|
582
|
+
export { ErrorBoundary, FaasDataWrapper, FaasReactClient, FormContainer as Form, FormContextProvider, FormDefaultElements, FormDefaultLang, FormDefaultRules, FormItem, OptionalWrapper, createSplittingContext, equal, faas, getClient, useConstant, useEqualCallback, useEqualEffect, useEqualMemo, useEqualMemoize, useFaas, useFormContext, usePrevious, useSplittingState, validValues, withFaasData };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/react",
|
|
3
|
-
"version": "3.7.
|
|
3
|
+
"version": "3.7.1",
|
|
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.
|
|
37
|
+
"@faasjs/browser": "3.7.1"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@faasjs/browser": "3.7.
|
|
40
|
+
"@faasjs/browser": "3.7.1",
|
|
41
41
|
"@types/react": "*",
|
|
42
42
|
"react": "*"
|
|
43
43
|
},
|