@faasjs/react 3.7.0-beta.0 → 3.7.0-beta.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 +9 -0
- package/dist/index.d.mts +33 -32
- package/dist/index.d.ts +33 -32
- package/dist/index.js +54 -85
- package/dist/index.mjs +53 -87
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -45,6 +45,7 @@ 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)
|
|
48
49
|
- [getClient](functions/getClient.md)
|
|
49
50
|
- [OptionalWrapper](functions/OptionalWrapper.md)
|
|
50
51
|
- [useConstant](functions/useConstant.md)
|
|
@@ -53,6 +54,7 @@ npm install @faasjs/react
|
|
|
53
54
|
- [useEqualMemo](functions/useEqualMemo.md)
|
|
54
55
|
- [useEqualMemoize](functions/useEqualMemoize.md)
|
|
55
56
|
- [useFaas](functions/useFaas.md)
|
|
57
|
+
- [useFormContext](functions/useFormContext.md)
|
|
56
58
|
- [useSplittingState](functions/useSplittingState.md)
|
|
57
59
|
- [withFaasData](functions/withFaasData.md)
|
|
58
60
|
|
|
@@ -76,8 +78,15 @@ npm install @faasjs/react
|
|
|
76
78
|
- [FaasParams](type-aliases/FaasParams.md)
|
|
77
79
|
- [FaasReactClientInstance](type-aliases/FaasReactClientInstance.md)
|
|
78
80
|
- [FaasReactClientOptions](type-aliases/FaasReactClientOptions.md)
|
|
81
|
+
- [FormContextProps](type-aliases/FormContextProps.md)
|
|
82
|
+
- [FormElementTypes](type-aliases/FormElementTypes.md)
|
|
83
|
+
- [FormProps](type-aliases/FormProps.md)
|
|
79
84
|
- [OnError](type-aliases/OnError.md)
|
|
80
85
|
- [OptionalWrapperProps](type-aliases/OptionalWrapperProps.md)
|
|
81
86
|
- [Options](type-aliases/Options.md)
|
|
82
87
|
- [ResponseHeaders](type-aliases/ResponseHeaders.md)
|
|
83
88
|
- [useFaasOptions](type-aliases/useFaasOptions.md)
|
|
89
|
+
|
|
90
|
+
## Variables
|
|
91
|
+
|
|
92
|
+
- [FormDefaultElements](variables/FormDefaultElements.md)
|
package/dist/index.d.mts
CHANGED
|
@@ -2,7 +2,8 @@ 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
|
|
5
|
+
import * as react from 'react';
|
|
6
|
+
import { ReactNode, Dispatch, SetStateAction, ReactElement, Component, ComponentType, ComponentProps } from 'react';
|
|
6
7
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
7
8
|
|
|
8
9
|
/**
|
|
@@ -376,59 +377,59 @@ type FormRules = {
|
|
|
376
377
|
required?: boolean;
|
|
377
378
|
};
|
|
378
379
|
|
|
379
|
-
type
|
|
380
|
+
type FormInputElementProps = {
|
|
380
381
|
name: string;
|
|
381
382
|
value: any;
|
|
382
383
|
onChange: (value: any) => void;
|
|
383
384
|
};
|
|
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
385
|
|
|
396
|
-
type
|
|
386
|
+
type FormLabelElementProps = {
|
|
397
387
|
name: string;
|
|
398
388
|
rules?: FormRules;
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
389
|
+
title?: ReactNode;
|
|
390
|
+
description?: ReactNode;
|
|
391
|
+
Label?: React.ComponentType<FormLabelElementProps>;
|
|
392
|
+
input?: {
|
|
393
|
+
Input?: ComponentType<FormInputElementProps>;
|
|
394
|
+
props?: FormInputElementProps;
|
|
403
395
|
};
|
|
404
|
-
input?: FormInputProps<FormElements>;
|
|
405
396
|
};
|
|
406
397
|
|
|
407
|
-
type
|
|
398
|
+
type FormButtonElementProps = {
|
|
408
399
|
children?: React.ReactNode;
|
|
409
400
|
disabled?: boolean;
|
|
410
401
|
onClick?: () => void;
|
|
411
402
|
};
|
|
412
403
|
|
|
413
|
-
type FormSelectOption = {
|
|
414
|
-
value: string | number;
|
|
415
|
-
label: string;
|
|
416
|
-
};
|
|
417
404
|
type FormElementTypes = {
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
options?: FormSelectOption[];
|
|
422
|
-
}>;
|
|
423
|
-
button: ComponentType<FormButtonProps>;
|
|
405
|
+
Label: ComponentType<FormLabelElementProps>;
|
|
406
|
+
Input: ComponentType<FormInputElementProps>;
|
|
407
|
+
Button: ComponentType<FormButtonElementProps>;
|
|
424
408
|
};
|
|
409
|
+
declare const FormDefaultElements: FormElementTypes;
|
|
425
410
|
|
|
426
411
|
type FormProps<Values extends Record<string, any> = Record<string, any>, FormElements extends FormElementTypes = FormElementTypes> = {
|
|
427
|
-
items:
|
|
412
|
+
items: FormLabelElementProps[];
|
|
428
413
|
onSubmit?: (values: Values) => Promise<void>;
|
|
429
414
|
elements?: FormElements;
|
|
430
415
|
defaultValues?: Values;
|
|
431
416
|
};
|
|
432
417
|
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;
|
|
433
418
|
|
|
434
|
-
|
|
419
|
+
type FormContextProps<Values extends Record<string, any> = Record<string, any>> = {
|
|
420
|
+
items: FormLabelElementProps[];
|
|
421
|
+
onSubmit: (values: Values) => Promise<void>;
|
|
422
|
+
Elements: FormElementTypes;
|
|
423
|
+
submitting: boolean;
|
|
424
|
+
setSubmitting: React.Dispatch<React.SetStateAction<boolean>>;
|
|
425
|
+
values: Values;
|
|
426
|
+
setValues: React.Dispatch<React.SetStateAction<Values>>;
|
|
427
|
+
};
|
|
428
|
+
declare const FormContextProvider: <NewT extends FormContextProps<Record<string, any>> = FormContextProps<Record<string, any>>>(props: {
|
|
429
|
+
value?: NewT;
|
|
430
|
+
children: react.ReactNode;
|
|
431
|
+
memo?: true | any[];
|
|
432
|
+
}) => react.ReactNode;
|
|
433
|
+
declare const useFormContext: <NewT extends FormContextProps<Record<string, any>> = FormContextProps<Record<string, any>>>() => Readonly<NewT>;
|
|
434
|
+
|
|
435
|
+
export { ErrorBoundary, type ErrorBoundaryProps, type ErrorChildrenProps, type FaasDataInjection, FaasDataWrapper, type FaasDataWrapperProps, FaasReactClient, type FaasReactClientInstance, type FaasReactClientOptions, FormContainer as Form, type FormContextProps, FormContextProvider, FormDefaultElements, type FormElementTypes, type FormProps, type OnError, OptionalWrapper, type OptionalWrapperProps, createSplittingContext, equal, faas, getClient, useConstant, useEqualCallback, useEqualEffect, useEqualMemo, useEqualMemoize, useFaas, type useFaasOptions, useFormContext, useSplittingState, withFaasData };
|
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,8 @@ 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
|
|
5
|
+
import * as react from 'react';
|
|
6
|
+
import { ReactNode, Dispatch, SetStateAction, ReactElement, Component, ComponentType, ComponentProps } from 'react';
|
|
6
7
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
7
8
|
|
|
8
9
|
/**
|
|
@@ -376,59 +377,59 @@ type FormRules = {
|
|
|
376
377
|
required?: boolean;
|
|
377
378
|
};
|
|
378
379
|
|
|
379
|
-
type
|
|
380
|
+
type FormInputElementProps = {
|
|
380
381
|
name: string;
|
|
381
382
|
value: any;
|
|
382
383
|
onChange: (value: any) => void;
|
|
383
384
|
};
|
|
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
385
|
|
|
396
|
-
type
|
|
386
|
+
type FormLabelElementProps = {
|
|
397
387
|
name: string;
|
|
398
388
|
rules?: FormRules;
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
389
|
+
title?: ReactNode;
|
|
390
|
+
description?: ReactNode;
|
|
391
|
+
Label?: React.ComponentType<FormLabelElementProps>;
|
|
392
|
+
input?: {
|
|
393
|
+
Input?: ComponentType<FormInputElementProps>;
|
|
394
|
+
props?: FormInputElementProps;
|
|
403
395
|
};
|
|
404
|
-
input?: FormInputProps<FormElements>;
|
|
405
396
|
};
|
|
406
397
|
|
|
407
|
-
type
|
|
398
|
+
type FormButtonElementProps = {
|
|
408
399
|
children?: React.ReactNode;
|
|
409
400
|
disabled?: boolean;
|
|
410
401
|
onClick?: () => void;
|
|
411
402
|
};
|
|
412
403
|
|
|
413
|
-
type FormSelectOption = {
|
|
414
|
-
value: string | number;
|
|
415
|
-
label: string;
|
|
416
|
-
};
|
|
417
404
|
type FormElementTypes = {
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
options?: FormSelectOption[];
|
|
422
|
-
}>;
|
|
423
|
-
button: ComponentType<FormButtonProps>;
|
|
405
|
+
Label: ComponentType<FormLabelElementProps>;
|
|
406
|
+
Input: ComponentType<FormInputElementProps>;
|
|
407
|
+
Button: ComponentType<FormButtonElementProps>;
|
|
424
408
|
};
|
|
409
|
+
declare const FormDefaultElements: FormElementTypes;
|
|
425
410
|
|
|
426
411
|
type FormProps<Values extends Record<string, any> = Record<string, any>, FormElements extends FormElementTypes = FormElementTypes> = {
|
|
427
|
-
items:
|
|
412
|
+
items: FormLabelElementProps[];
|
|
428
413
|
onSubmit?: (values: Values) => Promise<void>;
|
|
429
414
|
elements?: FormElements;
|
|
430
415
|
defaultValues?: Values;
|
|
431
416
|
};
|
|
432
417
|
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;
|
|
433
418
|
|
|
434
|
-
|
|
419
|
+
type FormContextProps<Values extends Record<string, any> = Record<string, any>> = {
|
|
420
|
+
items: FormLabelElementProps[];
|
|
421
|
+
onSubmit: (values: Values) => Promise<void>;
|
|
422
|
+
Elements: FormElementTypes;
|
|
423
|
+
submitting: boolean;
|
|
424
|
+
setSubmitting: React.Dispatch<React.SetStateAction<boolean>>;
|
|
425
|
+
values: Values;
|
|
426
|
+
setValues: React.Dispatch<React.SetStateAction<Values>>;
|
|
427
|
+
};
|
|
428
|
+
declare const FormContextProvider: <NewT extends FormContextProps<Record<string, any>> = FormContextProps<Record<string, any>>>(props: {
|
|
429
|
+
value?: NewT;
|
|
430
|
+
children: react.ReactNode;
|
|
431
|
+
memo?: true | any[];
|
|
432
|
+
}) => react.ReactNode;
|
|
433
|
+
declare const useFormContext: <NewT extends FormContextProps<Record<string, any>> = FormContextProps<Record<string, any>>>() => Readonly<NewT>;
|
|
434
|
+
|
|
435
|
+
export { ErrorBoundary, type ErrorBoundaryProps, type ErrorChildrenProps, type FaasDataInjection, FaasDataWrapper, type FaasDataWrapperProps, FaasReactClient, type FaasReactClientInstance, type FaasReactClientOptions, FormContainer as Form, type FormContextProps, FormContextProvider, FormDefaultElements, type FormElementTypes, type FormProps, type OnError, OptionalWrapper, type OptionalWrapperProps, createSplittingContext, equal, faas, getClient, useConstant, useEqualCallback, useEqualEffect, useEqualMemo, useEqualMemoize, useFaas, type useFaasOptions, useFormContext, useSplittingState, withFaasData };
|
package/dist/index.js
CHANGED
|
@@ -335,110 +335,76 @@ var OptionalWrapper = ({ condition, Wrapper, wrapperProps, children }) => {
|
|
|
335
335
|
OptionalWrapper.whyDidYouRender = true;
|
|
336
336
|
|
|
337
337
|
// src/Form/context.tsx
|
|
338
|
-
var FormContext = createSplittingContext(["items", "onSubmit", "
|
|
338
|
+
var FormContext = createSplittingContext(["items", "onSubmit", "Elements", "submitting", "setSubmitting", "values", "setValues"]);
|
|
339
339
|
var FormContextProvider = FormContext.Provider;
|
|
340
340
|
var useFormContext = FormContext.use;
|
|
341
341
|
function FormLabel(props) {
|
|
342
|
-
const {
|
|
343
|
-
if (props.
|
|
344
|
-
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
342
|
+
const { Elements } = useFormContext();
|
|
343
|
+
if (props.Label) return /* @__PURE__ */ jsxRuntime.jsx(props.Label, { ...props });
|
|
344
|
+
return /* @__PURE__ */ jsxRuntime.jsx(Elements.Label, { ...props });
|
|
345
345
|
}
|
|
346
346
|
function FormBody() {
|
|
347
347
|
const { items } = useFormContext();
|
|
348
348
|
return items.map((item) => /* @__PURE__ */ jsxRuntime.jsx(FormLabel, { ...item }, item.name));
|
|
349
349
|
}
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
if (typeof input === "object" && "target" in input) {
|
|
353
|
-
value = input.target.value;
|
|
354
|
-
}
|
|
355
|
-
switch (rules?.type) {
|
|
356
|
-
case "number":
|
|
357
|
-
return Number(value);
|
|
358
|
-
case "string":
|
|
359
|
-
return String(value);
|
|
360
|
-
default:
|
|
361
|
-
return value;
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
function FormInput({
|
|
350
|
+
var FormInputElement = react.forwardRef(({ onChange, ...props }, ref) => /* @__PURE__ */ jsxRuntime.jsx("input", { ...props, onChange: (e) => onChange(e.target.value), ref }));
|
|
351
|
+
var FormLabelElement = ({
|
|
365
352
|
name,
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
353
|
+
title,
|
|
354
|
+
description,
|
|
355
|
+
Label,
|
|
356
|
+
input
|
|
357
|
+
}) => {
|
|
358
|
+
const { values, setValues } = useFormContext();
|
|
359
|
+
if (Label) return /* @__PURE__ */ jsxRuntime.jsx(Label, { name, title, description, input });
|
|
360
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("label", { children: [
|
|
361
|
+
title ?? name,
|
|
362
|
+
input?.Input ? /* @__PURE__ */ jsxRuntime.jsx(
|
|
363
|
+
input.Input,
|
|
374
364
|
{
|
|
375
365
|
name,
|
|
376
|
-
value,
|
|
366
|
+
value: values[name],
|
|
377
367
|
onChange: (v) => setValues((prev) => ({
|
|
378
368
|
...prev,
|
|
379
369
|
[name]: v
|
|
380
370
|
}))
|
|
381
371
|
}
|
|
382
|
-
)
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
onChange: (v) => setValues((prev) => ({
|
|
406
|
-
...prev,
|
|
407
|
-
[name]: processValue(v, rules)
|
|
408
|
-
})),
|
|
409
|
-
...rest.props
|
|
410
|
-
}
|
|
411
|
-
);
|
|
412
|
-
}
|
|
372
|
+
) : /* @__PURE__ */ jsxRuntime.jsx(
|
|
373
|
+
FormInputElement,
|
|
374
|
+
{
|
|
375
|
+
name,
|
|
376
|
+
value: values[name],
|
|
377
|
+
onChange: (v) => setValues((prev) => ({
|
|
378
|
+
...prev,
|
|
379
|
+
[name]: v
|
|
380
|
+
}))
|
|
381
|
+
}
|
|
382
|
+
),
|
|
383
|
+
description
|
|
384
|
+
] });
|
|
385
|
+
};
|
|
386
|
+
var FormButtonElement = react.forwardRef(({ disabled, children, onClick, ...props }, ref) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
387
|
+
"button",
|
|
388
|
+
{
|
|
389
|
+
type: "button",
|
|
390
|
+
disabled,
|
|
391
|
+
onClick,
|
|
392
|
+
...props,
|
|
393
|
+
ref,
|
|
394
|
+
children
|
|
413
395
|
}
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
[name]: processValue(v, rules)
|
|
422
|
-
}))
|
|
423
|
-
}
|
|
424
|
-
);
|
|
425
|
-
}
|
|
426
|
-
var FormElements = {
|
|
427
|
-
label: react.forwardRef(({ name, label, input, rules }, ref) => /* @__PURE__ */ jsxRuntime.jsxs("label", { ref, children: [
|
|
428
|
-
label?.title ?? name,
|
|
429
|
-
/* @__PURE__ */ jsxRuntime.jsx(FormInput, { name, rules, ...input }),
|
|
430
|
-
label?.description
|
|
431
|
-
] })),
|
|
432
|
-
input: react.forwardRef(
|
|
433
|
-
(props, ref) => /* @__PURE__ */ jsxRuntime.jsx("input", { ...props, ref })
|
|
434
|
-
),
|
|
435
|
-
select: react.forwardRef(({ options, ...props }, ref) => /* @__PURE__ */ jsxRuntime.jsx("select", { ...props, ref, children: options?.map((option) => /* @__PURE__ */ jsxRuntime.jsx("option", { value: option.value, children: option.label }, option.value)) })),
|
|
436
|
-
button: react.forwardRef(({ disabled, children, onClick, ...props }, ref) => /* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", disabled, onClick, ...props, ref, children }))
|
|
396
|
+
));
|
|
397
|
+
|
|
398
|
+
// src/Form/elements/index.ts
|
|
399
|
+
var FormDefaultElements = {
|
|
400
|
+
Label: FormLabelElement,
|
|
401
|
+
Input: FormInputElement,
|
|
402
|
+
Button: FormButtonElement
|
|
437
403
|
};
|
|
438
404
|
function FormFooter() {
|
|
439
|
-
const { submitting, setSubmitting, onSubmit, values,
|
|
405
|
+
const { submitting, setSubmitting, onSubmit, values, Elements } = useFormContext();
|
|
440
406
|
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
441
|
-
|
|
407
|
+
Elements.Button,
|
|
442
408
|
{
|
|
443
409
|
disabled: submitting,
|
|
444
410
|
onClick: () => {
|
|
@@ -459,7 +425,7 @@ function FormContainer({ defaultValues, elements, ...props }) {
|
|
|
459
425
|
const states = useSplittingState({
|
|
460
426
|
values: mergeValues(props.items, defaultValues),
|
|
461
427
|
submitting: false,
|
|
462
|
-
|
|
428
|
+
Elements: Object.assign(FormDefaultElements, elements)
|
|
463
429
|
});
|
|
464
430
|
return /* @__PURE__ */ jsxRuntime.jsxs(
|
|
465
431
|
FormContextProvider,
|
|
@@ -480,6 +446,8 @@ exports.ErrorBoundary = ErrorBoundary;
|
|
|
480
446
|
exports.FaasDataWrapper = FaasDataWrapper;
|
|
481
447
|
exports.FaasReactClient = FaasReactClient;
|
|
482
448
|
exports.Form = FormContainer;
|
|
449
|
+
exports.FormContextProvider = FormContextProvider;
|
|
450
|
+
exports.FormDefaultElements = FormDefaultElements;
|
|
483
451
|
exports.OptionalWrapper = OptionalWrapper;
|
|
484
452
|
exports.createSplittingContext = createSplittingContext;
|
|
485
453
|
exports.equal = equal;
|
|
@@ -491,5 +459,6 @@ exports.useEqualEffect = useEqualEffect;
|
|
|
491
459
|
exports.useEqualMemo = useEqualMemo;
|
|
492
460
|
exports.useEqualMemoize = useEqualMemoize;
|
|
493
461
|
exports.useFaas = useFaas;
|
|
462
|
+
exports.useFormContext = useFormContext;
|
|
494
463
|
exports.useSplittingState = useSplittingState;
|
|
495
464
|
exports.withFaasData = withFaasData;
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { forwardRef, useRef, useMemo, useEffect, useCallback, createContext, useState, cloneElement, Component, useContext } from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
3
3
|
import { FaasBrowserClient } from '@faasjs/browser';
|
|
4
4
|
|
|
5
5
|
// src/constant.ts
|
|
@@ -333,110 +333,76 @@ var OptionalWrapper = ({ condition, Wrapper, wrapperProps, children }) => {
|
|
|
333
333
|
OptionalWrapper.whyDidYouRender = true;
|
|
334
334
|
|
|
335
335
|
// src/Form/context.tsx
|
|
336
|
-
var FormContext = createSplittingContext(["items", "onSubmit", "
|
|
336
|
+
var FormContext = createSplittingContext(["items", "onSubmit", "Elements", "submitting", "setSubmitting", "values", "setValues"]);
|
|
337
337
|
var FormContextProvider = FormContext.Provider;
|
|
338
338
|
var useFormContext = FormContext.use;
|
|
339
339
|
function FormLabel(props) {
|
|
340
|
-
const {
|
|
341
|
-
if (props.
|
|
342
|
-
return /* @__PURE__ */ jsx(
|
|
340
|
+
const { Elements } = useFormContext();
|
|
341
|
+
if (props.Label) return /* @__PURE__ */ jsx(props.Label, { ...props });
|
|
342
|
+
return /* @__PURE__ */ jsx(Elements.Label, { ...props });
|
|
343
343
|
}
|
|
344
344
|
function FormBody() {
|
|
345
345
|
const { items } = useFormContext();
|
|
346
346
|
return items.map((item) => /* @__PURE__ */ jsx(FormLabel, { ...item }, item.name));
|
|
347
347
|
}
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
if (typeof input === "object" && "target" in input) {
|
|
351
|
-
value = input.target.value;
|
|
352
|
-
}
|
|
353
|
-
switch (rules?.type) {
|
|
354
|
-
case "number":
|
|
355
|
-
return Number(value);
|
|
356
|
-
case "string":
|
|
357
|
-
return String(value);
|
|
358
|
-
default:
|
|
359
|
-
return value;
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
function FormInput({
|
|
348
|
+
var FormInputElement = forwardRef(({ onChange, ...props }, ref) => /* @__PURE__ */ jsx("input", { ...props, onChange: (e) => onChange(e.target.value), ref }));
|
|
349
|
+
var FormLabelElement = ({
|
|
363
350
|
name,
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
351
|
+
title,
|
|
352
|
+
description,
|
|
353
|
+
Label,
|
|
354
|
+
input
|
|
355
|
+
}) => {
|
|
356
|
+
const { values, setValues } = useFormContext();
|
|
357
|
+
if (Label) return /* @__PURE__ */ jsx(Label, { name, title, description, input });
|
|
358
|
+
return /* @__PURE__ */ jsxs("label", { children: [
|
|
359
|
+
title ?? name,
|
|
360
|
+
input?.Input ? /* @__PURE__ */ jsx(
|
|
361
|
+
input.Input,
|
|
372
362
|
{
|
|
373
363
|
name,
|
|
374
|
-
value,
|
|
364
|
+
value: values[name],
|
|
375
365
|
onChange: (v) => setValues((prev) => ({
|
|
376
366
|
...prev,
|
|
377
367
|
[name]: v
|
|
378
368
|
}))
|
|
379
369
|
}
|
|
380
|
-
)
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
onChange: (v) => setValues((prev) => ({
|
|
404
|
-
...prev,
|
|
405
|
-
[name]: processValue(v, rules)
|
|
406
|
-
})),
|
|
407
|
-
...rest.props
|
|
408
|
-
}
|
|
409
|
-
);
|
|
410
|
-
}
|
|
370
|
+
) : /* @__PURE__ */ jsx(
|
|
371
|
+
FormInputElement,
|
|
372
|
+
{
|
|
373
|
+
name,
|
|
374
|
+
value: values[name],
|
|
375
|
+
onChange: (v) => setValues((prev) => ({
|
|
376
|
+
...prev,
|
|
377
|
+
[name]: v
|
|
378
|
+
}))
|
|
379
|
+
}
|
|
380
|
+
),
|
|
381
|
+
description
|
|
382
|
+
] });
|
|
383
|
+
};
|
|
384
|
+
var FormButtonElement = forwardRef(({ disabled, children, onClick, ...props }, ref) => /* @__PURE__ */ jsx(
|
|
385
|
+
"button",
|
|
386
|
+
{
|
|
387
|
+
type: "button",
|
|
388
|
+
disabled,
|
|
389
|
+
onClick,
|
|
390
|
+
...props,
|
|
391
|
+
ref,
|
|
392
|
+
children
|
|
411
393
|
}
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
[name]: processValue(v, rules)
|
|
420
|
-
}))
|
|
421
|
-
}
|
|
422
|
-
);
|
|
423
|
-
}
|
|
424
|
-
var FormElements = {
|
|
425
|
-
label: forwardRef(({ name, label, input, rules }, ref) => /* @__PURE__ */ jsxs("label", { ref, children: [
|
|
426
|
-
label?.title ?? name,
|
|
427
|
-
/* @__PURE__ */ jsx(FormInput, { name, rules, ...input }),
|
|
428
|
-
label?.description
|
|
429
|
-
] })),
|
|
430
|
-
input: forwardRef(
|
|
431
|
-
(props, ref) => /* @__PURE__ */ jsx("input", { ...props, ref })
|
|
432
|
-
),
|
|
433
|
-
select: forwardRef(({ options, ...props }, ref) => /* @__PURE__ */ jsx("select", { ...props, ref, children: options?.map((option) => /* @__PURE__ */ jsx("option", { value: option.value, children: option.label }, option.value)) })),
|
|
434
|
-
button: forwardRef(({ disabled, children, onClick, ...props }, ref) => /* @__PURE__ */ jsx("button", { type: "button", disabled, onClick, ...props, ref, children }))
|
|
394
|
+
));
|
|
395
|
+
|
|
396
|
+
// src/Form/elements/index.ts
|
|
397
|
+
var FormDefaultElements = {
|
|
398
|
+
Label: FormLabelElement,
|
|
399
|
+
Input: FormInputElement,
|
|
400
|
+
Button: FormButtonElement
|
|
435
401
|
};
|
|
436
402
|
function FormFooter() {
|
|
437
|
-
const { submitting, setSubmitting, onSubmit, values,
|
|
403
|
+
const { submitting, setSubmitting, onSubmit, values, Elements } = useFormContext();
|
|
438
404
|
return /* @__PURE__ */ jsx(
|
|
439
|
-
|
|
405
|
+
Elements.Button,
|
|
440
406
|
{
|
|
441
407
|
disabled: submitting,
|
|
442
408
|
onClick: () => {
|
|
@@ -457,7 +423,7 @@ function FormContainer({ defaultValues, elements, ...props }) {
|
|
|
457
423
|
const states = useSplittingState({
|
|
458
424
|
values: mergeValues(props.items, defaultValues),
|
|
459
425
|
submitting: false,
|
|
460
|
-
|
|
426
|
+
Elements: Object.assign(FormDefaultElements, elements)
|
|
461
427
|
});
|
|
462
428
|
return /* @__PURE__ */ jsxs(
|
|
463
429
|
FormContextProvider,
|
|
@@ -474,4 +440,4 @@ function FormContainer({ defaultValues, elements, ...props }) {
|
|
|
474
440
|
);
|
|
475
441
|
}
|
|
476
442
|
|
|
477
|
-
export { ErrorBoundary, FaasDataWrapper, FaasReactClient, FormContainer as Form, OptionalWrapper, createSplittingContext, equal, faas, getClient, useConstant, useEqualCallback, useEqualEffect, useEqualMemo, useEqualMemoize, useFaas, useSplittingState, withFaasData };
|
|
443
|
+
export { ErrorBoundary, FaasDataWrapper, FaasReactClient, FormContainer as Form, FormContextProvider, FormDefaultElements, OptionalWrapper, createSplittingContext, equal, faas, getClient, useConstant, useEqualCallback, useEqualEffect, useEqualMemo, useEqualMemoize, useFaas, useFormContext, useSplittingState, withFaasData };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/react",
|
|
3
|
-
"version": "3.7.0-beta.
|
|
3
|
+
"version": "3.7.0-beta.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.0-beta.
|
|
37
|
+
"@faasjs/browser": "3.7.0-beta.1"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@faasjs/browser": "3.7.0-beta.
|
|
40
|
+
"@faasjs/browser": "3.7.0-beta.1",
|
|
41
41
|
"@types/react": "*",
|
|
42
42
|
"react": "*"
|
|
43
43
|
},
|