@faasjs/react 3.7.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +80 -3
- package/dist/index.d.ts +80 -3
- package/dist/index.js +54 -19
- package/dist/index.mjs +54 -19
- package/package.json +3 -3
package/dist/index.d.mts
CHANGED
|
@@ -3,7 +3,7 @@ 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
5
|
import * as react from 'react';
|
|
6
|
-
import { ReactNode, Dispatch, SetStateAction, ReactElement, Component, ComponentType, ComponentProps, JSXElementConstructor } from 'react';
|
|
6
|
+
import { ReactNode, Dispatch, SetStateAction, JSX, ReactElement, Component, ComponentType, ComponentProps, JSXElementConstructor } from 'react';
|
|
7
7
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
8
8
|
|
|
9
9
|
/**
|
|
@@ -371,7 +371,7 @@ declare class ErrorBoundary extends Component<ErrorBoundaryProps, {
|
|
|
371
371
|
static whyDidYouRender: boolean;
|
|
372
372
|
constructor(props: ErrorBoundaryProps);
|
|
373
373
|
componentDidCatch(error: Error | null, info: any): void;
|
|
374
|
-
render(): string | number | boolean | Iterable<ReactNode> | react_jsx_runtime.JSX.Element;
|
|
374
|
+
render(): string | number | bigint | boolean | Iterable<ReactNode> | Promise<string | number | bigint | boolean | react.ReactPortal | ReactElement<unknown, string | react.JSXElementConstructor<any>> | Iterable<ReactNode>> | react_jsx_runtime.JSX.Element;
|
|
375
375
|
}
|
|
376
376
|
|
|
377
377
|
type OptionalWrapperProps<TWrapper extends ComponentType<{
|
|
@@ -404,18 +404,42 @@ declare const OptionalWrapper: React.FC<OptionalWrapperProps> & {
|
|
|
404
404
|
whyDidYouRender: boolean;
|
|
405
405
|
};
|
|
406
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
|
+
*/
|
|
407
414
|
type FormButtonElementProps = {
|
|
408
415
|
children?: React.ReactNode;
|
|
409
|
-
|
|
416
|
+
submitting?: boolean;
|
|
410
417
|
submit: () => Promise<void>;
|
|
411
418
|
};
|
|
412
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
|
+
*/
|
|
413
427
|
type FormInputElementProps = {
|
|
414
428
|
name: string;
|
|
415
429
|
value: any;
|
|
416
430
|
onChange: (value: any) => void;
|
|
417
431
|
};
|
|
418
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
|
+
*/
|
|
419
443
|
type FormLabelElementProps = {
|
|
420
444
|
name: string;
|
|
421
445
|
title?: ReactNode;
|
|
@@ -425,6 +449,14 @@ type FormLabelElementProps = {
|
|
|
425
449
|
children: ReactNode;
|
|
426
450
|
};
|
|
427
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
|
+
*/
|
|
428
460
|
type FormElementTypes = {
|
|
429
461
|
Label: ComponentType<FormLabelElementProps>;
|
|
430
462
|
Input: ComponentType<FormInputElementProps>;
|
|
@@ -440,12 +472,44 @@ declare const FormDefaultLang: {
|
|
|
440
472
|
};
|
|
441
473
|
type FormLang = typeof FormDefaultLang;
|
|
442
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
|
+
*/
|
|
443
494
|
type FormRule<Options = any> = (value: any, options?: Options, lang?: FormLang) => Promise<void>;
|
|
444
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
|
+
*/
|
|
445
503
|
type FormRules = Record<string, FormRule>;
|
|
446
504
|
type InferFormRulesOptions<T> = {
|
|
447
505
|
[K in keyof T]: InferRuleOption<T[K]>;
|
|
448
506
|
};
|
|
507
|
+
/**
|
|
508
|
+
* Default validation rules for a form.
|
|
509
|
+
*
|
|
510
|
+
* @constant
|
|
511
|
+
* @type {FormRules}
|
|
512
|
+
*/
|
|
449
513
|
declare const FormDefaultRules: FormRules;
|
|
450
514
|
type FormDefaultRulesOptions = InferFormRulesOptions<typeof FormDefaultRules>;
|
|
451
515
|
declare function validValues(rules: FormRules, items: FormItemProps[], values: Record<string, any>, lang: FormLang): Promise<Record<string, Error>>;
|
|
@@ -495,6 +559,19 @@ type FormProps<Values extends Record<string, any> = Record<string, any>, FormEle
|
|
|
495
559
|
* @param {Partial<FormContextProps>} props - Additional properties for the form context.
|
|
496
560
|
*
|
|
497
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
|
+
* ```
|
|
498
575
|
*/
|
|
499
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;
|
|
500
577
|
declare namespace FormContainer {
|
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ 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
5
|
import * as react from 'react';
|
|
6
|
-
import { ReactNode, Dispatch, SetStateAction, ReactElement, Component, ComponentType, ComponentProps, JSXElementConstructor } from 'react';
|
|
6
|
+
import { ReactNode, Dispatch, SetStateAction, JSX, ReactElement, Component, ComponentType, ComponentProps, JSXElementConstructor } from 'react';
|
|
7
7
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
8
8
|
|
|
9
9
|
/**
|
|
@@ -371,7 +371,7 @@ declare class ErrorBoundary extends Component<ErrorBoundaryProps, {
|
|
|
371
371
|
static whyDidYouRender: boolean;
|
|
372
372
|
constructor(props: ErrorBoundaryProps);
|
|
373
373
|
componentDidCatch(error: Error | null, info: any): void;
|
|
374
|
-
render(): string | number | boolean | Iterable<ReactNode> | react_jsx_runtime.JSX.Element;
|
|
374
|
+
render(): string | number | bigint | boolean | Iterable<ReactNode> | Promise<string | number | bigint | boolean | react.ReactPortal | ReactElement<unknown, string | react.JSXElementConstructor<any>> | Iterable<ReactNode>> | react_jsx_runtime.JSX.Element;
|
|
375
375
|
}
|
|
376
376
|
|
|
377
377
|
type OptionalWrapperProps<TWrapper extends ComponentType<{
|
|
@@ -404,18 +404,42 @@ declare const OptionalWrapper: React.FC<OptionalWrapperProps> & {
|
|
|
404
404
|
whyDidYouRender: boolean;
|
|
405
405
|
};
|
|
406
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
|
+
*/
|
|
407
414
|
type FormButtonElementProps = {
|
|
408
415
|
children?: React.ReactNode;
|
|
409
|
-
|
|
416
|
+
submitting?: boolean;
|
|
410
417
|
submit: () => Promise<void>;
|
|
411
418
|
};
|
|
412
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
|
+
*/
|
|
413
427
|
type FormInputElementProps = {
|
|
414
428
|
name: string;
|
|
415
429
|
value: any;
|
|
416
430
|
onChange: (value: any) => void;
|
|
417
431
|
};
|
|
418
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
|
+
*/
|
|
419
443
|
type FormLabelElementProps = {
|
|
420
444
|
name: string;
|
|
421
445
|
title?: ReactNode;
|
|
@@ -425,6 +449,14 @@ type FormLabelElementProps = {
|
|
|
425
449
|
children: ReactNode;
|
|
426
450
|
};
|
|
427
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
|
+
*/
|
|
428
460
|
type FormElementTypes = {
|
|
429
461
|
Label: ComponentType<FormLabelElementProps>;
|
|
430
462
|
Input: ComponentType<FormInputElementProps>;
|
|
@@ -440,12 +472,44 @@ declare const FormDefaultLang: {
|
|
|
440
472
|
};
|
|
441
473
|
type FormLang = typeof FormDefaultLang;
|
|
442
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
|
+
*/
|
|
443
494
|
type FormRule<Options = any> = (value: any, options?: Options, lang?: FormLang) => Promise<void>;
|
|
444
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
|
+
*/
|
|
445
503
|
type FormRules = Record<string, FormRule>;
|
|
446
504
|
type InferFormRulesOptions<T> = {
|
|
447
505
|
[K in keyof T]: InferRuleOption<T[K]>;
|
|
448
506
|
};
|
|
507
|
+
/**
|
|
508
|
+
* Default validation rules for a form.
|
|
509
|
+
*
|
|
510
|
+
* @constant
|
|
511
|
+
* @type {FormRules}
|
|
512
|
+
*/
|
|
449
513
|
declare const FormDefaultRules: FormRules;
|
|
450
514
|
type FormDefaultRulesOptions = InferFormRulesOptions<typeof FormDefaultRules>;
|
|
451
515
|
declare function validValues(rules: FormRules, items: FormItemProps[], values: Record<string, any>, lang: FormLang): Promise<Record<string, Error>>;
|
|
@@ -495,6 +559,19 @@ type FormProps<Values extends Record<string, any> = Record<string, any>, FormEle
|
|
|
495
559
|
* @param {Partial<FormContextProps>} props - Additional properties for the form context.
|
|
496
560
|
*
|
|
497
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
|
+
* ```
|
|
498
575
|
*/
|
|
499
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;
|
|
500
577
|
declare namespace FormContainer {
|
package/dist/index.js
CHANGED
|
@@ -6,7 +6,7 @@ var browser = require('@faasjs/browser');
|
|
|
6
6
|
|
|
7
7
|
// src/constant.ts
|
|
8
8
|
function useConstant(fn) {
|
|
9
|
-
const ref = react.useRef();
|
|
9
|
+
const ref = react.useRef(null);
|
|
10
10
|
if (!ref.current) {
|
|
11
11
|
ref.current = { v: fn() };
|
|
12
12
|
}
|
|
@@ -78,7 +78,7 @@ function useEqualCallback(callback, dependencies) {
|
|
|
78
78
|
);
|
|
79
79
|
}
|
|
80
80
|
function usePrevious(value) {
|
|
81
|
-
const ref = react.useRef();
|
|
81
|
+
const ref = react.useRef(void 0);
|
|
82
82
|
react.useEffect(() => {
|
|
83
83
|
ref.current = value;
|
|
84
84
|
});
|
|
@@ -193,7 +193,7 @@ function useFaas(action, defaultParams, options = {}) {
|
|
|
193
193
|
const [skip, setSkip] = react.useState(
|
|
194
194
|
typeof options.skip === "function" ? options.skip(defaultParams) : options.skip
|
|
195
195
|
);
|
|
196
|
-
const promiseRef = react.useRef();
|
|
196
|
+
const promiseRef = react.useRef(null);
|
|
197
197
|
const controllerRef = react.useRef(null);
|
|
198
198
|
useEqualEffect(() => {
|
|
199
199
|
setSkip(
|
|
@@ -364,21 +364,56 @@ var FormContext = createSplittingContext([
|
|
|
364
364
|
]);
|
|
365
365
|
var FormContextProvider = FormContext.Provider;
|
|
366
366
|
var useFormContext = FormContext.use;
|
|
367
|
-
function
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
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,
|
|
373
400
|
{
|
|
374
|
-
name
|
|
375
|
-
value
|
|
401
|
+
name,
|
|
402
|
+
value,
|
|
376
403
|
onChange: (v) => setValues((prev) => ({
|
|
377
404
|
...prev,
|
|
378
|
-
[
|
|
379
|
-
}))
|
|
405
|
+
[name]: processValue(v, rules)
|
|
406
|
+
})),
|
|
407
|
+
...rest.props
|
|
380
408
|
}
|
|
381
|
-
)
|
|
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 }) });
|
|
382
417
|
}
|
|
383
418
|
FormItem.displayName = "FormItem";
|
|
384
419
|
FormItem.whyDidYouRender = true;
|
|
@@ -443,7 +478,7 @@ function FormFooter() {
|
|
|
443
478
|
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
444
479
|
Elements.Button,
|
|
445
480
|
{
|
|
446
|
-
|
|
481
|
+
submitting,
|
|
447
482
|
submit: async () => {
|
|
448
483
|
setSubmitting(true);
|
|
449
484
|
const errors = await validValues(rules, items, values, lang);
|
|
@@ -460,11 +495,11 @@ function FormFooter() {
|
|
|
460
495
|
}
|
|
461
496
|
FormFooter.displayName = "FormFooter";
|
|
462
497
|
FormFooter.whyDidYouRender = true;
|
|
463
|
-
var FormButtonElement = react.forwardRef(({
|
|
498
|
+
var FormButtonElement = react.forwardRef(({ children, submit, ...props }, ref) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
464
499
|
"button",
|
|
465
500
|
{
|
|
466
501
|
type: "button",
|
|
467
|
-
disabled,
|
|
502
|
+
disabled: props.submitting,
|
|
468
503
|
onClick: submit,
|
|
469
504
|
...props,
|
|
470
505
|
ref,
|
|
@@ -472,10 +507,10 @@ var FormButtonElement = react.forwardRef(({ disabled, children, submit, ...props
|
|
|
472
507
|
}
|
|
473
508
|
));
|
|
474
509
|
FormButtonElement.displayName = "FormButtonElement";
|
|
475
|
-
FormButtonElement
|
|
510
|
+
Object.assign(FormButtonElement, { whyDidYouRender: true });
|
|
476
511
|
var FormInputElement = react.forwardRef(({ onChange, ...props }, ref) => /* @__PURE__ */ jsxRuntime.jsx("input", { ...props, onChange: (e) => onChange(e.target.value), ref }));
|
|
477
512
|
FormInputElement.displayName = "FormInputElement";
|
|
478
|
-
FormInputElement
|
|
513
|
+
Object.assign(FormInputElement, { whyDidYouRender: true });
|
|
479
514
|
var FormLabelElement = ({
|
|
480
515
|
name,
|
|
481
516
|
title,
|
package/dist/index.mjs
CHANGED
|
@@ -4,7 +4,7 @@ import { FaasBrowserClient } from '@faasjs/browser';
|
|
|
4
4
|
|
|
5
5
|
// src/constant.ts
|
|
6
6
|
function useConstant(fn) {
|
|
7
|
-
const ref = useRef();
|
|
7
|
+
const ref = useRef(null);
|
|
8
8
|
if (!ref.current) {
|
|
9
9
|
ref.current = { v: fn() };
|
|
10
10
|
}
|
|
@@ -76,7 +76,7 @@ function useEqualCallback(callback, dependencies) {
|
|
|
76
76
|
);
|
|
77
77
|
}
|
|
78
78
|
function usePrevious(value) {
|
|
79
|
-
const ref = useRef();
|
|
79
|
+
const ref = useRef(void 0);
|
|
80
80
|
useEffect(() => {
|
|
81
81
|
ref.current = value;
|
|
82
82
|
});
|
|
@@ -191,7 +191,7 @@ function useFaas(action, defaultParams, options = {}) {
|
|
|
191
191
|
const [skip, setSkip] = useState(
|
|
192
192
|
typeof options.skip === "function" ? options.skip(defaultParams) : options.skip
|
|
193
193
|
);
|
|
194
|
-
const promiseRef = useRef();
|
|
194
|
+
const promiseRef = useRef(null);
|
|
195
195
|
const controllerRef = useRef(null);
|
|
196
196
|
useEqualEffect(() => {
|
|
197
197
|
setSkip(
|
|
@@ -362,21 +362,56 @@ var FormContext = createSplittingContext([
|
|
|
362
362
|
]);
|
|
363
363
|
var FormContextProvider = FormContext.Provider;
|
|
364
364
|
var useFormContext = FormContext.use;
|
|
365
|
-
function
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
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,
|
|
371
398
|
{
|
|
372
|
-
name
|
|
373
|
-
value
|
|
399
|
+
name,
|
|
400
|
+
value,
|
|
374
401
|
onChange: (v) => setValues((prev) => ({
|
|
375
402
|
...prev,
|
|
376
|
-
[
|
|
377
|
-
}))
|
|
403
|
+
[name]: processValue(v, rules)
|
|
404
|
+
})),
|
|
405
|
+
...rest.props
|
|
378
406
|
}
|
|
379
|
-
)
|
|
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 }) });
|
|
380
415
|
}
|
|
381
416
|
FormItem.displayName = "FormItem";
|
|
382
417
|
FormItem.whyDidYouRender = true;
|
|
@@ -441,7 +476,7 @@ function FormFooter() {
|
|
|
441
476
|
return /* @__PURE__ */ jsx(
|
|
442
477
|
Elements.Button,
|
|
443
478
|
{
|
|
444
|
-
|
|
479
|
+
submitting,
|
|
445
480
|
submit: async () => {
|
|
446
481
|
setSubmitting(true);
|
|
447
482
|
const errors = await validValues(rules, items, values, lang);
|
|
@@ -458,11 +493,11 @@ function FormFooter() {
|
|
|
458
493
|
}
|
|
459
494
|
FormFooter.displayName = "FormFooter";
|
|
460
495
|
FormFooter.whyDidYouRender = true;
|
|
461
|
-
var FormButtonElement = forwardRef(({
|
|
496
|
+
var FormButtonElement = forwardRef(({ children, submit, ...props }, ref) => /* @__PURE__ */ jsx(
|
|
462
497
|
"button",
|
|
463
498
|
{
|
|
464
499
|
type: "button",
|
|
465
|
-
disabled,
|
|
500
|
+
disabled: props.submitting,
|
|
466
501
|
onClick: submit,
|
|
467
502
|
...props,
|
|
468
503
|
ref,
|
|
@@ -470,10 +505,10 @@ var FormButtonElement = forwardRef(({ disabled, children, submit, ...props }, re
|
|
|
470
505
|
}
|
|
471
506
|
));
|
|
472
507
|
FormButtonElement.displayName = "FormButtonElement";
|
|
473
|
-
FormButtonElement
|
|
508
|
+
Object.assign(FormButtonElement, { whyDidYouRender: true });
|
|
474
509
|
var FormInputElement = forwardRef(({ onChange, ...props }, ref) => /* @__PURE__ */ jsx("input", { ...props, onChange: (e) => onChange(e.target.value), ref }));
|
|
475
510
|
FormInputElement.displayName = "FormInputElement";
|
|
476
|
-
FormInputElement
|
|
511
|
+
Object.assign(FormInputElement, { whyDidYouRender: true });
|
|
477
512
|
var FormLabelElement = ({
|
|
478
513
|
name,
|
|
479
514
|
title,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/react",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
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": "
|
|
37
|
+
"@faasjs/browser": "4.0.0"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@faasjs/browser": "
|
|
40
|
+
"@faasjs/browser": "4.0.0",
|
|
41
41
|
"@types/react": "*",
|
|
42
42
|
"react": "*"
|
|
43
43
|
},
|