@faasjs/react 3.7.0-beta.0 → 3.7.0-beta.10

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 CHANGED
@@ -34,7 +34,7 @@ React plugin for FaasJS.
34
34
  ## Install
35
35
 
36
36
  ```sh
37
- npm install @faasjs/react
37
+ npm install @faasjs/react react
38
38
  ```
39
39
 
40
40
  ## Functions
@@ -45,6 +45,8 @@ npm install @faasjs/react
45
45
  - [FaasDataWrapper](functions/FaasDataWrapper.md)
46
46
  - [FaasReactClient](functions/FaasReactClient.md)
47
47
  - [Form](functions/Form.md)
48
+ - [FormContextProvider](functions/FormContextProvider.md)
49
+ - [FormItem](functions/FormItem.md)
48
50
  - [getClient](functions/getClient.md)
49
51
  - [OptionalWrapper](functions/OptionalWrapper.md)
50
52
  - [useConstant](functions/useConstant.md)
@@ -53,7 +55,10 @@ npm install @faasjs/react
53
55
  - [useEqualMemo](functions/useEqualMemo.md)
54
56
  - [useEqualMemoize](functions/useEqualMemoize.md)
55
57
  - [useFaas](functions/useFaas.md)
58
+ - [useFormContext](functions/useFormContext.md)
59
+ - [usePrevious](functions/usePrevious.md)
56
60
  - [useSplittingState](functions/useSplittingState.md)
61
+ - [validValues](functions/validValues.md)
57
62
  - [withFaasData](functions/withFaasData.md)
58
63
 
59
64
  ## Classes
@@ -76,8 +81,27 @@ npm install @faasjs/react
76
81
  - [FaasParams](type-aliases/FaasParams.md)
77
82
  - [FaasReactClientInstance](type-aliases/FaasReactClientInstance.md)
78
83
  - [FaasReactClientOptions](type-aliases/FaasReactClientOptions.md)
84
+ - [FormButtonElementProps](type-aliases/FormButtonElementProps.md)
85
+ - [FormContextProps](type-aliases/FormContextProps.md)
86
+ - [FormDefaultRulesOptions](type-aliases/FormDefaultRulesOptions.md)
87
+ - [FormElementTypes](type-aliases/FormElementTypes.md)
88
+ - [FormInputElementProps](type-aliases/FormInputElementProps.md)
89
+ - [FormItemName](type-aliases/FormItemName.md)
90
+ - [FormItemProps](type-aliases/FormItemProps.md)
91
+ - [FormLabelElementProps](type-aliases/FormLabelElementProps.md)
92
+ - [FormLang](type-aliases/FormLang.md)
93
+ - [FormProps](type-aliases/FormProps.md)
94
+ - [FormRule](type-aliases/FormRule.md)
95
+ - [FormRules](type-aliases/FormRules.md)
96
+ - [InferFormRulesOptions](type-aliases/InferFormRulesOptions.md)
79
97
  - [OnError](type-aliases/OnError.md)
80
98
  - [OptionalWrapperProps](type-aliases/OptionalWrapperProps.md)
81
99
  - [Options](type-aliases/Options.md)
82
100
  - [ResponseHeaders](type-aliases/ResponseHeaders.md)
83
101
  - [useFaasOptions](type-aliases/useFaasOptions.md)
102
+
103
+ ## Variables
104
+
105
+ - [FormDefaultElements](variables/FormDefaultElements.md)
106
+ - [FormDefaultLang](variables/FormDefaultLang.md)
107
+ - [FormDefaultRules](variables/FormDefaultRules.md)
package/dist/index.d.mts CHANGED
@@ -2,6 +2,7 @@ import { FaasAction, FaasData, FaasParams } from '@faasjs/types';
2
2
  export { FaasAction, FaasData, FaasParams } from '@faasjs/types';
3
3
  import { Response, BaseUrl, ResponseError, Options, FaasBrowserClient } from '@faasjs/browser';
4
4
  export { Options, Response, ResponseError, ResponseHeaders } from '@faasjs/browser';
5
+ import * as react from 'react';
5
6
  import { ReactNode, Dispatch, SetStateAction, ReactElement, Component, ComponentType, ComponentProps, JSXElementConstructor } from 'react';
6
7
  import * as react_jsx_runtime from 'react/jsx-runtime';
7
8
 
@@ -56,6 +57,14 @@ declare function useEqualMemo<T>(callback: () => T, dependencies: any[]): T;
56
57
  * @returns The result of the `useCallback` hook with memoized dependencies.
57
58
  */
58
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;
59
68
 
60
69
  /**
61
70
  * Creates a splitting context with the given default value.
@@ -123,7 +132,7 @@ declare function createSplittingContext<T extends Record<string, any>>(defaultVa
123
132
  * ```
124
133
  */
125
134
  Provider<NewT extends T = T>(props: {
126
- value?: NewT;
135
+ value?: Partial<NewT>;
127
136
  children: ReactNode;
128
137
  /**
129
138
  * Whether to use memoization for the children.
@@ -134,6 +143,20 @@ declare function createSplittingContext<T extends Record<string, any>>(defaultVa
134
143
  * `any[]`: memoize the children with specific dependencies.
135
144
  */
136
145
  memo?: true | any[];
146
+ /**
147
+ * An object containing initial values that will be automatically converted into state variables using {@link useSplittingState} hook. Each property will create both a state value and its setter following the pattern: value/setValue.
148
+ *
149
+ * @example
150
+ * ```tsx
151
+ * <Provider
152
+ * initializeStates={{
153
+ * value: 0,
154
+ * }}
155
+ * >
156
+ * // Children will have access to: value, setValue
157
+ * </Provider>
158
+ */
159
+ initializeStates?: Partial<NewT>;
137
160
  }): ReactNode;
138
161
  /**
139
162
  * The hook to use the splitting context.
@@ -152,7 +175,7 @@ declare function createSplittingContext<T extends Record<string, any>>(defaultVa
152
175
  use: <NewT extends T = T>() => Readonly<NewT>;
153
176
  };
154
177
 
155
- type SetPrefix<S extends string | number | symbol> = S extends string ? (S extends `${infer First}${infer Rest}` ? `set${Capitalize<First>}${Rest}` : never) : never;
178
+ type SetPrefix<S extends string | number | symbol> = S extends string ? S extends `${infer First}${infer Rest}` ? `set${Capitalize<First>}${Rest}` : never : never;
156
179
  type StateSetters<T> = {
157
180
  [K in keyof T as SetPrefix<K>]: Dispatch<SetStateAction<T[K]>>;
158
181
  };
@@ -211,6 +234,7 @@ type FaasDataWrapperProps<PathOrData extends FaasAction> = {
211
234
  };
212
235
  declare function FaasDataWrapper<PathOrData extends FaasAction>(props: FaasDataWrapperProps<PathOrData>): JSX.Element;
213
236
  declare namespace FaasDataWrapper {
237
+ var displayName: string;
214
238
  var whyDidYouRender: boolean;
215
239
  }
216
240
  /**
@@ -262,6 +286,14 @@ type FaasReactClientOptions = {
262
286
  /** @default `/` */
263
287
  baseUrl?: BaseUrl;
264
288
  options?: Options;
289
+ /**
290
+ * @example
291
+ * ```ts
292
+ * onError: (action, params) => async (res) => {
293
+ * console.error(action, params, res)
294
+ * }
295
+ * ```
296
+ */
265
297
  onError?: OnError;
266
298
  };
267
299
  type FaasReactClientInstance = {
@@ -335,6 +367,7 @@ declare class ErrorBoundary extends Component<ErrorBoundaryProps, {
335
367
  componentStack?: string;
336
368
  };
337
369
  }> {
370
+ static displayName: string;
338
371
  static whyDidYouRender: boolean;
339
372
  constructor(props: ErrorBoundaryProps);
340
373
  componentDidCatch(error: Error | null, info: any): void;
@@ -371,64 +404,123 @@ declare const OptionalWrapper: React.FC<OptionalWrapperProps> & {
371
404
  whyDidYouRender: boolean;
372
405
  };
373
406
 
374
- type FormRules = {
375
- type?: 'string' | 'number';
376
- required?: boolean;
407
+ type FormButtonElementProps = {
408
+ children?: React.ReactNode;
409
+ disabled: boolean;
410
+ submit: () => Promise<void>;
377
411
  };
378
412
 
379
- type FormInputComponentProps = {
413
+ type FormInputElementProps = {
380
414
  name: string;
381
415
  value: any;
382
416
  onChange: (value: any) => void;
383
417
  };
384
- type FormInputComponent<Extra extends Record<string, any> = Record<string, any>> = ComponentType<FormInputComponentProps & Extra>;
385
- type InferFormInputProps<T extends FormInputComponent | JSXElementConstructor<any>> = T extends FormInputComponent ? Omit<ComponentProps<T>, 'name' | 'value' | 'onChange'> : Omit<ComponentProps<T>, 'name' | 'value'>;
386
- type FormInputProps<FormElements extends FormElementTypes = FormElementTypes> = {
387
- Input: FormInputComponent;
388
- } | {
389
- type?: 'input';
390
- props?: InferFormInputProps<FormElements['input']>;
391
- } | {
392
- type: 'select';
393
- props?: InferFormInputProps<FormElements['select']>;
394
- };
395
418
 
396
- type FormLabelProps<FormElements extends FormElementTypes = FormElementTypes> = {
419
+ type FormLabelElementProps = {
397
420
  name: string;
398
- rules?: FormRules;
399
- label?: {
400
- title?: ReactNode;
401
- description?: ReactNode;
402
- Label?: React.ComponentType<FormLabelProps>;
403
- };
404
- input?: FormInputProps<FormElements>;
421
+ title?: ReactNode;
422
+ description?: ReactNode;
423
+ error?: Error;
424
+ /** as Input element */
425
+ children: ReactNode;
405
426
  };
406
427
 
407
- type FormButtonProps = {
408
- children?: React.ReactNode;
409
- disabled?: boolean;
410
- onClick?: () => void;
428
+ type FormElementTypes = {
429
+ Label: ComponentType<FormLabelElementProps>;
430
+ Input: ComponentType<FormInputElementProps>;
431
+ Button: ComponentType<FormButtonElementProps>;
411
432
  };
433
+ declare const FormDefaultElements: FormElementTypes;
412
434
 
413
- type FormSelectOption = {
414
- value: string | number;
415
- label: string;
435
+ declare const FormDefaultLang: {
436
+ submit: string;
437
+ required: string;
438
+ string: string;
439
+ number: string;
416
440
  };
417
- type FormElementTypes = {
418
- label: ComponentType<FormLabelProps>;
419
- input: FormInputComponent;
420
- select: FormInputComponent<{
421
- options?: FormSelectOption[];
422
- }>;
423
- button: ComponentType<FormButtonProps>;
441
+ type FormLang = typeof FormDefaultLang;
442
+
443
+ type FormRule<Options = any> = (value: any, options?: Options, lang?: FormLang) => Promise<void>;
444
+ type InferRuleOption<T> = T extends (value: any, options: infer O, lang?: FormLang) => Promise<void> ? O : never;
445
+ type FormRules = Record<string, FormRule>;
446
+ type InferFormRulesOptions<T> = {
447
+ [K in keyof T]: InferRuleOption<T[K]>;
448
+ };
449
+ declare const FormDefaultRules: FormRules;
450
+ type FormDefaultRulesOptions = InferFormRulesOptions<typeof FormDefaultRules>;
451
+ declare function validValues(rules: FormRules, items: FormItemProps[], values: Record<string, any>, lang: FormLang): Promise<Record<string, Error>>;
452
+
453
+ type InferFormInputProps<T extends ComponentType<FormInputElementProps> | JSXElementConstructor<any>> = T extends ComponentType<FormInputElementProps> ? Omit<ComponentProps<T>, 'name' | 'value' | 'onChange'> : Omit<ComponentProps<T>, 'name' | 'value'>;
454
+ type FormInputProps<FormElements extends FormElementTypes = FormElementTypes> = {
455
+ Input?: ComponentType<FormInputElementProps>;
456
+ props?: InferFormInputProps<FormElements['Input']>;
424
457
  };
425
458
 
426
- type FormProps<Values extends Record<string, any> = Record<string, any>, FormElements extends FormElementTypes = FormElementTypes> = {
427
- items: FormLabelProps<FormElements>[];
459
+ type FormItemName = string;
460
+ type FormItemProps<FormElements extends FormElementTypes = FormElementTypes, FormRulesOptions extends Record<string, any> = FormDefaultRulesOptions> = {
461
+ name: FormItemName;
462
+ label?: Omit<FormLabelElementProps, 'name' | 'children'> & {
463
+ Label?: ComponentType<FormLabelElementProps>;
464
+ };
465
+ input?: FormInputProps<FormElements>;
466
+ rules?: FormRulesOptions;
467
+ };
468
+ declare function FormItem(props: FormItemProps): react_jsx_runtime.JSX.Element;
469
+ declare namespace FormItem {
470
+ var displayName: string;
471
+ var whyDidYouRender: boolean;
472
+ }
473
+
474
+ type FormProps<Values extends Record<string, any> = Record<string, any>, FormElements extends FormElementTypes = FormElementTypes, Rules extends FormRules = typeof FormDefaultRules> = {
475
+ items: FormItemProps<FormElements, InferFormRulesOptions<Rules>>[];
428
476
  onSubmit?: (values: Values) => Promise<void>;
429
- elements?: FormElements;
477
+ Elements?: Partial<FormElements>;
478
+ lang?: Partial<FormLang>;
430
479
  defaultValues?: Values;
480
+ rules?: typeof FormDefaultRules & Rules;
481
+ };
482
+ /**
483
+ * FormContainer component is a wrapper that provides context and state management for form elements.
484
+ * It initializes form states such as values, errors, submitting status, elements, language, and rules.
485
+ *
486
+ * @template Values - The type of form values, defaults to Record<string, any>.
487
+ * @template FormElements - The type of form elements, defaults to FormElementTypes.
488
+ * @template Rules - The type of form rules, defaults to FormDefaultRules.
489
+ *
490
+ * @param {FormProps<Values, FormElements, Rules>} props - The properties for the FormContainer component.
491
+ * @param {Values} props.defaultValues - The default values for the form fields.
492
+ * @param {FormElements} props.Elements - The form elements to be used in the form.
493
+ * @param {Rules} props.rules - The validation rules for the form fields.
494
+ * @param {FormLang} props.lang - The language settings for the form.
495
+ * @param {Partial<FormContextProps>} props - Additional properties for the form context.
496
+ *
497
+ * @returns {JSX.Element} The FormContainer component.
498
+ */
499
+ 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;
500
+ declare namespace FormContainer {
501
+ var displayName: string;
502
+ var whyDidYouRender: boolean;
503
+ }
504
+
505
+ type FormContextProps<Values extends Record<string, any> = Record<string, any>, Rules extends FormRules = typeof FormDefaultRules> = {
506
+ items: FormLabelElementProps[];
507
+ onSubmit: (values: Values) => Promise<void>;
508
+ Elements: FormElementTypes;
509
+ lang: FormLang;
510
+ rules: typeof FormDefaultRules & Rules;
511
+ submitting: boolean;
512
+ setSubmitting: Dispatch<SetStateAction<boolean>>;
513
+ values: Values;
514
+ setValues: Dispatch<SetStateAction<Values>>;
515
+ errors: Record<string, Error>;
516
+ setErrors: Dispatch<SetStateAction<Record<string, Error>>>;
431
517
  };
432
- declare function FormContainer<Values extends Record<string, any> = Record<string, any>, FormElements extends FormElementTypes = FormElementTypes>({ defaultValues, elements, ...props }: FormProps<Values, FormElements>): react_jsx_runtime.JSX.Element;
518
+ declare const FormContextProvider: <NewT extends FormContextProps<Record<string, any>, FormRules> = FormContextProps<Record<string, any>, FormRules>>(props: {
519
+ value?: Partial<NewT>;
520
+ children: react.ReactNode;
521
+ memo?: true | any[];
522
+ initializeStates?: Partial<NewT>;
523
+ }) => react.ReactNode;
524
+ declare const useFormContext: <NewT extends FormContextProps<Record<string, any>, FormRules> = FormContextProps<Record<string, any>, FormRules>>() => Readonly<NewT>;
433
525
 
434
- export { ErrorBoundary, type ErrorBoundaryProps, type ErrorChildrenProps, type FaasDataInjection, FaasDataWrapper, type FaasDataWrapperProps, FaasReactClient, type FaasReactClientInstance, type FaasReactClientOptions, FormContainer as Form, type OnError, OptionalWrapper, type OptionalWrapperProps, createSplittingContext, equal, faas, getClient, useConstant, useEqualCallback, useEqualEffect, useEqualMemo, useEqualMemoize, useFaas, type useFaasOptions, useSplittingState, withFaasData };
526
+ 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
@@ -2,6 +2,7 @@ import { FaasAction, FaasData, FaasParams } from '@faasjs/types';
2
2
  export { FaasAction, FaasData, FaasParams } from '@faasjs/types';
3
3
  import { Response, BaseUrl, ResponseError, Options, FaasBrowserClient } from '@faasjs/browser';
4
4
  export { Options, Response, ResponseError, ResponseHeaders } from '@faasjs/browser';
5
+ import * as react from 'react';
5
6
  import { ReactNode, Dispatch, SetStateAction, ReactElement, Component, ComponentType, ComponentProps, JSXElementConstructor } from 'react';
6
7
  import * as react_jsx_runtime from 'react/jsx-runtime';
7
8
 
@@ -56,6 +57,14 @@ declare function useEqualMemo<T>(callback: () => T, dependencies: any[]): T;
56
57
  * @returns The result of the `useCallback` hook with memoized dependencies.
57
58
  */
58
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;
59
68
 
60
69
  /**
61
70
  * Creates a splitting context with the given default value.
@@ -123,7 +132,7 @@ declare function createSplittingContext<T extends Record<string, any>>(defaultVa
123
132
  * ```
124
133
  */
125
134
  Provider<NewT extends T = T>(props: {
126
- value?: NewT;
135
+ value?: Partial<NewT>;
127
136
  children: ReactNode;
128
137
  /**
129
138
  * Whether to use memoization for the children.
@@ -134,6 +143,20 @@ declare function createSplittingContext<T extends Record<string, any>>(defaultVa
134
143
  * `any[]`: memoize the children with specific dependencies.
135
144
  */
136
145
  memo?: true | any[];
146
+ /**
147
+ * An object containing initial values that will be automatically converted into state variables using {@link useSplittingState} hook. Each property will create both a state value and its setter following the pattern: value/setValue.
148
+ *
149
+ * @example
150
+ * ```tsx
151
+ * <Provider
152
+ * initializeStates={{
153
+ * value: 0,
154
+ * }}
155
+ * >
156
+ * // Children will have access to: value, setValue
157
+ * </Provider>
158
+ */
159
+ initializeStates?: Partial<NewT>;
137
160
  }): ReactNode;
138
161
  /**
139
162
  * The hook to use the splitting context.
@@ -152,7 +175,7 @@ declare function createSplittingContext<T extends Record<string, any>>(defaultVa
152
175
  use: <NewT extends T = T>() => Readonly<NewT>;
153
176
  };
154
177
 
155
- type SetPrefix<S extends string | number | symbol> = S extends string ? (S extends `${infer First}${infer Rest}` ? `set${Capitalize<First>}${Rest}` : never) : never;
178
+ type SetPrefix<S extends string | number | symbol> = S extends string ? S extends `${infer First}${infer Rest}` ? `set${Capitalize<First>}${Rest}` : never : never;
156
179
  type StateSetters<T> = {
157
180
  [K in keyof T as SetPrefix<K>]: Dispatch<SetStateAction<T[K]>>;
158
181
  };
@@ -211,6 +234,7 @@ type FaasDataWrapperProps<PathOrData extends FaasAction> = {
211
234
  };
212
235
  declare function FaasDataWrapper<PathOrData extends FaasAction>(props: FaasDataWrapperProps<PathOrData>): JSX.Element;
213
236
  declare namespace FaasDataWrapper {
237
+ var displayName: string;
214
238
  var whyDidYouRender: boolean;
215
239
  }
216
240
  /**
@@ -262,6 +286,14 @@ type FaasReactClientOptions = {
262
286
  /** @default `/` */
263
287
  baseUrl?: BaseUrl;
264
288
  options?: Options;
289
+ /**
290
+ * @example
291
+ * ```ts
292
+ * onError: (action, params) => async (res) => {
293
+ * console.error(action, params, res)
294
+ * }
295
+ * ```
296
+ */
265
297
  onError?: OnError;
266
298
  };
267
299
  type FaasReactClientInstance = {
@@ -335,6 +367,7 @@ declare class ErrorBoundary extends Component<ErrorBoundaryProps, {
335
367
  componentStack?: string;
336
368
  };
337
369
  }> {
370
+ static displayName: string;
338
371
  static whyDidYouRender: boolean;
339
372
  constructor(props: ErrorBoundaryProps);
340
373
  componentDidCatch(error: Error | null, info: any): void;
@@ -371,64 +404,123 @@ declare const OptionalWrapper: React.FC<OptionalWrapperProps> & {
371
404
  whyDidYouRender: boolean;
372
405
  };
373
406
 
374
- type FormRules = {
375
- type?: 'string' | 'number';
376
- required?: boolean;
407
+ type FormButtonElementProps = {
408
+ children?: React.ReactNode;
409
+ disabled: boolean;
410
+ submit: () => Promise<void>;
377
411
  };
378
412
 
379
- type FormInputComponentProps = {
413
+ type FormInputElementProps = {
380
414
  name: string;
381
415
  value: any;
382
416
  onChange: (value: any) => void;
383
417
  };
384
- type FormInputComponent<Extra extends Record<string, any> = Record<string, any>> = ComponentType<FormInputComponentProps & Extra>;
385
- type InferFormInputProps<T extends FormInputComponent | JSXElementConstructor<any>> = T extends FormInputComponent ? Omit<ComponentProps<T>, 'name' | 'value' | 'onChange'> : Omit<ComponentProps<T>, 'name' | 'value'>;
386
- type FormInputProps<FormElements extends FormElementTypes = FormElementTypes> = {
387
- Input: FormInputComponent;
388
- } | {
389
- type?: 'input';
390
- props?: InferFormInputProps<FormElements['input']>;
391
- } | {
392
- type: 'select';
393
- props?: InferFormInputProps<FormElements['select']>;
394
- };
395
418
 
396
- type FormLabelProps<FormElements extends FormElementTypes = FormElementTypes> = {
419
+ type FormLabelElementProps = {
397
420
  name: string;
398
- rules?: FormRules;
399
- label?: {
400
- title?: ReactNode;
401
- description?: ReactNode;
402
- Label?: React.ComponentType<FormLabelProps>;
403
- };
404
- input?: FormInputProps<FormElements>;
421
+ title?: ReactNode;
422
+ description?: ReactNode;
423
+ error?: Error;
424
+ /** as Input element */
425
+ children: ReactNode;
405
426
  };
406
427
 
407
- type FormButtonProps = {
408
- children?: React.ReactNode;
409
- disabled?: boolean;
410
- onClick?: () => void;
428
+ type FormElementTypes = {
429
+ Label: ComponentType<FormLabelElementProps>;
430
+ Input: ComponentType<FormInputElementProps>;
431
+ Button: ComponentType<FormButtonElementProps>;
411
432
  };
433
+ declare const FormDefaultElements: FormElementTypes;
412
434
 
413
- type FormSelectOption = {
414
- value: string | number;
415
- label: string;
435
+ declare const FormDefaultLang: {
436
+ submit: string;
437
+ required: string;
438
+ string: string;
439
+ number: string;
416
440
  };
417
- type FormElementTypes = {
418
- label: ComponentType<FormLabelProps>;
419
- input: FormInputComponent;
420
- select: FormInputComponent<{
421
- options?: FormSelectOption[];
422
- }>;
423
- button: ComponentType<FormButtonProps>;
441
+ type FormLang = typeof FormDefaultLang;
442
+
443
+ type FormRule<Options = any> = (value: any, options?: Options, lang?: FormLang) => Promise<void>;
444
+ type InferRuleOption<T> = T extends (value: any, options: infer O, lang?: FormLang) => Promise<void> ? O : never;
445
+ type FormRules = Record<string, FormRule>;
446
+ type InferFormRulesOptions<T> = {
447
+ [K in keyof T]: InferRuleOption<T[K]>;
448
+ };
449
+ declare const FormDefaultRules: FormRules;
450
+ type FormDefaultRulesOptions = InferFormRulesOptions<typeof FormDefaultRules>;
451
+ declare function validValues(rules: FormRules, items: FormItemProps[], values: Record<string, any>, lang: FormLang): Promise<Record<string, Error>>;
452
+
453
+ type InferFormInputProps<T extends ComponentType<FormInputElementProps> | JSXElementConstructor<any>> = T extends ComponentType<FormInputElementProps> ? Omit<ComponentProps<T>, 'name' | 'value' | 'onChange'> : Omit<ComponentProps<T>, 'name' | 'value'>;
454
+ type FormInputProps<FormElements extends FormElementTypes = FormElementTypes> = {
455
+ Input?: ComponentType<FormInputElementProps>;
456
+ props?: InferFormInputProps<FormElements['Input']>;
424
457
  };
425
458
 
426
- type FormProps<Values extends Record<string, any> = Record<string, any>, FormElements extends FormElementTypes = FormElementTypes> = {
427
- items: FormLabelProps<FormElements>[];
459
+ type FormItemName = string;
460
+ type FormItemProps<FormElements extends FormElementTypes = FormElementTypes, FormRulesOptions extends Record<string, any> = FormDefaultRulesOptions> = {
461
+ name: FormItemName;
462
+ label?: Omit<FormLabelElementProps, 'name' | 'children'> & {
463
+ Label?: ComponentType<FormLabelElementProps>;
464
+ };
465
+ input?: FormInputProps<FormElements>;
466
+ rules?: FormRulesOptions;
467
+ };
468
+ declare function FormItem(props: FormItemProps): react_jsx_runtime.JSX.Element;
469
+ declare namespace FormItem {
470
+ var displayName: string;
471
+ var whyDidYouRender: boolean;
472
+ }
473
+
474
+ type FormProps<Values extends Record<string, any> = Record<string, any>, FormElements extends FormElementTypes = FormElementTypes, Rules extends FormRules = typeof FormDefaultRules> = {
475
+ items: FormItemProps<FormElements, InferFormRulesOptions<Rules>>[];
428
476
  onSubmit?: (values: Values) => Promise<void>;
429
- elements?: FormElements;
477
+ Elements?: Partial<FormElements>;
478
+ lang?: Partial<FormLang>;
430
479
  defaultValues?: Values;
480
+ rules?: typeof FormDefaultRules & Rules;
481
+ };
482
+ /**
483
+ * FormContainer component is a wrapper that provides context and state management for form elements.
484
+ * It initializes form states such as values, errors, submitting status, elements, language, and rules.
485
+ *
486
+ * @template Values - The type of form values, defaults to Record<string, any>.
487
+ * @template FormElements - The type of form elements, defaults to FormElementTypes.
488
+ * @template Rules - The type of form rules, defaults to FormDefaultRules.
489
+ *
490
+ * @param {FormProps<Values, FormElements, Rules>} props - The properties for the FormContainer component.
491
+ * @param {Values} props.defaultValues - The default values for the form fields.
492
+ * @param {FormElements} props.Elements - The form elements to be used in the form.
493
+ * @param {Rules} props.rules - The validation rules for the form fields.
494
+ * @param {FormLang} props.lang - The language settings for the form.
495
+ * @param {Partial<FormContextProps>} props - Additional properties for the form context.
496
+ *
497
+ * @returns {JSX.Element} The FormContainer component.
498
+ */
499
+ 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;
500
+ declare namespace FormContainer {
501
+ var displayName: string;
502
+ var whyDidYouRender: boolean;
503
+ }
504
+
505
+ type FormContextProps<Values extends Record<string, any> = Record<string, any>, Rules extends FormRules = typeof FormDefaultRules> = {
506
+ items: FormLabelElementProps[];
507
+ onSubmit: (values: Values) => Promise<void>;
508
+ Elements: FormElementTypes;
509
+ lang: FormLang;
510
+ rules: typeof FormDefaultRules & Rules;
511
+ submitting: boolean;
512
+ setSubmitting: Dispatch<SetStateAction<boolean>>;
513
+ values: Values;
514
+ setValues: Dispatch<SetStateAction<Values>>;
515
+ errors: Record<string, Error>;
516
+ setErrors: Dispatch<SetStateAction<Record<string, Error>>>;
431
517
  };
432
- declare function FormContainer<Values extends Record<string, any> = Record<string, any>, FormElements extends FormElementTypes = FormElementTypes>({ defaultValues, elements, ...props }: FormProps<Values, FormElements>): react_jsx_runtime.JSX.Element;
518
+ declare const FormContextProvider: <NewT extends FormContextProps<Record<string, any>, FormRules> = FormContextProps<Record<string, any>, FormRules>>(props: {
519
+ value?: Partial<NewT>;
520
+ children: react.ReactNode;
521
+ memo?: true | any[];
522
+ initializeStates?: Partial<NewT>;
523
+ }) => react.ReactNode;
524
+ declare const useFormContext: <NewT extends FormContextProps<Record<string, any>, FormRules> = FormContextProps<Record<string, any>, FormRules>>() => Readonly<NewT>;
433
525
 
434
- export { ErrorBoundary, type ErrorBoundaryProps, type ErrorChildrenProps, type FaasDataInjection, FaasDataWrapper, type FaasDataWrapperProps, FaasReactClient, type FaasReactClientInstance, type FaasReactClientOptions, FormContainer as Form, type OnError, OptionalWrapper, type OptionalWrapperProps, createSplittingContext, equal, faas, getClient, useConstant, useEqualCallback, useEqualEffect, useEqualMemo, useEqualMemoize, useFaas, type useFaasOptions, useSplittingState, withFaasData };
526
+ 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 };